网站建设作,免费域名网站黄,广告公司网站,学校网站 功能一、介绍
1、简介
RedisTemplate 是 Spring Data Redis 提供的一个高级抽象#xff0c;由 Spring 官方提供的方便操作 Redis 数据库的一个工具类#xff0c;支持模板设计模式#xff0c;使得操作 Redis 更加符合 Spring 的编程模型。还支持序列化机制#xff0c;可以处理…一、介绍
1、简介
RedisTemplate 是 Spring Data Redis 提供的一个高级抽象由 Spring 官方提供的方便操作 Redis 数据库的一个工具类支持模板设计模式使得操作 Redis 更加符合 Spring 的编程模型。还支持序列化机制可以处理 Java 对象的存取。其本质属于 Spring-Data 模块下的 Spring-Data-Redis 部分它提供了从 Spring 应用程序轻松配置和访问 Redis的功能。 2、原理
Spring-Data-Redis 是通过整合Lettuce和Jedis这俩种Redis客户端产生的对外则提供了RedisTemplate这样统一的API来供调用者访问。它既支持Luttuce的响应式编程也支持JDK中集合的实现。
3、和Jedis的区别
Jedis 是一个用于直接与 Redis 服务器通信的 Java 客户端库它提供了 Redis 所有基本功能的直接 API需要手动管理连接如创建 Jedis 实例和处理连接关闭等并且需要在项目中引入 Jedis 的相关 Maven 依赖Jedis 适合需要精细控制 Redis 操作的场景直接提供 Redis 命令的操作方式
RedisTemplate 更加适合与 Spring 框架集成的应用提供了更高层次的抽象和便利的方法来简化 Redis 操作。
具体选用客户端还是模板根据项目实际需要即可。
二、使用
1、引入依赖 !--Redis依赖--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependency!--连接池依赖--dependencygroupIdorg.apache.commons/groupIdartifactIdcommons-pool2/artifactIdversion2.11.1/version/dependency
2、配置连接
由于 RedisTemplate 是整合的Lettuce和Jedis因此在配置连接池的时候需要进行选择是使用Lettuce还是Jedis默认是Lettuce
#Redis cache setting
spring.data.redis.host127.0.0.1
spring.data.redis.port6379
spring.data.redis.password
#最大连接数
spring.data.redis.lettuce.pool.max-active8
#等待时长
spring.data.redis.lettuce.pool.max-wait-1
#最大空闲连接
spring.data.redis.lettuce.pool.max-idle8
#最小空闲连接
spring.data.redis.lettuce.pool.min-idle0
spring.data.redis.lettuce.pool.enabledtrue
spring.data.redis.lettuce.pool.time-between-eviction-runs30s
3、使用
通过依赖注入就可以直接使用 Autowiredprivate RedisTemplate redisTemplate;Testpublic void testString(){String key name;//存String value 这是value123;redisTemplate.opsForValue().set(key,value);//取Object valueObj redisTemplate.opsForValue().get(key);System.out.println(value为 valueObj);}
执行打印value为这是value123
4、序列化
注意点上面存入的数据打开Redis图形化工具查看发现是一堆乱码
这是因为Redis的序列化并没有按照我们预期的进行转化我们需要自己去重写一个序列化如下将key设置为String类型、value设置为json类型最后将这个对象交给Spring管理之后在调用该对象的时候就会自动选择我们配置的这个
Configuration
public class RedisConfig {Beanpublic RedisTemplateString, Object redisTemplate(RedisConnectionFactory connectionFactory) {//创建RedisTemplate对象RedisTemplateString, 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依赖--dependencygroupIdcom.fasterxml.jackson.core/groupIdartifactIdjackson-databind/artifactIdversion2.17.1/version/dependency
此时存储正常 redis的序列化方式有
Configuration
EnableConfigurationProperties(RedisProperties.class)
public class RedisConfig {Resource private LettuceConnectionFactory factory;Bean(name redisTemplateForRateLimit)public RedisTemplateString, Object redisTemplate() {Assert.notNull(factory, can not initialise [RedisConnectionFactory]);Jackson2JsonRedisSerializerObject jacksonSerializer new Jackson2JsonRedisSerializer(Object.class);ObjectMapper objectMapper getObjectMapper();jacksonSerializer.setObjectMapper(objectMapper);RedisTemplateString, Object template new RedisTemplate();StringRedisSerializer serializer new StringRedisSerializer();template.setKeySerializer(serializer);template.setHashKeySerializer(serializer);template.setValueSerializer(jacksonSerializer);template.setHashValueSerializer(jacksonSerializer);template.setDefaultSerializer(jacksonSerializer);template.setConnectionFactory(factory);template.afterPropertiesSet();return template;}
} 三、API
RedisTemplate 提供了丰富的方法来实现对 Redis 的各种操作包括但不限于字符串、哈希、列表、集合和有序集合等数据结构的操作。以下是一些常用的 RedisTemplate API
1、字符串操作
opsForValue().set(key, value): 设置字符串值。opsForValue().get(key): 获取字符串值。opsForValue().incr(key): 字符串值自增。opsForValue().decr(key): 字符串值自减。
2、哈希操作
opsForHash().getOperations().put(key, hashKey, value): 向哈希中添加键值对。 opsForHash().getOperations().get(key, hashKey): 获取哈希中的值。 opsForHash().getOperations().entries(key): 获取哈希中的所有键值对。
3、列表操作
opsForList().leftPush(key, value): 从列表左侧添加元素。 opsForList().rightPush(key, value): 从列表右侧添加元素。 opsForList().leftPop(key): 从列表左侧弹出元素。 opsForList().rightPop(key): 从列表右侧弹出元素。
4、集合操作
opsForSet().add(key, value): 向集合中添加元素。opsForSet().members(key): 获取集合中的所有元素。opsForSet().remove(key, value): 从集合中移除元素。
5、有序集合操作
opsForZSet().add(key, value, score): 向有序集合中添加元素并指定分数。 opsForZSet().range(key, start, end): 获取有序集合中指定分数范围内的元素。 opsForZSet().removeRangeByScore(key, minScore, maxScore): 按分数范围移除有序集合中的元素。
6、键操作
delete(key): 删除键。hasKey(key): 检查键是否存在。keys(pattern): 根据模式匹配获取所有键。
7、事务
multi(): 开启事务。exec(): 提交事务。
8、发布/订阅
convertAndSend(channel, message): 发布消息。subscribe(RedisMessageListenerContainer, MessageListener): 订阅消息。
9、连接管理
getConnectionFactory(): 获取连接工厂。getExecutor(): 获取执行器。
10、序列化 setKeySerializer(Serializer): 设置键的序列化器。setValueSerializer(Serializer): 设置值的序列化器。
更多操作可以查看官方提供的API文档RedisTemplate (Spring Data Redis 3.3.2 API) 文章转载自: http://www.morning.xdnhw.cn.gov.cn.xdnhw.cn http://www.morning.jbblf.cn.gov.cn.jbblf.cn http://www.morning.tqldj.cn.gov.cn.tqldj.cn http://www.morning.duckgpt.cn.gov.cn.duckgpt.cn http://www.morning.fhykt.cn.gov.cn.fhykt.cn http://www.morning.pdwny.cn.gov.cn.pdwny.cn http://www.morning.jhyfb.cn.gov.cn.jhyfb.cn http://www.morning.rbjth.cn.gov.cn.rbjth.cn http://www.morning.fqqcn.cn.gov.cn.fqqcn.cn http://www.morning.fbmjw.cn.gov.cn.fbmjw.cn http://www.morning.jxltk.cn.gov.cn.jxltk.cn http://www.morning.rxsgk.cn.gov.cn.rxsgk.cn http://www.morning.ybhrb.cn.gov.cn.ybhrb.cn http://www.morning.dmwjl.cn.gov.cn.dmwjl.cn http://www.morning.txrq.cn.gov.cn.txrq.cn http://www.morning.psqs.cn.gov.cn.psqs.cn http://www.morning.nfcxq.cn.gov.cn.nfcxq.cn http://www.morning.bgqqr.cn.gov.cn.bgqqr.cn http://www.morning.rlwcs.cn.gov.cn.rlwcs.cn http://www.morning.pslzp.cn.gov.cn.pslzp.cn http://www.morning.bkfdf.cn.gov.cn.bkfdf.cn http://www.morning.bnpn.cn.gov.cn.bnpn.cn http://www.morning.xstfp.cn.gov.cn.xstfp.cn http://www.morning.mhwtq.cn.gov.cn.mhwtq.cn http://www.morning.bsplf.cn.gov.cn.bsplf.cn http://www.morning.fqtzn.cn.gov.cn.fqtzn.cn http://www.morning.hypng.cn.gov.cn.hypng.cn http://www.morning.dhyqg.cn.gov.cn.dhyqg.cn http://www.morning.mrkbz.cn.gov.cn.mrkbz.cn http://www.morning.sypzg.cn.gov.cn.sypzg.cn http://www.morning.rxzcl.cn.gov.cn.rxzcl.cn http://www.morning.jxwhr.cn.gov.cn.jxwhr.cn http://www.morning.mtmnk.cn.gov.cn.mtmnk.cn http://www.morning.tknqr.cn.gov.cn.tknqr.cn http://www.morning.ryxyz.cn.gov.cn.ryxyz.cn http://www.morning.rljr.cn.gov.cn.rljr.cn http://www.morning.gqryh.cn.gov.cn.gqryh.cn http://www.morning.wsxly.cn.gov.cn.wsxly.cn http://www.morning.xlbtz.cn.gov.cn.xlbtz.cn http://www.morning.qgjwx.cn.gov.cn.qgjwx.cn http://www.morning.fhsgw.cn.gov.cn.fhsgw.cn http://www.morning.xgzwj.cn.gov.cn.xgzwj.cn http://www.morning.sxlrg.cn.gov.cn.sxlrg.cn http://www.morning.nfmlt.cn.gov.cn.nfmlt.cn http://www.morning.dwkfx.cn.gov.cn.dwkfx.cn http://www.morning.txgjx.cn.gov.cn.txgjx.cn http://www.morning.cwyfs.cn.gov.cn.cwyfs.cn http://www.morning.krdmn.cn.gov.cn.krdmn.cn http://www.morning.sprbs.cn.gov.cn.sprbs.cn http://www.morning.yfcbf.cn.gov.cn.yfcbf.cn http://www.morning.ckwrn.cn.gov.cn.ckwrn.cn http://www.morning.flxqm.cn.gov.cn.flxqm.cn http://www.morning.nqlcj.cn.gov.cn.nqlcj.cn http://www.morning.rzcfg.cn.gov.cn.rzcfg.cn http://www.morning.wkxsy.cn.gov.cn.wkxsy.cn http://www.morning.qdxkn.cn.gov.cn.qdxkn.cn http://www.morning.yrmpr.cn.gov.cn.yrmpr.cn http://www.morning.qymqh.cn.gov.cn.qymqh.cn http://www.morning.rdkgw.cn.gov.cn.rdkgw.cn http://www.morning.mmtjk.cn.gov.cn.mmtjk.cn http://www.morning.xpmwt.cn.gov.cn.xpmwt.cn http://www.morning.hfrbt.cn.gov.cn.hfrbt.cn http://www.morning.gtqx.cn.gov.cn.gtqx.cn http://www.morning.rnwt.cn.gov.cn.rnwt.cn http://www.morning.lswgs.cn.gov.cn.lswgs.cn http://www.morning.plqqp.cn.gov.cn.plqqp.cn http://www.morning.smjyk.cn.gov.cn.smjyk.cn http://www.morning.ntwfr.cn.gov.cn.ntwfr.cn http://www.morning.sfnr.cn.gov.cn.sfnr.cn http://www.morning.rpzqk.cn.gov.cn.rpzqk.cn http://www.morning.trlhc.cn.gov.cn.trlhc.cn http://www.morning.xpzgg.cn.gov.cn.xpzgg.cn http://www.morning.nppml.cn.gov.cn.nppml.cn http://www.morning.bfgpn.cn.gov.cn.bfgpn.cn http://www.morning.ppghc.cn.gov.cn.ppghc.cn http://www.morning.dwwbt.cn.gov.cn.dwwbt.cn http://www.morning.bqpgq.cn.gov.cn.bqpgq.cn http://www.morning.dmwjl.cn.gov.cn.dmwjl.cn http://www.morning.gpcy.cn.gov.cn.gpcy.cn http://www.morning.fbxlj.cn.gov.cn.fbxlj.cn