您好,欢迎访问一九零五行业门户网

SpringBoot中如何操作Redis

方案一:spring data redis创建工程
创建工程,引入 redis 依赖:
创建成功后,还需要手动引入 commos-pool2 的依赖,因此最终完整的 pom.xml 依赖如下:
<dependencies> <dependency>  <groupid>org.springframework.boot</groupid>  <artifactid>spring-boot-starter-data-redis</artifactid> </dependency> <dependency>  <groupid>org.springframework.boot</groupid>  <artifactid>spring-boot-starter-web</artifactid> </dependency></dependencies><dependency> <groupid>org.apache.commons</groupid> <artifactid>commons-pool2</artifactid></dependency>
这里主要就是引入了 spring data redis + 连接池。
配置 redis 信息
接下来配置 redis 的信息,信息包含两方面,一方面是 redis 的基本信息,另一方面则是连接池信息:
spring.redis.database=0spring.redis.password=123spring.redis.port=6379spring.redis.host=192.168.66.128spring.redis.lettuce.pool.min-idle=5spring.redis.lettuce.pool.max-idle=10spring.redis.lettuce.pool.max-active=8spring.redis.lettuce.pool.max-wait=1msspring.redis.lettuce.shutdown-timeout=100ms
自动配置
当开发者在项目中引入了 spring data redis ,并且配置了 redis 的基本信息,此时,自动化配置就会生效。
我们从 spring boot 中 redis 的自动化配置类中就可以看出端倪:
@configuration@conditionalonclass(redisoperations.class)@enableconfigurationproperties(redisproperties.class)@import({ lettuceconnectionconfiguration.class, jedisconnectionconfiguration.class })public class redisautoconfiguration {  @bean  @conditionalonmissingbean(name = redistemplate)  public redistemplate<object, object> redistemplate(      redisconnectionfactory redisconnectionfactory) throws unknownhostexception {    redistemplate<object, object> template = new redistemplate<>();    template.setconnectionfactory(redisconnectionfactory);    return template;  }  @bean  @conditionalonmissingbean  public stringredistemplate stringredistemplate(      redisconnectionfactory redisconnectionfactory) throws unknownhostexception {    stringredistemplate template = new stringredistemplate();    template.setconnectionfactory(redisconnectionfactory);    return template;  }}
这个自动化配置类很好理解:
首先标记这个是一个配置类,同时该配置在 redisoperations 存在的情况下才会生效(即项目中引入了 spring data redis)
然后导入在 application.properties 中配置的属性
然后再导入连接池信息(如果存在的话)
最后,提供了两个 bean ,redistemplate 和 stringredistemplate ,其中 stringredistemplate 是 redistemplate 的子类,两个的方法基本一致,不同之处主要体现在操作的数据类型不同,redistemplate 中的两个泛型都是 object ,意味者存储的 key 和 value 都可以是一个对象,而 stringredistemplate 的 两个泛型都是 string ,意味者 stringredistemplate 的 key 和 value 都只能是字符串。只有在开发者没有提供相关的 bean 时,这两个配置才会生效。如果有提供相关的 bean,则不会生效。
使用
接下来,可以直接在 service 中注入 stringredistemplate 或者 redistemplate 来使用:
@servicepublic class helloservice { @autowired redistemplate redistemplate; public void hello() {  valueoperations ops = redistemplate.opsforvalue();  ops.set(k1, v1);  object k1 = ops.get(k1);  system.out.println(k1); }}
redis 中的数据操作,大体上来说,可以分为两种:
针对 key 的操作,相关的方法就在 redistemplate 中
针对具体数据类型的操作,相关的方法需要首先获取对应的数据类型,获取相应数据类型的操作方法是 opsforxxx
调用该方法就可以将数据存储到 redis 中去了,如下:
k1 前面的字符是由于使用了 redistemplate 导致的,redistemplate 对 key 进行序列化之后的结果。
redistemplate 中,key 默认的序列化方案是 jdkserializationredisserializer 。
而在 stringredistemplate 中,key 默认的序列化方案是 stringredisserializer ,因此,如果使用 stringredistemplate ,默认情况下 key 前面不会有前缀。
不过开发者也可以自行修改 redistemplate 中的序列化方案,如下:
@servicepublic class helloservice { @autowired redistemplate redistemplate; public void hello() {  redistemplate.setkeyserializer(new stringredisserializer());  valueoperations ops = redistemplate.opsforvalue();  ops.set(k1, v1);  object k1 = ops.get(k1);  system.out.println(k1); }}
当然也可以直接使用 stringredistemplate:
@servicepublic class helloservice { @autowired stringredistemplate stringredistemplate; public void hello2() {  valueoperations ops = stringredistemplate.opsforvalue();  ops.set(k2, v2);  object k1 = ops.get(k2);  system.out.println(k1); }}
另外需要注意 ,spring boot 的自动化配置,只能配置单机的 redis ,如果是 redis 集群,则所有的东西都需要自己手动配置,关于如何操作 redis 集群,松哥以后再来和大家分享。
方案二:spring cache
通过 spring cache 的形式来操作 redis,spring cache 统一了缓存江湖的门面,这种方案,松哥之前有过一篇专门的文章介绍,小伙伴可以移步这里:spring boot中,redis缓存还能这么用!。
方案三:回归原始时代
第三种方案,就是直接使用 jedis 或者 其他的客户端工具来操作 redis ,这种方案在 spring boot 中也是支持的,虽然操作麻烦,但是支持
以上就是springboot中如何操作redis的详细内容。
其它类似信息

推荐信息