|
@ -6,6 +6,8 @@ import org.springframework.data.redis.connection.RedisConnection;
|
|
|
import org.springframework.data.redis.connection.StringRedisConnection;
|
|
|
import org.springframework.data.redis.core.RedisCallback;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.data.redis.serializer.RedisSerializer;
|
|
|
import org.springframework.data.redis.serializer.SerializationException;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.SerializationUtils;
|
|
|
|
|
@ -46,11 +48,11 @@ public class RedisClient {
|
|
|
* @param key
|
|
|
* @param value
|
|
|
*/
|
|
|
public void set(final String key, final Serializable value,long seconds) {
|
|
|
public void set(final String key, final Serializable value, long seconds) {
|
|
|
redisTemplate.execute((RedisCallback<Object>) connection -> {
|
|
|
byte[] key_ = key.getBytes();
|
|
|
byte[] value_ = SerializationUtils.serialize(value);
|
|
|
connection.setEx(key_,seconds, value_);
|
|
|
connection.setEx(key_, seconds, value_);
|
|
|
return true;
|
|
|
});
|
|
|
}
|
|
@ -60,12 +62,12 @@ public class RedisClient {
|
|
|
*
|
|
|
* @param data
|
|
|
*/
|
|
|
public void multiSet(Map<Serializable, Serializable> data){
|
|
|
public void multiSet(Map<Serializable, Serializable> data) {
|
|
|
redisTemplate.executePipelined(new RedisCallback<Object>() {
|
|
|
@Override
|
|
|
public Object doInRedis(RedisConnection connection) throws DataAccessException {
|
|
|
StringRedisConnection stringRedisConn = (StringRedisConnection)connection;
|
|
|
for(Serializable key : data.keySet()){
|
|
|
StringRedisConnection stringRedisConn = (StringRedisConnection) connection;
|
|
|
for (Serializable key : data.keySet()) {
|
|
|
Serializable value = data.get(key);
|
|
|
connection.rPushX(SerializationUtils.serialize(key), SerializationUtils.serialize(value));
|
|
|
}
|
|
@ -80,7 +82,7 @@ public class RedisClient {
|
|
|
*
|
|
|
* @param data
|
|
|
*/
|
|
|
public void multiSetData(Map<String, Serializable> data){
|
|
|
public void multiSetData(Map<String, Serializable> data) {
|
|
|
redisTemplate.executePipelined(new RedisCallback<Object>() {
|
|
|
@Override
|
|
|
public Object doInRedis(RedisConnection connection) throws DataAccessException {
|
|
@ -104,10 +106,12 @@ public class RedisClient {
|
|
|
* @return
|
|
|
*/
|
|
|
public <T> T get(final String key) {
|
|
|
return (T)redisTemplate.execute((RedisCallback<Serializable>) connection -> {
|
|
|
return (T) redisTemplate.execute((RedisCallback<Serializable>) connection -> {
|
|
|
byte[] keyBytes = key.getBytes();
|
|
|
byte[] bytes = connection.get(keyBytes);
|
|
|
if(bytes == null) return null;
|
|
|
if (bytes == null) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
return (Serializable) SerializationUtils.deserialize(bytes);
|
|
|
});
|
|
@ -119,8 +123,24 @@ public class RedisClient {
|
|
|
* @param keys
|
|
|
* @return
|
|
|
*/
|
|
|
public List<Serializable> multiGet(Collection<String> keys){
|
|
|
return redisTemplate.opsForValue().multiGet(keys);
|
|
|
public List<Object> multiGet(Collection<String> keys) {
|
|
|
return redisTemplate.executePipelined((RedisCallback<Object>) connection -> {
|
|
|
keys.forEach(key -> {
|
|
|
byte[] keyBytes = key.getBytes();
|
|
|
connection.get(keyBytes);
|
|
|
});
|
|
|
return null;
|
|
|
}, new RedisSerializer<Serializable>() {
|
|
|
@Override
|
|
|
public byte[] serialize(Serializable serializable) throws SerializationException {
|
|
|
return SerializationUtils.serialize(serializable);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Serializable deserialize(byte[] bytes) throws SerializationException {
|
|
|
return (Serializable) SerializationUtils.deserialize(bytes);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
/**
|
|
@ -134,6 +154,7 @@ public class RedisClient {
|
|
|
|
|
|
/**
|
|
|
* 删除多条记录,如果Key集合过大,建议使用Key模糊匹配删除
|
|
|
*
|
|
|
* @param keys
|
|
|
*/
|
|
|
public void delete(Collection<String> keys) {
|
|
@ -142,6 +163,7 @@ public class RedisClient {
|
|
|
|
|
|
/**
|
|
|
* 匹配特定模式的Key列表
|
|
|
*
|
|
|
* @param pattern
|
|
|
* @return
|
|
|
*/
|
|
@ -158,6 +180,7 @@ public class RedisClient {
|
|
|
|
|
|
/**
|
|
|
* 是否包含指定Key
|
|
|
*
|
|
|
* @param key
|
|
|
* @return
|
|
|
*/
|