redis的使用场景-热点数据缓存(把经常访问的数据放入缓存减少数据库压力)

一、使用redis实现(不推荐,会增加业务代码维护)

java 复制代码
@Service
public class ClazzServiceImpl implements ClazzService {

 @Autowired
 private ClazzDao clazzDao;	//注入mapper
 @Autowired
 private RedisTemplate<String, Object> redisTemplate;	//调用redis使用
 @Override
 public Clazz getById(Integer id) {	//查询业务
     //查询redis中是否存有缓存
     ValueOperations<String, Object> forValue = redisTemplate.opsForValue();
     Object o = forValue.get("clazz::" + id);
     //如果有直接将redis查询的数据返回
     if(o!=null){
         return (Clazz) o;
     }
     //如果没有查询数据库并将数据库数据保存到redis缓存中
     Clazz clazz = clazzDao.selectById(id);
     if(clazz!=null){
         forValue.set("clazz::" + id,clazz);
     }
     return clazz;
 }

 @Override
 public Clazz update(Clazz clazz) {	//修改业务
     int i = clazzDao.updateById(clazz);
     if(i>0){
         //修改redis缓存的内容为修改后的数据
         redisTemplate.opsForValue().set("clazz::"+clazz.getCid(),clazz);
     }
     return clazz;
 }

 @Override
 public int delete(Integer cid) {	//删除业务
     int i = clazzDao.deleteById(cid);
     if(i>0){
         //删除redis缓存
         redisTemplate.delete("clazz::"+cid);
     }
     return i;
 }
}

二、使用缓存注解完成功能

编写配置类并在启动类中使用@EnableCaching注解

java 复制代码
@Bean
 public CacheManager cacheManager(RedisConnectionFactory factory) {
     RedisSerializer<String> redisSerializer = new StringRedisSerializer();
     Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
     //解决查询缓存转换异常的问题
     ObjectMapper om = new ObjectMapper();
     om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
     om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
     jackson2JsonRedisSerializer.setObjectMapper(om);
     // 配置序列化(解决乱码的问题),过期时间600秒
     RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
             .entryTtl(Duration.ofSeconds(600)) //缓存过期10分钟 ---- 业务需求。
             .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))//设置key的序列化方式
             .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) //设置value的序列化
             .disableCachingNullValues();
     RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
             .cacheDefaults(config)
             .build();
     return cacheManager;
 }

使用

java 复制代码
@Service
public class ClazzServiceImpl implements ClazzService {

 @Autowired
 private ClazzDao clazzDao;
 @Autowired
 private RedisTemplate<String, Object> redisTemplate;


 //Cacheable:表示查询时使用的注解	等价于:redis的clazz::id
 @Cacheable(cacheNames ={ "clazz"}, key = "#id")
 @Override
 public Clazz getById(Integer id) {
     //查询数据库
     Clazz clazz = clazzDao.selectById(id);
     return clazz;
 }

 @Override
 public Clazz save(Clazz clazz) {
     int insert = clazzDao.insert(clazz);
     return clazz;
 }

 //CachePut:表示修改时使用的注解	等价于:redis的clazz::id
 @CachePut(cacheNames = "clazz", key = "#clazz.cid")
 public Clazz update(Clazz clazz) {
     //修改数据库
     int i = clazzDao.updateById(clazz);
     return clazz;
 }

 //CacheEvict:表示删除时使用的注解	等价于:redis的clazz::id
 @CacheEvict(cacheNames = "clazz", key = "#cid")
 @Override
 public int delete(Integer cid) {
     int i = clazzDao.deleteById(cid);
     return i;
 }
}
相关推荐
jllllyuz1 小时前
matlab实现蚁群算法解决公交车路径规划问题
服务器·前端·数据库
下雨天u2 小时前
maven dependencyManagement标签作用
java·数据库·maven
代码配咖啡2 小时前
国产数据库工具突围:SQLynx如何解决Navicat的三大痛点?深度体验报告
数据库
清酒伴风(面试准备中......)2 小时前
小白学编程之——数据库如何性能优化
数据库·oracle·性能优化
The Future is mine2 小时前
SQL Server中delete table和truncate table删除全表数据哪个快?
数据库
瀚高PG实验室3 小时前
HGDB插入超长字段报错指示列名的问题处理
数据库
爱刘温柔的小猪3 小时前
Redis+Caffeine构造多级缓存
redis·spring·缓存
hello1114-3 小时前
Redis学习打卡-Day2-缓存更新策略、主动更新策略、缓存穿透、缓存雪崩、缓存击穿
java·redis·学习·缓存·javaweb
好吃的肘子3 小时前
MongoDB 高可用复制集架构
数据库·mongodb·架构
兮兮能吃能睡3 小时前
Python之with语句
数据库·python