pom文件中的引用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
加版本号需要注意适配
<!-- <version>3.1.0</version> 需要适配版本 -->
之前加了3.1.0的版本后 导致stringRedisTemplate 注入失败,自定义的 RedisTemplate 注入失败 ,删除了就正常了。备注:我使用的是jdk 8 ,springboot1.5.6 也不知道哪个出问题了,没有深入研究
stringRedisTemplate 关于五种数据的使用
String操作opsForValue,存储字符串
set操作opsForSet,不可重复数组
hash操作 key [{key val}]
list操作可用于实现消息队列,顺序结构
zset操作 key [{val score}] score从小到大排序
过期设置:
redisTemplate.expire(key1, 5000, TimeUnit.MILLISECONDS); 5秒后过期
redisTemplate.expireAt(key, date);//指定时间过期
参考:https://blog.csdn.net/sinat_22797429/article/details/89196933
数据库切换
JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
redisConnectionFactory.setHostName("127.0.0.1");
redisConnectionFactory.setPort(6379);
redisConnectionFactory.setDatabase(indexDb);
redisConnectionFactory.afterPropertiesSet();
redisTemplate.setConnectionFactory(redisConnectionFactory); 创建新连接 切换连接
自定义序列化方式 自动序列化
注入bean 配置 key value 的序列化方式 默认jdk实现JdkSerializationRedisSerializer 采用用ask 编码,序列化的 类需要需要实现Serializable接口
key 一般才用 String序列化 new StringRedisSerializer()
value
Jackson2JsonRedisSerializer序列化 //对String数组序列化 再反序列化时 缺少类型的描述
例:
User user = new User();
user.setId("1");
user.setUsername("zs");
user.setPassword("123");
RedisUtils.set("key",user);
value:{"@class": “com.carl.security.entity.User”, “id”: “1”, “username”: “zs”, “password”: “123”}
GenericFastJsonRedisSerializer序列化
value:{"@type": “com.carl.security.entity.User”, “id”: “1”, “username”: “zs”, “password”: “123”}
参考:https://blog.csdn.net/qq_53641150/article/details/124177358
自定义缓存读写机制 继承 CachingConfigurerSupport
重写 cacheManager
通用缓存时间setDefaultExpiration
个性化缓存时间 setExpires
根据传参不同进行缓存 缓存注解 https://blog.csdn.net/m0_67402774/article/details/126114968
批量操作
使用管道pipeline
两种方式
SessionCallback 实现 RedisOperations接口 RedisTemplate实现类 更友好
RedisCallback RedisConnection接口 是LettuceConnection是实现类 没有序列化部
返回的list 为空 是因为 复制方法过滤了非null返回值 例:operations.opsForValue().set(key, value, 20, TimeUnit.SECONDS)
使用 opsForValue().setIfAbsent 有返回值
参考 https://www.jb51.net/article/284285.htm
事务
可以理解为把操作存在管道中,未提交,来实现事务
方式一
//开启事务支持 使用redis原子机制做事务控制
redisTemplate.setEnableTransactionSupport(true);
//开启事务
redisTemplate.multi();
//回滚
redisTemplate.discard();
//执行事务
redisTemplate.exec();
方式二 在在使用SessionCallback中使用
redisTemplate.discard();
redisTemplate.exec();
参考 https://blog.csdn.net/Think_and_work/article/details/123219093
过期策略
定时删除 惰性删除(取到时才判断是否过期)
内存淘汰策略
设置Redis最大内存 在配置文件redis.conf 中
1)volatile-lru 利用LRU算法移除设置过过期时间的key (LRU:最近使用 Least Recently Used ) 。
2)allkeys-lru 利用LRU算法移除任何key (和上一个相比,删除的key包括设置过期时间和不设置过期时间的)。通常使用该方式。
3)volatile-random 移除设置过过期时间的随机key 。
4)allkeys-random 无差别的随机移除。
5)volatile-ttl 移除即将过期的key(minor TTL)
6)noeviction 不移除任何key,只是返回一个写错误 ,默认选项,一般不会选用。
参考:http://www.taodudu.cc/news/show-135191.html?action=onClick
集群部署
还未研究。。
相关问题
1.Redis--保证缓存与数据库的一致性 https://blog.csdn.net/feiying0canglang/article/details/120631538
先更新数据库、再删除缓存 (热点数据在做互斥锁读取)
2.分布式锁
通过缓存中的值来控制开关