博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Redis 分布式锁
阅读量:4039 次
发布时间:2019-05-24

本文共 2567 字,大约阅读时间需要 8 分钟。

最近项目又用到分布式锁,下面把自己写的redis分布式锁代码贡献出来。


public class RedisManager {    private static final String SET_IF_NOT_EXIST = "NX";//仅在键不存在时设置键。    private static final String SET_WITH_EXPIRE_TIME = "EX";//PX表示超时时间是毫秒设置,EX表示超时时间是为 second 秒    private static final Long RELEASE_SUCCESS = 1L;    /**     * 单线程独享变量     **/    private ThreadLocal
threadLocal = new ThreadLocal<>(); @Autowired private MergeJedisCmd jedisCmd; /*** * @param key 键 * @return string * */ public String get(String key) { return jedisCmd.get(key); } /*** * @param key 键 * @param value 值 * @param expiredTime 秒 * @return true false * 将 key 的值设为 value ,当且仅当 key 不存在。 * 若给定的 key 已经存在,则 SETNX 不做任何动作。 * */ public boolean set(String key, int expiredTime, String value) { String result = jedisCmd.setex(key, expiredTime, value); return result == null ? false : true; } /*** * @param key 键 * @param value 值 * @return true false * 将 key 的值设为 value ,当且仅当 key 不存在。 * 若给定的 key 已经存在,则 SETNX 不做任何动作。 * */ public boolean set(String key, String value) { return jedisCmd.setnx(key, value) > 0; } /** * 尝试获取分布式锁 * * @param lockKey 锁 * @param expireTime 超期时间 300(秒) * @return 是否获取成功 */ public boolean lock(String lockKey, long expireTime) { String uuid = UUID.randomUUID().toString(); String value = jedisCmd.set(lockKey, uuid, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, expireTime); if (value != null && value.equals("OK")) { threadLocal.set(uuid); return true; } return false; } /** * 释放分布式锁 * git * * @param lockKey 锁 * @return 是否释放成功 */ public boolean unLock(String lockKey) { String key = jedisCmd.get(lockKey); //可能会存在锁失效的时候 if (key == null) { return true; } //保持原子性 (Get —>判断 -> del )操作 String script = "if redis.call('get',KEYS[1]) == ARGV[1] then return redis.call('del',KEYS[1]) else return 0 end "; Object object = jedisCmd.eval(script, Collections.singletonList(lockKey), Collections.singletonList(threadLocal.get())); if (RELEASE_SUCCESS.equals(object)) { return true; } return false; }

由于MergeJedisCmd 是自己项目中封装了,所以大家可以用redis 客户端的原类。

1.MergeJedisCmd extends JedisCmd, BinaryJedisCmd

 2.JedisCmd extends JedisCommands, MultiKeyCommands, ScriptingCommands

3.BinaryJedisCmd extends BinaryJedisCommands, MultiKeyBinaryCommands, BinaryScriptingCommands

 

转载地址:http://wujdi.baihongyu.com/

你可能感兴趣的文章
Django 的Error: [Errno 10013]错误
查看>>
机器学习实战之决策树(一)
查看>>
机器学习实战之决策树二
查看>>
[LeetCode By Python]7 Reverse Integer
查看>>
[leetCode By Python] 14. Longest Common Prefix
查看>>
[leetCode By Python]111. Minimum Depth of Binary Tree
查看>>
[LeetCode By Python]118. Pascal's Triangle
查看>>
[LeetCode By Python]121. Best Time to Buy and Sell Stock
查看>>
[LeetCode By Python]122. Best Time to Buy and Sell Stock II
查看>>
[LeetCode By Python]125. Valid Palindrome
查看>>
[LeetCode By Python]136. Single Number
查看>>
[LeetCode By MYSQL] Combine Two Tables
查看>>
如何打开ipynb文件
查看>>
[Leetcode BY python ]190. Reverse Bits
查看>>
Android下调用收发短信邮件等(转载)
查看>>
Android中电池信息(Battery information)的取得
查看>>
SVN客户端命令详解
查看>>
Android/Linux 内存监视
查看>>
Linux系统信息查看
查看>>
用find命令查找最近修改过的文件
查看>>