当前位置: 移动技术网 > IT编程>开发语言>Java > Springboot 2.0 - 集成redis

Springboot 2.0 - 集成redis

2018年10月04日  | 移动技术网IT编程  | 我要评论

最近在入门springboot,然后在感慨 springboot较于spring真的方便多时,顺便记录下自己在集成redis时的一些想法。

1、从springboot官网查看redis的依赖包

<dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-data-redis</artifactid>
</dependency>

2、操作redis

/*
   操作k-v都是字符串的
 */
@autowired
stringredistemplate stringredistemplet;

 /*
     操作k-v都是对象的
 */
@autowired
redistemplate redistemplate;

redis的包中提供了两个可以操作方法,根据不同类型的值相对应选择。

两个操作方法对应的redis操作都是相同的

 stringredistemplet.opsforvalue() // 字符串
 stringredistemplet.opsforlist() // 列表
 stringredistemplet.opsforset() // 集合
 stringredistemplet.opsforhash() // 哈希
 stringredistemplet.opsforzset() // 有序集合

3、修改数据的存储方式

在stringredistemplet中,默认都是存储字符串的形式;在redistemplet中,值可以是某个对象,而redis默认把对象序列化后存储在redis中(所以存放的对象默认情况下需要序列化)

如果需要更改数据的存储方式,如采用json来存储在redis中,而不是以序列化后的形式。

1)自己创建一个redistemplate实例,在该实例中自己定义json的序列化格式(org.springframework.data.redis.serializer.jackson2jsonredisserializer)

// 这里传入的是employee对象(employee 要求可以序列化)
jackson2jsonredisserializer<employee> jackson2jsonredisserializer = new jackson2jsonredisserializer<employee>(employee.class);

2)把定义的格式放进自己定义的redistemplate实例中

redistemplate<object,employee> template = new redistemplate<>();
template.setconnectionfactory(redisconnectionfactory);
// 定义格式
jackson2jsonredisserializer<employee> jackson2jsonredisserializer = new jackson2jsonredisserializer<employee>(employee.class);
// 放入redistemplate实例中
template.setdefaultserializer(jackson2jsonredisserializer);

参考代码:

@bean
 public redistemplate<object,employee> employeeredistemplate(redisconnectionfactory redisconnectionfactory)throws unknownhostexception{
        redistemplate<object,employee> template = new redistemplate<>();
        template.setconnectionfactory(redisconnectionfactory); 
        jackson2jsonredisserializer<employee> jackson2jsonredisserializer = new jackson2jsonredisserializer<employee>(employee.class);
        template.setdefaultserializer(jackson2jsonredisserializer);
        return template;
    }

原理:

@configuration
@conditionalonclass({redisoperations.class})
@enableconfigurationproperties({redisproperties.class})
@import({lettuceconnectionconfiguration.class, jedisconnectionconfiguration.class})
public class redisautoconfiguration {
    public redisautoconfiguration() {
    }

    @bean
    @conditionalonmissingbean(
        name = {"redistemplate"}
    ) // 在容器当前没有redistemplate时运行
    public redistemplate<object, object> redistemplate(redisconnectionfactory redisconnectionfactory) throws unknownhostexception {
        redistemplate<object, object> template = new redistemplate();
        template.setconnectionfactory(redisconnectionfactory);
        return template;
    }

    @bean
    @conditionalonmissingbean // 在容器当前没有stringredistemplate时运行
    public stringredistemplate stringredistemplate(redisconnectionfactory redisconnectionfactory) throws unknownhostexception {
        stringredistemplate template = new stringredistemplate();
        template.setconnectionfactory(redisconnectionfactory);
        return template;
    }
}

如果你自己定义了redistemplate后并添加@bean注解,(要在配置类中定义),那么默认的redistemplate就不会被添加到容器中,运行的就是自己定义的reidstemplate实例,而你在实例中自己定义了序列化格式,所以就会以你采用的格式定义存放在redis中的对象。

4、更改默认的缓冲

springboot默认提供基于注解的缓冲,只要在主程序类(xxxapplication)标注@enablecaching,缓冲注解有

@cachingable、@cachingevict、@cachingput,并且该缓冲默认使用的是concurrenthashmapcachemanager

当引入redis的starter后,容器中保存的是rediscachemanager ,rediscachemanager创建rediscache作为缓冲组件,rediscache通过操纵redis缓冲数据

5、修改redis缓冲的序列化机制

在springboot中,如果要修改序列化机制,可以直接建立一个配置类,在配置类中自定义cachemanager,在cachemanager中可以自定义序列化的规则,默认的序列化规则是采用jdk的序列化

注:在springboot 1.5.6 和springboot 2.0.5 的版本中自定义cachemanager存在差异

参考代码:

// springboot 1.x的版本
public rediscachemanager employeecachemanager(redisconnectionfactory redisconnectionfactory){
    
    // 1、自定义redistemplate
    redistemplate<object,employee> template = new redistemplate<>();
    template.setconnectionfactory(redisconnectionfactory);
    jackson2jsonredisserializer<employee> jackson2jsonredisserializer = new jackson2jsonredisserializer<employee>(employee.class);
    template.setdefaultserializer(jackson2jsonredisserializer);
    
    // 2、自定义rediscachemanager
    rediscachemanager cachemanager = new rediscachemanager(template);
    cachemanager.setuseprefix(true); // 会将cachename作为key的前缀
    
    return cachemanager;
}



// springboot 2.x的版本

/**
 * serializekeyswith() 修改key的序列化规则,这里采用的是stringredisserializer()
 * serializevalueswith() 修改value的序列化规则,这里采用的是jackson2jsonredisserializer<employee>(employee.class)
 * @param factory
 * @return
 */
@bean
public rediscachemanager employeecachemanager(redisconnectionfactory redisconnectionfactory) {


rediscacheconfiguration config = rediscacheconfiguration.defaultcacheconfig()
              .serializekeyswith(redisserializationcontext.serializationpair.fromserializer(new stringredisserializer())) 
             .serializevalueswith(redisserializationcontext.serializationpair.fromserializer(new jackson2jsonredisserializer<employee>(employee.class)));

        rediscachemanager cachemanager = rediscachemanager.builder(redisconnectionfactory).cachedefaults(config).build();

        return cachemanager;
    }

tip:可以通过查看各版本的org.springframework.data.redis.cache.rediscacheconfiguration去自定义cachemanager.

因为不同版本的springboot对应的redis版本也是不同的,所以要重写时可以查看官方是怎么定义cachemanager,才知道怎样去自定义cachemanager。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网