原文连接:
(1)、https://blog.csdn.net/weixin_39025362/article/details/105042476?spm=1001.2101.3001.6650.4&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-4-105042476-blog-117926658.pc_relevant_scanpaymentv1&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-4-105042476-blog-117926658.pc_relevant_scanpaymentv1&utm_relevant_index=9
(2)、https://blog.csdn.net/XinhuaShuDiao/article/details/84906538
1. 先了解Redistemplate和StringRedistemplate之间的关系:
Redistemplate是Spring对于Redis的封装,而StringRedistemplate继承Redistemplate。
两者的数据是不共通的;也就是说StringRedistemplate只能管理StringRedistemplate里面的数据,
Redistemplate只能管理Redistemplate中的数据。
StringRedistemplate默认采用的是String的序列化策略,保存的key和value都是采用此策略序列化保存的。
Redistemplate默认采用的是JDK的序列化策略,保存的key和value都是采用此策略序列化保存的。
2. Redistemplate和StringRedistemplate使用上的差别
Redistemplate:
redistemplate中定义了对几种常用数据结构操作:
redistemplate.opsForList(); //操作list
redistemplate.opsForValue(); //操作字符串
redistemplate.opsForCluster(); //集群时使用
redistemplate.opsForGeo(); //地理位置时使用
redistemplate.opsForHash(); //操作hash
redistemplate.opsForSet(); //操作set
redistemplate.opsForZSet(); //操作有序set
- StringRedistemplate
StringRedistemplate中定义的5中数据结构,其实和redistemplate一样,只是参数改成了String,
两者的使用就看大家的业务场景了,对笔者来说stringRedistemplate也够用了
5. RedisConfig配置类,相当于配置了Redistemplate、和StringRedistemplate
@Configuration public class RedisConfig{ @Autowired RedisConnectionFactory redisConnectionFactory; @Bean public Redistemplate<String, Object> functionDomainRedistemplate() { Redistemplate<String, Object> redistemplate = new Redistemplate<String, Object>(); initDomainRedistemplate(redistemplate, redisConnectionFactory); return redistemplate; } /* *设置redistemplate序列化策略,否则在使用redistemplate的时候在redis的客户端查看会出现乱码 */ private void initDomainRedistemplate(Redistemplate<String, Object> redistemplate, RedisConnectionFactory factory) { redistemplate.setKeySerializer(new StringRedisSerializer()); redistemplate.setHashKeySerializer(new StringRedisSerializer()); redistemplate.setHashValueSerializer(new JdkSerializationRedisSerializer()); redistemplate.setValueSerializer(new JdkSerializationRedisSerializer()); redistemplate.setConnectionFactory(factory); } }
6、到这一步就可以在代码上就使用redis了,下面简单演示StringRedistemplate方法使用,如设置过期时间600秒
//先引入StringRedistemplate @Autowired private StringRedistemplate stringRedistemplate; //向redis里存入数据和设置缓存时间 stringRedistemplate.opsForValue().set("redis", "100", 60 * 10, TimeUnit.SECONDS); //val做-1操作 stringRedistemplate.boundValueOps("redis").increment(-1); //根据key获取缓存中的val stringRedistemplate.opsForValue().get("redis") //val +1 stringRedistemplate.boundValueOps("redis").increment(1); //根据key获取过期时间 stringRedistemplate.getExpire("redis"); //根据key获取过期时间并换算成指定单位 stringRedistemplate.getExpire("redis",TimeUnit.SECONDS); //根据key删除缓存 stringRedistemplate.delete("redis"); //检查key是否存在,返回boolean值 stringRedistemplate.hasKey("redis"); //向指定key中存放set集合 stringRedistemplate.opsForSet().add("redis", "1","2","3"); //设置过期时间 stringRedistemplate.expire("redis",1000 , TimeUnit.MILLISECONDS); //根据key查看集合中是否存在指定数据 stringRedistemplate.opsForSet().isMember("redis", "1"); //根据key获取set集合 stringRedistemplate.opsForSet().members("redis"); //验证有效时间 Long expire = stringRedistemplate.boundHashOps("redis").getExpire(); System.out.println("redis有效时间:"+expire+"秒");
=====================================================================================
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。