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;
 }
}
相关推荐
倔强的石头_13 小时前
KingbaseES 新版MySQL 兼容版体验:旧版迁移 + 功能实测
数据库
用户3169353811833 天前
Java连接Redis
redis
倔强的石头_3 天前
《Kingbase护城河》——数据库存储空间全景探测与精细化瘦身实战
数据库
冬奇Lab4 天前
每日一个开源项目(第134篇):Zvec - 阿里开源的嵌入式向量数据库,向量搜索界的 SQLite
数据库·人工智能·llm
ClouGence4 天前
Oracle CDC 架构优化:从主库直连到 DataGuard 备库同步
数据库·后端·oracle
无响应de神4 天前
三、用户与权限管理
数据库·mysql
小小工匠5 天前
Redis - 事务机制:能实现 ACID 属性吗
数据结构·redis·性能优化·并发·持久化
麦聪聊数据5 天前
数据服务化时代:企业数据能力输出的核心路径
数据库
shushangyun_5 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
DARLING Zero two♡5 天前
【MySQL数据库】数据类型与表约束
数据库·mysql