springmvc中redis数据操作封装(RedisTemplate)
简单直接上代码
package com.xxxx.common;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import redis.clients.jedis.Jedis;
@Service
public class ReidsImpl{
@Resource
private RedisTemplate<Object, Object> redisTemplate;
private static final String objectKey="objectMapKey";
public void delete(String... keys) {
redisTemplate.delete(CollectionUtils.arrayToList(keys));
}
public void expire(String key,Integer timeout) {
redisTemplate.expire(key, timeout, TimeUnit.MILLISECONDS);
}
public void persist(String key) {
redisTemplate.persist(key);
}
public boolean hasKey(String key) {
return redisTemplate.hasKey(key);
}
public DataType getType(String key) {
return redisTemplate.type(key);
}
public Object redomKey() {
return redisTemplate.randomKey();
}
public void setObject(String key,Object value) {
final byte[] values = SerializeUtil.serialize(value);
redisTemplate.execute((RedisCallback<Void>) connection -> {
connection.set(key.getBytes(), values);return null;});
}
public <T> T getObject(final String key, Class<T> targetClass) {
byte[] result = redisTemplate.execute((RedisCallback<byte[]>) connection -> connection.get(key.getBytes()));
if (result == null) {
return null;
}
return SerializeUtil.deserialize(result, targetClass);
}
public <T> void setObjectTime(String key, T value, final long expireTime) {
final byte[] values = SerializeUtil.serialize(value);
redisTemplate.execute((RedisCallback<Void>) connection -> {
connection.setEx(key.getBytes(), expireTime, values);
return null;
});
}
public <T> void setList(String key, List<T> objList) {
final byte[] value = SerializeUtil.serializeList(objList);
redisTemplate.execute((RedisCallback<Void>) connection -> {
connection.set(key.getBytes(), value);
return null;
});
}
public <T> List<T> getList(final String key, Class<T> targetClass) {
byte[] result = redisTemplate.execute((RedisCallback<byte[]>) connection -> connection.get(key.getBytes()));
if (result == null) {
return null;
}
return SerializeUtil.deserializeList(result, targetClass);
}
public <T> T hGetObject(final String key, final String hashKey, Class<T> targetClass) {
byte[] result = redisTemplate.execute((RedisCallback<byte[]>) connection -> connection.hGet(key.getBytes(), hashKey.getBytes()));
if (result == null) {
return null;
}
return SerializeUtil.deserialize(result, targetClass);
}
public <T> boolean hSetObject(String key, String hashKey, T obj) {
final byte[] value = SerializeUtil.serialize(obj);
return redisTemplate.execute( (RedisCallback<Boolean>) connection -> connection.hSet(key.getBytes(), hashKey.getBytes(), value));
}
public <T> void hSetObjectTime(String key, String hashKey, T obj, long expireTime) {
final byte[] value = SerializeUtil.serialize(obj);
redisTemplate.execute((RedisCallback<Void>) connection -> {
connection.hSet(key.getBytes(