package com.haier.hsi.workplace.redis;
import com.haier.hsi.framework.commons.util.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* redis操作类 所有需要存入redis的对象都必须序列化
*
* @param <K> 目前仅支持String类型
* @author WangHQ
* @date 2017-08-24 13:59
* @param <V>
*/
@Component
@SuppressWarnings("unchecked")
public class BaseRedisHelper<K, V> {
@Resource(name = "redisTemplate")
private RedisTemplate<K, V> redisTemplate; // redis服务
private String prefix = "com:haier:hsi:baseData:"; // 前缀
/**
* 构造方法
*
* @author WangHQ
* @date 2017-08-24
*/
public BaseRedisHelper() {}
/**
* 设置对象
*
* @param key 键
* @param value 值
* @return 执行结果
* @author WangHQ
* @date 2017-08-24
*/
public boolean setObject(K key, V value) {
keySerialize();
SessionCallback<V> sessionCallback = new SessionCallback<V>() {
@Override
public List<V> execute(RedisOperations redisOperations) throws DataAccessException {
redisOperations.multi();
redisOperations.opsForValue().set(prefix + key, value, 60 * 60 * 24, TimeUnit.SECONDS);
return redisOperations.exec();
}
};
redisTemplate.execute(sessionCallback);
return true;
}
/**
* key唯一 如果key存在 就返回false 并且无法插入 lixy add
* */
public boolean setIfAbsent(K key, V value) {
keySerialize();
SessionCallback<V> sessionCallback = new SessionCallback<V>() {
@Override
public List<V> execute(RedisOperations redisOperations) throws DataAccessException {
redisOperations.multi();
Boolean bb = redisOperations.opsForValue().setIfAbsent(prefix + key, value);
List<V> ss = redisOperations.exec();
return ss;
}
};
List<V> sss = (List<V>)redisTemplate.execute(sessionCallback);
V s = sss.get(0);
return (Boolean)s;
}
/**
* key唯一 如果key存在 就返回false 并且无法插入 lixy add
* */
public boolean setIfAbsent(K key, V value, long time) {
keySerialize();
SessionCallback<V> sessionCallback = new SessionCallback<V>() {
@Override
public List<V> execute(RedisOperations redisOperations) throws DataAccessException {
redisOperations.multi();
redisOperations.opsForValue().setIfAbsent(prefix + key, value);
List<V> ss = redisOperations.exec();
System.out.println("ss=" + ss.toString());
V s = ss.get(0);
if((Boolean)s){
redisOperations.multi();
redisOperations.expire(prefix + key, time, TimeUnit.SECONDS);
redisOperations.exec();
System.out.println("time=" + new Date().toString());
}
return ss;
}
};
List<V> sss = (List<V>)redisTemplate.execute(sessionCallback);
V s = sss.get(0);
return (Boolean)s;
}
/**
* 设置对象和生存时效
*
* @param key 键
* @param value 值
* @param time
* @return 执行结果
* @author wayne
* @date 2018-05-11
*/
public boolean setObject(K key, V value, long time) {
keySerialize();
SessionCallback<V> sessionCallback = new SessionCallback<V>() {
@Override
public List<V> execute(RedisOperations redisOperations) throws DataAccessException {
redisOperations.multi();
redisOperations.opsForValue().set(prefix + key, value, time, TimeUnit.SECONDS);
return redisOperations.exec();
}
};
redisTemplate.execute(sessionCallback);
return true;
}
/**
* 设置对象和生存时效
*
* @param key 键
* @param value 值
* @param noTimeout
* @return 执行结果
* @author wayne
* @date 2019-05-11
*/
public boolean setObject(K key, V value, boolean noTimeout) {
keySerialize();
SessionCallback<V> sessionCallback = new SessionCallback<V>() {
@Override
public List<V> execute(RedisOperations redisOperations) throws DataAccessException {
redisOperations.multi();
redisOperations.opsForValue().set(prefix + key, value);
return redisOperations.exec();
}
};
redisTemplate.execute(sessionCallback);
return true;
}
/**
* 设置对象list
*
* @param map map
* @return 执行结果
* @author WangHQ
* @date 2017-08-24
*/
public boolean setObjectList(Map<K, V> map) {
keySerialize();
Map<K, V> setMap = new HashMap<>();
for (Map.Entry<K, V> kvEntry : map.entrySet()) {
setMap.put((K) (prefix + kvEntry.getKey()), kvEntry.getValue());
}
SessionCallback<V> sessionCallback = new SessionCallback<V>() {
@Override
public List<V> execute(RedisOperations redisOperations) throws DataAccessException {
redisOperations.multi();
redisOperations.opsForValue().multiSet(setMap);
return redisOperations.exec();
}
};
redisTemplate.execute(sessionCallback);
return true;
}
/**
* 获取对象
*
* @param key 键
* @return 对象
* @author WangHQ
* @date 2017-08-24
*/
public V getObject(K key) {
keySerialize();
SessionCallback<V> sessionCallback = new SessionCallback<V>() {
@Override
public V execute(RedisOperations redisOperations) throws DataAccessException {
redisOperations.multi();
redisOperations.opsForValue().get(prefix + key);
List<V> v = redisOperations.exec();
if (ListUtil.isNotEmpty(v)) {
return v.get(0);
} else {
return null;
}
}
};
return redisTemplate.execute(sessionCallback);
}
/**
* 获取对象列表
*
* @param pattern 表达式 keys
* @return 对象
* @author WangHQ
* @date 2017-08-24
*/
public List<V> getObjectList(K pattern) {
keySerialize();
Set<K> keys = redisTemplate.keys((K) (prefix + pattern));
// 如果没有相关的key存在,就没有必要进行后续操作了
if (keys.isEmpty()) {
return new ArrayList<>();
}
SessionCallback<List<V>> sessionCallback = new SessionCallback<List<V>>() {
@Override
public List<V> execute(RedisOperations redisOperations) throws DataAccessException {
redisOperations.multi();
redisOperations.opsForValue().multiGet(keys);
List<V> v = redisOperations.exec();
List<V> list = new ArrayList<>();
if (ListUtil.isNotEmpty(v)) {
list.addAll((List<V>) v.get(0));
}
return list;
}
};
return redisTemplate.execute(sessionCallback);
}
/**
* 删除对象
*
* @param key 键
* @return 执行结果
* @author WangHQ
* @date 2017-08-24
*/
public boolean deleteObject(K key) {
keySerialize();
SessionCallback<V> sessionCallback = new SessionCallback<V>() {
@Override
public List<V> execute(RedisOperations operations) throws DataAccessException {
operations.multi();
operations.delete(prefix + key);
return operations.exec();
}
};
redisTemplate.execute(sessionCallback);
return true;
}
/**
* 根据表达式删除对象
*
* @param pattern 表达式 keys
* @return 执行结果
* @author WangHQ
* @date 2017-08-24
*/
public boolean deleteObjectList(K pattern) {
keySerialize();
Set<K> keys = redisTemplate.keys((K) (prefix + pattern));
// 如果没有相关的key存在,就没有必要进行后续操作了
if (keys.isEmpty()) {
return false;
}
SessionCallback<V> sessionCallback = new SessionCallback<V>() {
@Override
public List<V> execute(RedisOperations operations) throws DataAccessException {
operations.multi();
operations.delete(keys);
return operations.exec();
}
};
redisTemplate.execute(sessionCallback);
return true;
}
/**
* 序列化
*
* @author WangHQ
* @date 2017-08-24
*/
private void keySerialize() {
redisTemplate.setKeySerializer(new StringRedisSerializer());
}
/**
* 按照表达式查询key列表
* @param pattern
* @return
*/
public Set<K> queryKeysByPattern(String pattern) {
return redisTemplate.keys((K) (prefix + pattern + "*"));
}
/**
* 按照表达式模糊查询key列表
* @param patternA,patternB
* @return
*/
public Set<K> queryKeysByPattern(String patternA,String patternB) {
return redisTemplate.keys((K) (prefix + patternA + "*"+patternB));
}
}
//~ Formatted by Jindent --- http://www.jindent.com