• 分布式锁-redis


    @Autowired
        private RedisTemplate<String, Object> redisTemplate;
    
    
    /**
         * 获取分布式锁 - 过期时间
         * @param key
         * @param expireTime 毫秒
         * @return
         */
        public Object getExpiredLock(String key, Long expireTime) {
            Long value = System.currentTimeMillis() + expireTime;
            return redisTemplate.execute((RedisCallback) connection -> {
                int count = 3;
                while (count > 0) {
                    try {
                        JedisCommands commands = (JedisCommands) connection.getNativeConnection();
                        return commands.set(key, String.valueOf(value), "NX", "PX", expireTime);
                    } catch (Exception e) {
                        logger.error("getExpiredLock exception key={}, count={}:", key, count, e);
                        count--;
                    }
                }
                return null;
            });
        }
    
    
    /**
         * 获取分布式锁 - 过期时间,value
         * @param key
         * @param expireTime
         * @return
         */
        public Object getExpiredLock(String key, Long expireTime, String value) {
            return redisTemplate.execute((RedisCallback) connection -> {
                int count = 3;
                while (count > 0) {
                    try {
                        JedisCommands commands = (JedisCommands) connection.getNativeConnection();
                        return commands.set(key, value, "NX", "PX", expireTime);
                    } catch (Exception e) {
                        logger.error("getExpiredLock exception key={}, count={}:", key, count, e);
                        count--;
                    }
                }
                return null;
            });
        }
    
    /**
         * 获取分布式锁value
         * @param key
         * @return
         */
        public Object getValue4ExpiredLock(String key) {
            return redisTemplate.execute((RedisCallback) connection -> {
                int count = 3;
                while (count > 0) {
                    try {
                        JedisCommands commands = (JedisCommands) connection.getNativeConnection();
                        return commands.get(key);
                    } catch (Exception e) {
                        logger.error("getExpiredLock exception key={}, count={}:", key, count, e);
                        count--;
                    }
                }
                return null;
            });
        }
    
    
    /**
         * 删除任务队列锁
         * @param lockKey
         */
        public void deleteLock(String lockKey) {
            int count = 3;
            while (count > 0) {
                try {
                    if (redisTemplate.hasKey(lockKey) && Long.valueOf(""+getValue4ExpiredLock(lockKey)) > System.currentTimeMillis()) {
                        // 当前时间比该锁对应的value小,说明未过期,并占有该锁,可以直接删除
                        redisTemplate.delete(lockKey);
                        logger.info("删除list锁:{}", lockKey);
                    } else {
                        logger.info("任务锁已过期, taskListLockKey={}", lockKey);
                    }
                    break;
                } catch (Exception e) {
                    logger.error("deleteLock count={}", count, e);
                    count--;
                }
            }
    
        }
    

      

            // 获取分布式锁
            if (redisService.getExpiredLock(key, 5000L) == null) {
                return;
            }    
    
    
            // 删除分布式锁
             redisService.deleteLock(key);    
    

      

  • 相关阅读:
    课时28:文件:因为懂你,所以永恒
    课时27:集合:在我的世界里,你就是唯一
    课时26:字典:各种内置方法
    课时25:字典:当索引不好用时
    课时24:递归:汉诺塔
    课时23:递归:这帮小兔崽子
    课时22:函数:递归是神马
    有序表查找-折半查找
    C#
    C#
  • 原文地址:https://www.cnblogs.com/wanhua-wu/p/11929297.html
Copyright © 2020-2023  润新知