package com.waken.dorm.common.utils.redis;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Lazy;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisListCommands.Position;
import org.springframework.data.redis.connection.RedisZSetCommands.Tuple;
import org.springframework.data.redis.connection.SortParameters;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* @ClassName RedisCacheManager
* @Description TODO
* @Author zhaoRong
* @Date 2019/3/21 19:56
**/
@Component
@Lazy(false)
public class RedisCacheManager implements InitializingBean, DisposableBean {
// private static Logger logger = LoggerFactory.getLogger(RedisCacheManager.class);
@Resource
public RedisTemplate<Object, ?> redisTemplate;
private RedisSerializer<Object> serializer;
// ======================String 相关操作========================
/**
* 将字符串值 value 关联到 key, 将字符串值 value 关联到 key 。如果 key 已经持有其他值, SET 就覆写旧值,无视类型。
*
* @param key
* @param value
* @return
*/
public Boolean set(final Object key, final Object value) {
if (redisTemplate != null) {
return redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection)
throws DataAccessException {
byte[] keys = serializer.serialize(key);
byte[] values = serializer.serialize(value);
connection.set(keys, values);
return true;
}
});
}
return false;
}
/**
* 将值 value 关联到 key ,并将 key 的生存时间设为 seconds (以秒为单位)。
* <p>
* 如果 key 已经存在, SETEX 命令将覆写旧值。
*
* @param key
* @param value
* @param t
* @return
*/
public Boolean setEx(final Object key, final Object value, final Long t) {
if (redisTemplate != null) {
return redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection)
throws DataAccessException {
byte[] keys = serializer.serialize(key);
byte[] values = serializer.serialize(value);
connection.setEx(keys, t, values);
return true;
}
});
}
return false;
}
/**
* 这个命令和 SETEX 命令相似,但它以毫秒为单位设置 key 的生存时间,而不是像 SETEX 命令那样,以秒为单位。
*
* @param key
* @param value
* @param t
* @return
*/
public Boolean pSetEx(final Object key, final Object value, final Long t) {
if (redisTemplate != null) {
return redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection)
throws DataAccessException {
byte[] keys = serializer.serialize(key);
byte[] values = serializer.serialize(value);
connection.pSetEx(keys, t, values);
return true;
}
});
}
return false;
}
/**
* 将 key 的值设为 value ,当且仅当 key 不存在。
* <p>
* 若给定的 key 已经存在,则 SETNX 不做任何动作。
*
* @param key
* @param value
* @return
*/
public Boolean setNx(final Object key, final Object value) {
if (redisTemplate != null) {
return redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection)
throws DataAccessException {
byte[] keys = serializer.serialize(key);
byte[] values = serializer.serialize(value);
connection.setNX(keys, values);
return true;
}
});
}
return false;
}
/**
* 同时设置一个或多个 key-value 对。
*
* @param tuple
* @param overwrite 是否覆盖,true:覆盖,false:不覆盖
* @return
*/
public Boolean mSet(final Map<Object, Object> tuple, final boolean overwrite) {
if (redisTemplate != null) {
return redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection)
throws DataAccessException {
if (null == tuple || tuple.isEmpty()) {
return false;
}
Map<byte[], byte[]> t = Maps.newHashMap();
for (Entry<Object, Object> e : tuple.entrySet()) {
t.put(serializer.serialize(e.getKey()),
serializer.serialize(e.getValue()));
}
if (overwrite) {
connection.mSet(t);
} else {
connection.mSetNX(t);
}
return true;
}
});
}
return false;
}
/**
* 返回 key 所关联的字符串值。
*
* @param key
* @return
*/
public Object get(final Object key) {
if (redisTemplate != null) {
return redisTemplate.execute(new RedisCallback<Object>() {
public Object doInRedis(RedisConnection connection)
throws DataAccessException {
byte[] keys = serializer.serialize(key);
byte[] values = connection.get(keys);
if (values == null) {
return null;
}
return serializer.deserialize(values);
}
});
}
return null;
}
public Object getString(final Object key) {
if (redisTemplate != null) {
return redisTemplate.execute(new RedisCallback<Object>() {
public Object doInRedis(RedisConnection connection)
throws DataAccessException {
byte[] keys = serializer.serialize(key);
byte[] values = connection.get(keys);
if (values == null) {
return null;
}
return new String(values);
}
});
}
return null;
}
/**
* 返回所有(一个或多个)给定 key 的值。
*
* @param keys
* @return
*/
public Map<Object, Object> mGet(final Object... keys) {
if (redisTemplate != null) {
return redisTemplate
.execute(new RedisCallback<Map<Object, Object>>() {
public Map<Object, Object> doInRedis(
RedisConnection connection)
throws DataAccessException {
final byte[][] rawKeys = ne