当前位置: 首页 > news >正文

缓存-分布式锁-原理和基本使用

分布式锁原理和使用

 

自旋 

 public Map<String, List<Catelog2Vo>> getCatalogJsonFromDBWithRedisLock() {Boolean b = redisTemplate.opsForValue().setIfAbsent(Lock, Lock, Duration.ofMinutes(1));if (!b) {int i = 10;while (i > 0) {Object result = redisTemplate.opsForValue().get(CATALOG_JSON);try {TimeUnit.MILLISECONDS.sleep(100);} catch (InterruptedException e) {throw new RuntimeException(e);}if (result != null) {System.out.println("命中缓存 db lock");return (Map<String, List<Catelog2Vo>>) result;}i--;}throw new RuntimeException("系统繁忙,请重新访问");}//1.查出所有1级分类List<CategoryEntity> selectList = baseMapper.selectList(null);/*** 将数据库的多次查询变成一次*/System.out.println("查询了数据库");//2. 封装数据List<CategoryEntity> level1Category = selectList.stream().filter(s -> s.getParentCid().equals(0L)).collect(Collectors.toList());Map<String, List<Catelog2Vo>> map = level1Category.stream().collect(Collectors.toMap(k -> k.getCatId().toString(), v -> {//1.每一个的一级分类,查到1级分类的所有二级分类List<CategoryEntity> categoryEntities = selectList.stream().filter(s -> s.getParentCid().equals(v.getCatId())).collect(Collectors.toList());List<Catelog2Vo> catelog2VoList = categoryEntities.stream().map(c -> {Catelog2Vo catelog2Vo = new Catelog2Vo();catelog2Vo.setId(c.getCatId().toString());catelog2Vo.setName(c.getName());catelog2Vo.setCatalog1Id(v.getCatId().toString());List<CategoryEntity> categoryEntities1 = selectList.stream().filter(s -> s.getParentCid().equals(c.getCatId())).collect(Collectors.toList());List<Catelog2Vo.Catelog3Vo> collect = categoryEntities1.stream().map(c3 -> {Catelog2Vo.Catelog3Vo catelog3Vo = new Catelog2Vo.Catelog3Vo();catelog3Vo.setId(c3.getCatId().toString());catelog3Vo.setName(c3.getName());catelog3Vo.setCatalog2Id(c.getCatId().toString());return catelog3Vo;}).collect(Collectors.toList());catelog2Vo.setCatalog3List(collect);return catelog2Vo;}).collect(Collectors.toList());return catelog2VoList;}));if (map == null) {/*** 解决缓存穿透*/map = new HashMap<>();}redisTemplate.opsForValue().set(CATALOG_JSON, map, Duration.ofDays(1));redisTemplate.delete(Lock);return map;}

 public Map<String, List<Catelog2Vo>> getCatalogJsonFromDBWithRedisLock() {String uuid = UUID.randomUUID().toString();Boolean b = redisTemplate.opsForValue().setIfAbsent(Lock, uuid, Duration.ofMinutes(1));if (!b) {int i = 10;while (i > 0) {Object result = redisTemplate.opsForValue().get(CATALOG_JSON);try {TimeUnit.MILLISECONDS.sleep(100);} catch (InterruptedException e) {throw new RuntimeException(e);}if (result != null) {System.out.println("命中缓存 db lock");return (Map<String, List<Catelog2Vo>>) result;}i--;}throw new RuntimeException("系统繁忙,请重新访问");}//1.查出所有1级分类List<CategoryEntity> selectList = baseMapper.selectList(null);/*** 将数据库的多次查询变成一次*/System.out.println("查询了数据库");//2. 封装数据List<CategoryEntity> level1Category = selectList.stream().filter(s -> s.getParentCid().equals(0L)).collect(Collectors.toList());Map<String, List<Catelog2Vo>> map = level1Category.stream().collect(Collectors.toMap(k -> k.getCatId().toString(), v -> {//1.每一个的一级分类,查到1级分类的所有二级分类List<CategoryEntity> categoryEntities = selectList.stream().filter(s -> s.getParentCid().equals(v.getCatId())).collect(Collectors.toList());List<Catelog2Vo> catelog2VoList = categoryEntities.stream().map(c -> {Catelog2Vo catelog2Vo = new Catelog2Vo();catelog2Vo.setId(c.getCatId().toString());catelog2Vo.setName(c.getName());catelog2Vo.setCatalog1Id(v.getCatId().toString());List<CategoryEntity> categoryEntities1 = selectList.stream().filter(s -> s.getParentCid().equals(c.getCatId())).collect(Collectors.toList());List<Catelog2Vo.Catelog3Vo> collect = categoryEntities1.stream().map(c3 -> {Catelog2Vo.Catelog3Vo catelog3Vo = new Catelog2Vo.Catelog3Vo();catelog3Vo.setId(c3.getCatId().toString());catelog3Vo.setName(c3.getName());catelog3Vo.setCatalog2Id(c.getCatId().toString());return catelog3Vo;}).collect(Collectors.toList());catelog2Vo.setCatalog3List(collect);return catelog2Vo;}).collect(Collectors.toList());return catelog2VoList;}));if (map == null) {/*** 解决缓存穿透*/map = new HashMap<>();}redisTemplate.opsForValue().set(CATALOG_JSON, map, Duration.ofDays(1));Object o = redisTemplate.opsForValue().get(Lock);if (o != null && o.equals(uuid)) {redisTemplate.delete(Lock);}return map;}

还是有问题

因为 在传输过程中需要耗时,这时候如果过期KEY,让其他线程进来创建KEY,然后数据返回到之前那个线程,删除KEY,又会把别人新加进来的key给删掉

获取值对比+对比成功删除=原子操作

 redis+lua脚本实现

 public static final String Lock = "Lock";
  public Map<String, List<Catelog2Vo>> getCatalogJsonFromDBWithRedisLock() {String uuid = UUID.randomUUID().toString();Boolean b = redisTemplate.opsForValue().setIfAbsent(Lock, uuid, Duration.ofMinutes(5));if (!b) {System.out.println("获取分布式锁失败,等待重试");int i = 10;while (i > 0) {Object result = redisTemplate.opsForValue().get(CATALOG_JSON);try {TimeUnit.MILLISECONDS.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}if (result != null) {System.out.println("命中缓存 db lock");return (Map<String, List<Catelog2Vo>>) result;}i--;}throw new RuntimeException("系统繁忙,请重新访问");}//1.查出所有1级分类/*** 将数据库的多次查询变成一次*/System.out.println("获取分布式锁成功");//2. 封装数据Map<String, List<Catelog2Vo>> map = null;try {System.out.println("查询了数据库");List<CategoryEntity> selectList = baseMapper.selectList(null);List<CategoryEntity> level1Category = selectList.stream().filter(s -> s.getParentCid().equals(0L)).collect(Collectors.toList());map = level1Category.stream().collect(Collectors.toMap(k -> k.getCatId().toString(), v -> {//1.每一个的一级分类,查到1级分类的所有二级分类List<CategoryEntity> categoryEntities = selectList.stream().filter(s -> s.getParentCid().equals(v.getCatId())).collect(Collectors.toList());List<Catelog2Vo> catelog2VoList = categoryEntities.stream().map(c -> {Catelog2Vo catelog2Vo = new Catelog2Vo();catelog2Vo.setId(c.getCatId().toString());catelog2Vo.setName(c.getName());catelog2Vo.setCatalog1Id(v.getCatId().toString());List<CategoryEntity> categoryEntities1 = selectList.stream().filter(s -> s.getParentCid().equals(c.getCatId())).collect(Collectors.toList());List<Catelog2Vo.Catelog3Vo> collect = categoryEntities1.stream().map(c3 -> {Catelog2Vo.Catelog3Vo catelog3Vo = new Catelog2Vo.Catelog3Vo();catelog3Vo.setId(c3.getCatId().toString());catelog3Vo.setName(c3.getName());catelog3Vo.setCatalog2Id(c.getCatId().toString());return catelog3Vo;}).collect(Collectors.toList());catelog2Vo.setCatalog3List(collect);return catelog2Vo;}).collect(Collectors.toList());return catelog2VoList;}));if (map == null) {/*** 解决缓存穿透*/map = new HashMap<>();}redisTemplate.opsForValue().set(CATALOG_JSON, map, Duration.ofDays(1));} catch (Exception e) {e.printStackTrace();} finally {//lua脚本解锁//如果获取key等于传过来的值,就执行删除操作,否则就不执行String script="if redis.call('get',KEYS[1])==ARGV[1] then return redis.call('del',KEYS[1]) else return 0 end";Long execute = redisTemplate.execute(new DefaultRedisScript<Long>(script, Long.class), Arrays.asList(Lock), uuid);if (execute==1){System.out.println("原子删锁成功");}else {System.out.println("原子删锁失败");}}return map;}

 只有一个查询了数据库

http://www.lryc.cn/news/394380.html

相关文章:

  • 判断国内ip
  • linux修改内核实现禁止被ping(随手记)
  • mac M1安装 VSCode
  • 代码随想录算法训练营第二十七天 |56. 合并区间 738.单调递增的数字 968.监控二叉树 (可跳过)
  • 网络基础:IS-IS协议
  • Java面试八股之如何提高MySQL的insert性能
  • 【密码学】什么是密码?什么是密码学?
  • k8s record 20240703
  • Ansible常用模块
  • 【JavaScript脚本宇宙】提升用户体验:探索 JavaScript 库中的浏览器特性支持检测
  • 深度学习:C++和Python如何对大图进行小目标检测
  • Eureka从入门到精通面试题及答案参考
  • io流 多线程
  • 人工智能、机器学习、神经网络、深度学习和卷积神经网络的概念和关系
  • 对话大模型Prompt是否需要礼貌点?
  • 【驱动篇】龙芯LS2K0300之ADC驱动
  • Python入门 2024/7/3
  • Go 语言 Map(集合)
  • SpringCloud学习Day7:Seata
  • 【ubuntu中关于驱动得问题】—— 如何将nouveau驱动程序加入黑名单和安装NVIDIA显卡驱动
  • LabVIEW从测试曲线中提取特征值
  • 【应届应知应会】SQL常用知识点50道
  • 【雷丰阳-谷粒商城 】【分布式高级篇-微服务架构篇】【21】【购物车】
  • 科技赋能智慧应急:“数字孪生+无人机”在防汛救灾中的应用
  • urfread刷算法|构建一棵树
  • 在卷积神经网络(CNN)中为什么可以使用多个较小的卷积核替代一个较大的卷积核,以达到相同的感受野
  • 【学习笔记】Mybatis-Plus(四):MP中内置的插件
  • GlusterFS分布式存储系统
  • 微信公众平台测试账号本地微信功能测试说明
  • Lua语言入门