package com.qxueyou.scc.base.dao;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Map;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import com.qxueyou.scc.base.util.ReisUtils;
|
|
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.ShardedJedis;
|
|
public abstract class AbstractRedisTemplate {
|
// lock script
|
private static String SET_NX_SCRIPT = "local key = KEYS[1] local ttl = KEYS[2] "
|
+ "local content = KEYS[3] local lockSet = redis.call('setnx', key, content) "
|
+ "if lockSet == 1 then redis.call('expire', key, ttl) end return lockSet ";
|
|
protected abstract ShardedJedis getJedisClient();
|
|
protected abstract void closeJedisClient(ShardedJedis jedisClient);
|
|
public void set(String key, String value) {
|
ShardedJedis client = null;
|
try {
|
client = this.getJedisClient();
|
this.getJedisClient().set(key, value);
|
} finally {
|
this.closeJedisClient(client);
|
}
|
}
|
|
/**
|
* 设置过期时间的set方法
|
*
|
* @param key
|
* 键
|
* @param value
|
* value
|
* @param timeout
|
* 过期时间 单位(s)
|
*/
|
public void setex(String key, String value, int timeout) {
|
ShardedJedis client = null;
|
try {
|
client = this.getJedisClient();
|
client.setex(key, timeout, value);
|
} finally {
|
this.closeJedisClient(client);
|
}
|
}
|
|
public boolean exists(String key) {
|
ShardedJedis client = null;
|
boolean result = false;
|
try {
|
client = this.getJedisClient();
|
result = client.exists(key);
|
} finally {
|
this.closeJedisClient(client);
|
}
|
return result;
|
}
|
|
public boolean expire(String key, int secondTimeOut) {
|
ShardedJedis client = null;
|
Long ex = 0L;
|
try {
|
client = this.getJedisClient();
|
ex = client.expire(key, secondTimeOut);
|
} finally {
|
this.closeJedisClient(client);
|
}
|
return ex.intValue() == 1;
|
}
|
|
public boolean expireAt(String key, long expireAt) {
|
int timeOut = (int)(expireAt - System.currentTimeMillis()) / 1000;
|
return expire(key, timeOut);
|
}
|
|
/** 根据键获取字符串值 */
|
public String get(String key) {
|
ShardedJedis client = null;
|
String value = "";
|
try {
|
client = this.getJedisClient();
|
value = client.get(key);
|
} finally {
|
this.closeJedisClient(client);
|
}
|
return value;
|
}
|
|
/** 获取并删除键 */
|
public String getAndDel(String key) {
|
ShardedJedis client = null;
|
String value = "";
|
try {
|
client = this.getJedisClient();
|
value = client.get(key);
|
client.del(key);
|
} finally {
|
this.closeJedisClient(client);
|
}
|
return value;
|
}
|
|
/** 删除键 */
|
public void del(String key) {
|
ShardedJedis client = null;
|
try {
|
client = this.getJedisClient();
|
client.del(key);
|
} finally {
|
this.closeJedisClient(client);
|
}
|
}
|
|
/** 获取并删除键 */
|
public void del(String[] keys) {
|
ShardedJedis client = null;
|
try {
|
client = this.getJedisClient();
|
for (String strKey : keys) {
|
if (StringUtils.isNotEmpty(strKey)) {
|
client.del(strKey.getBytes());
|
}
|
}
|
} finally {
|
this.closeJedisClient(client);
|
}
|
}
|
|
public long incr(String key) {
|
ShardedJedis client = null;
|
try {
|
client = this.getJedisClient();
|
return client.incr(key);
|
} finally {
|
this.closeJedisClient(client);
|
}
|
}
|
|
public long decr(String key) {
|
ShardedJedis client = null;
|
try {
|
client = this.getJedisClient();
|
return client.decr(key);
|
} finally {
|
this.closeJedisClient(client);
|
}
|
}
|
|
public <T> void setList(String key, List<T> lst) {
|
ShardedJedis client = null;
|
try {
|
client = this.getJedisClient();
|
client.set(key.getBytes(), ReisUtils.serialize(lst));
|
} finally {
|
this.closeJedisClient(client);
|
}
|
}
|
|
@SuppressWarnings("unchecked")
|
public <T> List<T> getList(String key) {
|
ShardedJedis client = null;
|
byte[] bytesArr = null;
|
List<T> result = null;
|
try {
|
client = this.getJedisClient();
|
bytesArr = client.get(key.getBytes());
|
result = (List<T>) ReisUtils.unserialize(bytesArr);
|
} finally {
|
this.closeJedisClient(client);
|
}
|
return result;
|
}
|
|
public List<String> getStrList(String key) {
|
ShardedJedis client = null;
|
List<String> result = null;
|
try {
|
client = this.getJedisClient();
|
result = client.lrange(key, 0, client.llen(key));
|
} finally {
|
this.closeJedisClient(client);
|
}
|
return result;
|
}
|
|
public void setMap(String key, Map<String,String> map) {
|
ShardedJedis client = null;
|
try {
|
client = this.getJedisClient();
|
client.hmset(key, map);
|
} finally {
|
this.closeJedisClient(client);
|
}
|
}
|
|
public List<String> getMap(String key, String... mKey) {
|
ShardedJedis client = null;
|
List<String> result = null;
|
try {
|
client = this.getJedisClient();
|
result = client.hmget(key, mKey);
|
} finally {
|
this.closeJedisClient(client);
|
}
|
return result;
|
}
|
|
public String hget(String key, String field) {
|
ShardedJedis client = null;
|
try {
|
client = this.getJedisClient();
|
return client.hget(key, field);
|
} finally {
|
this.closeJedisClient(client);
|
}
|
}
|
|
public <T> Long hset(String key, String field, T value) {
|
ShardedJedis client = null;
|
try {
|
client = this.getJedisClient();
|
return client.hset(key.getBytes(), field.getBytes(), ReisUtils.serialize(value));
|
} finally {
|
this.closeJedisClient(client);
|
}
|
}
|
|
public Long hdel(String key, String... field) {
|
ShardedJedis client = null;
|
try {
|
client = this.getJedisClient();
|
return client.hdel(key, field);
|
} finally {
|
this.closeJedisClient(client);
|
}
|
}
|
|
public Long hdel(String key, byte[]... field) {
|
ShardedJedis client = null;
|
try {
|
client = this.getJedisClient();
|
return client.hdel(key.getBytes(), field);
|
} finally {
|
this.closeJedisClient(client);
|
}
|
}
|
|
@SuppressWarnings("unchecked")
|
public <T> T get(String key, Class<T> cls) {
|
ShardedJedis client = null;
|
T result = null;
|
try {
|
client = this.getJedisClient();
|
byte[] obj = client.get(key.getBytes());
|
if (obj == null || obj.length==0) {
|
return null;
|
}
|
result = (T)ReisUtils.unserialize(obj);
|
} finally {
|
this.closeJedisClient(client);
|
}
|
return result;
|
}
|
|
public void setex(String key, Object value, int timeout) {
|
ShardedJedis client = null;
|
try {
|
client = this.getJedisClient();
|
client.setex(key.getBytes(), timeout, ReisUtils.serialize(value));
|
} finally {
|
this.closeJedisClient(client);
|
}
|
}
|
|
/**
|
* @param key
|
* @param value
|
* @return
|
*/
|
public long setnx(String key, String value) {
|
ShardedJedis client = null;
|
try {
|
client = this.getJedisClient();
|
return client.setnx(key, value);
|
} finally {
|
this.closeJedisClient(client);
|
}
|
}
|
|
public boolean setNxEx(String key, String value, long seconds) {
|
Jedis jedis = null;
|
ShardedJedis client = null;
|
try {
|
client = this.getJedisClient();
|
jedis = client.getShard(key);
|
long a = ((Long) jedis.eval(SET_NX_SCRIPT, 3, key, String.valueOf(seconds), value)).longValue();
|
return a == 1L;
|
} finally {
|
if (jedis != null) {
|
jedis.close();
|
}
|
this.closeJedisClient(client);
|
}
|
}
|
|
/**
|
* 多长时间内尝试获取锁
|
*
|
* @param key
|
* @param timeOut
|
* @return
|
* @throws Exception
|
*/
|
public boolean lock(String key, int timeOut) throws Exception {
|
long waitEndTime = System.currentTimeMillis() + (timeOut * 1000);
|
String lockKey = "JedisLock_".concat(key);
|
while (!this.setNxEx(lockKey, "1", 60 * 60)) {
|
if (waitEndTime < System.currentTimeMillis()) {
|
return false;
|
}
|
try {
|
// 每隔200ms秒尝试获取锁
|
Thread.sleep(200);
|
} catch (InterruptedException e) {
|
throw e;
|
}
|
}
|
return true;
|
}
|
|
public boolean tryLock(String key) {
|
String lockKey = "JedisLock_".concat(key);
|
return this.setNxEx(lockKey, "1", 30 * 60);
|
}
|
|
protected boolean tryLock(String key, long lockSeconds) {
|
String lockKey = "JedisLock_".concat(key);
|
return this.setNxEx(lockKey, "1", lockSeconds);
|
}
|
|
/**
|
*
|
* @param key
|
* @return
|
*/
|
public boolean release(String key) {
|
ShardedJedis client = null;
|
try {
|
client = this.getJedisClient();
|
String lockKey ="JedisLock_".concat(key);
|
return client.del(lockKey) == 1;
|
} finally {
|
this.closeJedisClient(client);
|
}
|
}
|
|
|
public void lpush(String key,String value){
|
ShardedJedis client = null;
|
try{
|
client = this.getJedisClient();
|
client.lpush(key, value);
|
}finally{
|
this.closeJedisClient(client);
|
}
|
}
|
|
public void rpush(String key,String str){
|
ShardedJedis client = null;
|
try{
|
client = this.getJedisClient();
|
client.rpush(key,str);
|
}finally{
|
this.closeJedisClient(client);
|
}
|
}
|
|
public<T> void rpush(String key,T t){
|
ShardedJedis client = null;
|
try{
|
client = this.getJedisClient();
|
client.rpush(key.getBytes(),ReisUtils.serialize(t));
|
}finally{
|
this.closeJedisClient(client);
|
}
|
}
|
|
public String rpop(String key){
|
ShardedJedis client = null;
|
String str = null;
|
try{
|
client = this.getJedisClient();
|
str = client.rpop(key);
|
}finally{
|
this.closeJedisClient(client);
|
}
|
return str;
|
}
|
|
|
public String lpop(String key){
|
ShardedJedis client = null;
|
String str = null;
|
try{
|
client = this.getJedisClient();
|
str = client.lpop(key);
|
}finally{
|
this.closeJedisClient(client);
|
}
|
return str;
|
}
|
|
|
/*public List<String> lrange(String key,Long start,Long end){
|
ShardedJedis client = null;
|
List<String> list = null;
|
try{
|
client = this.getJedisClient();
|
|
list = client.lrange(key, start, end);
|
}finally{
|
this.closeJedisClient(client);
|
}
|
return list;
|
}*/
|
|
public<T> List<T> lrange(String key,Long start,Long end){
|
ShardedJedis client = null;
|
List<byte[]> list = null;
|
List<T> list1 = new ArrayList<T>();
|
try{
|
client = this.getJedisClient();
|
|
list = client.lrange(key.getBytes(), start, end);
|
for(int i = 0;i < list.size();i++){
|
@SuppressWarnings("unchecked")
|
T t = (T) ReisUtils.unserialize(list.get(i));
|
list1.add(t);
|
}
|
}finally{
|
this.closeJedisClient(client);
|
}
|
return list1;
|
}
|
|
/**
|
* 对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。
|
*
|
* @param key
|
* @param start
|
* @param end
|
* @return
|
*/
|
public String ltrim(String key,Long start,Long end){
|
ShardedJedis client = null;
|
String result = null;
|
try{
|
client = this.getJedisClient();
|
result = client.ltrim(key.getBytes(), start, end);
|
}finally{
|
this.closeJedisClient(client);
|
}
|
return result;
|
}
|
}
|