欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > 基于Redis提高查询性能(保持数据一致性)

基于Redis提高查询性能(保持数据一致性)

2024/10/24 4:36:56 来源:https://blog.csdn.net/m0_74031434/article/details/139783747  浏览:    关键词:基于Redis提高查询性能(保持数据一致性)

Redis实战篇 | Kyle's Blog (cyborg2077.github.io)

目录

背景

商户查询缓存(根据ID查询)

 根据店铺类型查询(List型)

缓存更新策略(保证数据一致性)

案例(利用缓存更新策略)


背景

起初客户端直接向数据库查询数据                              添加redis后 

商户查询缓存(根据ID查询)

 实现业务流程

 ShopController

    @GetMapping("/{id}")public Result queryShopById(@PathVariable("id") Long id) {return shopService.queryById(id);}

IShopService 

public interface IShopService extends IService<Shop> {Result queryById(Long id);
}

ShopServiceImpl 

@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements IShopService {@Resourcepublic StringRedisTemplate stringRedisTemplate;@Overridepublic Result queryById(Long id) {String key = CACHE_SHOP_KEY + id;// 1.从redis查询商铺缓存String shopJson = stringRedisTemplate.opsForValue().get(key);// 2.判断缓存是否命中if(StrUtil.isNotBlank(shopJson)){// 3.缓存命中,返回// 将json反序列化为对象Shop shop = JSONUtil.toBean(shopJson, Shop.class);return Result.ok(shop);}// 4.缓存未命中,根据id查询数据库Shop shop = getById(id);// 5.数据库不存在,返回错误if(shop == null){return Result.fail("店铺不存在");}// 6.数据库存在,写入缓存stringRedisTemplate.opsForValue().set(key,JSONUtil.toJsonStr(shop),30L, TimeUnit.MINUTES);// 7.返回return Result.ok(shop);}
}

 根据店铺类型查询(List型)

Controller 

@RestController
@RequestMapping("/shop-type")
public class ShopTypeController {@Resourceprivate IShopTypeService typeService;@GetMapping("list")public Result queryTypeList() {return typeService.queryList();}
}

接口 

public interface IShopTypeService extends IService<ShopType> {Result queryList();
}

实现类

@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {@Resourcepublic StringRedisTemplate stringRedisTemplate;@Overridepublic Result queryList() {// 1.从redis查询商铺类型List<String> shopTypes = stringRedisTemplate.opsForList().range(CACHE_SHOP_KEY, 0, -1);// 2.判断redis是否命中if(!shopTypes.isEmpty()){//如果命中则转为ShopType类型返回List<ShopType> tmp = new ArrayList<>();for (String types : shopTypes) {ShopType shopType = JSONUtil.toBean(types, ShopType.class);tmp.add(shopType);}return Result.ok(tmp);}//没有命中则查询数据库List<ShopType> tmp = query().orderByAsc("sort").list();if(tmp.isEmpty()){return Result.fail("店铺类型不存在");}//3.将数据写入redisshopTypes.forEach(shopType -> {stringRedisTemplate.opsForList().rightPush(CACHE_SHOP_KEY,JSONUtil.toJsonStr(shopType));});//4.返回return Result.ok(tmp);}
}

缓存更新策略(保证数据一致性)

 三种更新缓存的策略(保证数据一致性)

我们主要使用主动更新策略

又因为操作数据库和缓存的先后顺序,线程问题;因此需要先修改数据库,再删除缓存,加锁,使用事务。

案例(利用缓存更新策略)

根据id查询店铺时,如果缓存未命中,则查询数据库,将数据库结果写入缓存,并设置超时时间 

// 6.数据库存在,写入缓存
stringRedisTemplate.opsForValue().set(key,JSONUtil.toJsonStr(shop), CACHE_SHOP_TTL , TimeUnit.MINUTES);

根据id修改店铺时,先修改数据库,再删除缓存

    @Override@Transactionalpublic Result update(Shop shop) {Long id = shop.getId();if(id == null){return Result.fail("店铺id不能为空");}// 1.更新数据库updateById(shop);// 2.删除缓存stringRedisTemplate.delete(CACHE_SHOP_KEY + id);return Result.ok();}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com