一.什么是redistemplate
redistemplate 是一个工具类,由 spring 官方提供的方便操作 redis 数据库的一个工具类,来源于 org.springframework.data.redis.core 包下。其本质属于 spring-data 模块下的 spring-data-redis 部分,它提供了从 spring 应用程序轻松配置和访问 redis的功能。
spring-data-redis 是通过整合lettuce和jedis这俩种redis客户端产生的,对外则提供了redistemplate这样统一的api来供调用者访问。它既支持luttuce的响应式编程也支持jdk中集合的实现。
二.如何使用redistemplate
首先要导入相关依赖
<!--redis依赖--> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-redis</artifactid> </dependency>
<!--连接池依赖--> <dependency> <groupid>org.apache.commons</groupid> <artifactid>commons-pool2</artifactid> <version>2.11.1</version> </dependency>
导入相关依赖之后,通过配置文件 application.yml 进行配置,由于 redistemplate 是整合的lettuce和jedis,因此在配置连接池的时候需要进行选择是使用lettuce还是jedis(默认是lettuce)
spring: data: redis: host: redis所在主机地址 port: redis对应端口号默认6379 password: 密码 lettuce: pool: max-active: 最大连接数 max-wait: 等待时长 max-idle: 最大空闲连接 min-idle: 最小空闲连接
在配置完成后,通过依赖注入就可以直接使用
@autowired private redistemplate redistemplate; @test void testcode() { string chechecode = "cheche_code"; string code = "168901"; //写入redis redistemplate.opsforvalue().set(chechecode, code); //读出redis object o = redistemplate.opsforvalue().get(chechecode); system.out.println("验证码为" + o); }
运行结果:
redistemplate的api
redistemplate 提供了丰富的方法来实现对 redis 的各种操作,包括但不限于字符串、哈希、列表、集合和有序集合等数据结构的操作。以下是一些常用的 redistemplate api:
字符串操作
opsforvalue().set(key, value)
: 设置字符串值。opsforvalue().get(key)
: 获取字符串值。opsforvalue().incr(key)
: 字符串值自增。opsforvalue().decr(key)
: 字符串值自减。
哈希操作
opsforhash().getoperations().put(key, hashkey, value)
: 向哈希中添加键值对。opsforhash().getoperations().get(key, hashkey)
: 获取哈希中的值。opsforhash().getoperations().entries(key)
: 获取哈希中的所有键值对。
列表操作
opsforlist().leftpush(key, value)
: 从列表左侧添加元素。opsforlist().rightpush(key, value)
: 从列表右侧添加元素。opsforlist().leftpop(key)
: 从列表左侧弹出元素。opsforlist().rightpop(key)
: 从列表右侧弹出元素。
集合操作
opsforset().add(key, value)
: 向集合中添加元素。opsforset().members(key)
: 获取集合中的所有元素。opsforset().remove(key, value)
: 从集合中移除元素。
有序集合操作
opsforzset().add(key, value, score)
: 向有序集合中添加元素,并指定分数。opsforzset().range(key, start, end)
: 获取有序集合中指定分数范围内的元素。opsforzset().removerangebyscore(key, minscore, maxscore)
: 按分数范围移除有序集合中的元素。
键操作
delete(key)
: 删除键。haskey(key)
: 检查键是否存在。keys(pattern)
: 根据模式匹配获取所有键。
事务
multi()
: 开启事务。exec()
: 提交事务。
发布/订阅
convertandsend(channel, message)
: 发布消息。subscribe(redismessagelistenercontainer, messagelistener)
: 订阅消息。
连接管理
getconnectionfactory()
: 获取连接工厂。getexecutor()
: 获取执行器。
序列化
setkeyserializer(serializer)
: 设置键的序列化器。setvalueserializer(serializer)
: 设置值的序列化器。
更多操作可以查看官方提供的api文档:redistemplate (spring data redis 3.3.2 api)
序列化
我们打开redis图形化工具查看一下刚才的验证码会发现刚才存入的验证码变成了一堆乱码
这是因为redis的序列化并没有按照我们预期的进行转化,我们需要自己去重写一个序列化,如下将key设置为string类型、value设置为json类型,最后将这个对象交给spring管理,之后在调用该对象的时候就会自动选择我们配置的这个
@configuration public class redisconfig { @bean public redistemplate<string, object> redistemplate(redisconnectionfactory connectionfactory) { //创建redistemplate对象 redistemplate<string, object> template = new redistemplate<>(); //设置连接工厂 template.setconnectionfactory(connectionfactory); //创建json序列化工具 genericjackson2jsonredisserializer jsonserializer = new genericjackson2jsonredisserializer(); //设置key的序列化 template.setkeyserializer(redisserializer.string()); template.sethashvalueserializer(redisserializer.string()); //设置value的序列化 template.setvalueserializer(jsonserializer); template.sethashvalueserializer(jsonserializer); return template; } }
对于json序列化工具,我们也需要引入依赖:
<!--jackson依赖--> <dependency> <groupid>com.fasterxml.jackson.core</groupid> <artifactid>jackson-databind</artifactid> <version>2.17.1</version> </dependency>
我们再次打开测试观察结果:
@springboottest class redisdemoapplicationtests { @autowired private redistemplate<string, object> redistemplate; @test void testcode() { string chechecode = "cheche_code"; string code = "168901"; //写入redis redistemplate.opsforvalue().set(chechecode, code); //读出redis object o = redistemplate.opsforvalue().get(chechecode); system.out.println("验证码为" + o); } }
我们可以发现数据已经正常显示出来了
三.stringredistemplate
由于存储在 redis 中的 key 和 value 通常是很常见的 string 类型,redis模块提供了 redisconnection 和 redistemplate 的扩展,分是 stringredisconnection 和 stringredistemplate,作为字符串操作的解决方案。
打开源码我们可以看见对于key、value、hashkey、hashvalue都是进行string类型的序列化。
因此对于一些复杂类型,如对象在stringredistemplate的时候往往需要自己手动序列化将对象转为json再存入redis。
到此这篇关于redistemplate的使用与注意事项小结的文章就介绍到这了,更多相关redistemplate使用内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论