使用命令介绍:
(1)SETNX
SETNX key val:当且仅当key不存在时,set一个key为val的字符串,返回1;若key存在,则什么都不做,返回0。
(2)GETSET
将给定 key 的值设为 value ,并返回 key 的旧值(old value)。
(3)delete
delete key:删除key
首先建立一个redis连接
resources目录下创建redis.properties
package com.dashenbuguohe.test.redis.impl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class RedisManager {
private static Logger logger = LoggerFactory.getLogger(RedisManager.class);
private static JedisPool jedisPool;
static {
InputStream stream = null;
try {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(500);
jedisPoolConfig.setMaxIdle(300);
jedisPoolConfig.setMinIdle(10);
jedisPoolConfig.setMaxWaitMillis(1000);
jedisPoolConfig.setTestWhileIdle(true);
jedisPoolConfig.setTestOnBorrow(true);
jedisPoolConfig.setTestOnReturn(true);
Properties properties = new Properties();
stream = RedisManager.class.getResourceAsStream("/redis.properties");
properties.load(stream);
String host = properties.getProperty("host");
int port = Integer.parseInt(properties.getProperty("port"));
int timeout = Integer.parseInt(properties.getProperty("timeout"));
String password = properties.getProperty("password");
int database = Integer.parseInt(properties.getProperty("database"));
jedisPool = new JedisPool(new JedisPoolConfig(), host, port, timeout, password, database);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stream != null) {
stream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static Jedis getResource() {
return jedisPool.getResource();
}
public static void closeResource(Jedis jedis) {
if (null != jedis) {
jedis.close();
}
}
}
锁服务
解决死锁 : 定义一个锁lock.foo
我们可以通过锁的键对应的时间戳来判断这种情况是否发生了,如果当前的时间已经大于lock.foo的值,说明该锁已失效,可以被重新使用。
当多个客户端检测到锁超时后都会尝试去释放它,这里就可能出现一个竞态条件,让我们模拟一下这个场景:
【场景一】
* C0操作超时了,但它还持有着锁,C1和C2读取lock.foo检查时间戳,先后发现超时了。
* C1 发送DEL lock.foo
* C1 发送SETNX lock.foo 并且成功了。
* C2 发送DEL lock.foo
* C2 发送SETNX lock.foo 并且成功了。
* 这样一来,C1,C2都拿到了锁!问题大了!发生这种情况时,可不能简单的通过DEL来删除锁,然后再SETNX一次。
为解决以上问题让我们模拟另一个场景场景:【场景二】
* C3发送SETNX lock.foo 想要获得锁,由于C0还持有锁,所以Redis返回给C3一个0
* C3发送watch 并GET lock.foo 以检查锁是否超时了,如果没超时,则等待或重试。
* 反之,如果已超时,C3通过下面的操作来尝试获得锁:
* multi加事务 GETSET lock.foo <current Unix time + lock timeout + 1>
* 通过GETSET,C3拿到的时间戳如果仍然是超时的,那就说明,C3如愿以偿拿到锁了。
* 如果在C3之前,有个叫C4的客户端比C3快一步执行了上面的操作,由于watch检测到lock.foo的变化所以会主动放弃锁
释放锁
定义一个锁lock.foo
C1 在释放lock.foo之前watch这个锁的变化
如果lock.foo发生变化(比如当前持锁线程C1超时,被其他线程C2拿到锁更改了过期时间),这时C1会主动放弃del lock.foo
package com.dashenbuguohe.test.lock.impl;
import com.dashenbuguohe.test.lock.DistributedLock;
import com.dashenbuguohe.test.redis.impl.RedisManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;
import java.util.List;
public class RedisLock {
private static Logger logger = LoggerFactory.getLogger(RedisLock.class);
private final static Integer DEFAULT_EXPIRE_TIME = 20;
private final static String DEFAULT_VALUE = "";
private final static String LOCK_PREFIX = "lock:";
public String lock(String key) throws Exception {
return lock(key, DEFAULT_EXPIRE_TIME);
}
/**
* 加锁
* @param key
* @param expireSeconds
* @return String 成功返回过期时间 失败返回空字符串
*/
public String lock(String key, Integer expireSeconds) throws Exception {
Jedis jedis = null;
if (expireSeconds == null || expireSeconds < 1) {
throw new Exception("expireSeconds can't be less than 1");
}
key = LOCK_PREFIX + key;
try {
jedis = RedisManager.getResource();
//加锁
String value = String.valueOf(System.currentTimeMillis() + expireSeconds * 1000);
Long setnx = jedis.setnx(key, value);
//加锁成功
if (setnx == 1) {
return value;
}
//锁被占用了 查看其过期时间
jedis.watch(key);
String time = jedis.get(key);
if (System.currentTimeMillis() > Long.valueOf(time)) {
Transaction transaction = jedis.multi();
transaction.getSet(key, value);
List<Object> exec = transaction.exec();
jedis.unwatch();
if (exec != null && exec.size() != 0
&& exec.get(0) != null && time.equals(exec.get(0).toString())) {
return value;
}
}
return DEFAULT_VALUE;
} finally {
RedisManager.closeResource(jedis);
}
}
/**
* 释放锁
* @param key
* @param value 获取锁时所返回的值(过期时间)
* @return
*/
public boolean releaseLock(String key, String value) throws Exception {
Jedis jedis = null;
key = LOCK_PREFIX + key;
try {
jedis = RedisManager.getResource();
jedis.watch(key);
Transaction transaction = jedis.multi();
if (Long.valueOf(value) > System.currentTimeMillis()) {
transaction.del(key);
transaction.exec();
} else {
transaction.discard();
}
jedis.unwatch();
return true;
} finally {
RedisManager.closeResource(jedis);
}
}
}