php redis锁工具类,redis分布式锁工具类

[TOC]

e51ecf63cec21be7f122ff867937dc41.png

(1)需要导入的包

redis.clients

jedis

2.8.0

##(2)JedisUtil类

import redis.clients.jedis.Jedis;

import redis.clients.jedis.JedisPool;

import java.util.Collections;

/**

* 不采用springTemplate 的操作类

* 因为springTemplate 的SetNx 非原子性,可能导致锁永久锁住,释放失败

*/

@Component("jedisUtil")

public class JedisUtil {

private static final String LOCK_SUCCESS = "OK";

private static final String SET_IF_NOT_EXIST = "NX";

private static final String SET_WITH_EXPIRE_TIME = "PX";

private static final Long RELEASE_SUCCESS = 1L;

@Autowired

private JedisPool jedisPool;

/**

* 尝试获取分布式锁

* @param lockKey 锁

* @param requestId 请求标识

* @param expireTime 超期时间

* @return 是否获取成功

*/

public boolean tryGetDistributedLock(String lockKey, String requestId, int expireTime) {

Jedis jedis = null;

try{

jedis = jedisPool.getResource();

String result = jedis.set(lockKey, requestId, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, expireTime);

if (LOCK_SUCCESS.equals(result)) {

return true;

}

} finally {

//归还 jedis 连接

if(jedis != null){

jedis.close();

}

}

return false;

}

/**

* 释放分布式锁

* @param lockKey 锁

* @param requestId 请求标识

* @return 是否释放成功

*/

public boolean releaseDistributedLock(String lockKey, String requestId) {

Jedis jedis = null;

try {

jedis = jedisPool.getResource();

String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";

Object result = jedis.eval(script, Collections.singletonList(lockKey), Collections.singletonList(requestId));

if (RELEASE_SUCCESS.equals(result)) {

return true;

}

} finally {

//归还 jedis 连接

if(jedis != null){

jedis.close();

}

}

return false;

}

/**

* 设置值并设置超时时间

* @param key

* @param value

* @param expireTime

* @return

*/

public Long rpushString(String key, String value, int expireTime){

Jedis jedis = null;

Long result = 0L;

try {

jedis = jedisPool.getResource();

result = jedis.rpush(key, value);

if(result > 0){

jedis.expire(key,expireTime);

}

} finally {

if(jedis != null){

jedis.close();

}

}

return result;

}

/**

* 批量插入

* @param key

* @param values

* @param expireTime

* @return

*/

public Long batchRpushString(String key, String[] values, int expireTime){

Jedis jedis = null;

Long result = 0L;

try {

jedis = jedisPool.getResource();

result = jedis.rpush(key, values);

if(result > 0){

jedis.expire(key,expireTime);

}

} finally {

if(jedis != null){

jedis.close();

}

}

return result;

}

/**

* 释放锁

* @param lockKey

*/

public Long releaseLock(String lockKey){

Long result = 0L;

Jedis jedis = null;

try {

jedis = jedisPool.getResource();

result = jedis.del(lockKey);

} finally {

if(jedis != null){

jedis.close();

}

}

return result;

}

}

(3)jedisPool配置

可以根据自己情况换成pringboot类配置

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

(4)使用举例

@Resource

private JedisUtil jedisUtil;

String lockKey=userId;//锁key

String requestId = UUID.randomUUID().toString();//请求标识

final int EXPIRED_TIME = 300*1000;//redis 数据存储过期时间

//加锁举例

boolean lockResult = jedisUtil.tryGetDistributedLock(lockKey, requestId, EXPIRED_TIME)

//放锁举例

booleanr releaseResult =jedisUtil.releaseDistributedLock(lockKey, requestId);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值