最近频繁使用到redis中间件,然后就封装了一下类,使用更方便些
@Component
@Slf4j
public class FundsRedisCacheUtil {
@Autowired
private RedisTemplate<Object,Object> redisTemplate ;
public RedisTemplate<Object,Object> getInstance(){
return redisTemplate;
}
/**
* 设置 String 类型 key-value
* @param key
* @param value
*/
public void set(String key,String value){
redisTemplate.opsForValue().set(key, value);
}
/**
* 获取 String 类型 key-value
* @param key
* @return
*/
public String get(String key){
return (String) redisTemplate.opsForValue().get(key);
}
/**
* 设置 String 类型 key-value 并添加过期时间 (毫秒单位)
* @param key
* @param value
* @param time 过期时间,毫秒单位
*/
public void setForTimeMS(String key,String value,long time){
redisTemplate.opsForValue().set(key, value, time, TimeUnit.MILLISECONDS);
}
/**
* 设置 String 类型 key-value 并添加过期时间 (分钟单位)
* @param key
* @param value
* @param time 过期时间,分钟单位
*/
public void setForTimeMIN(String key,String value,long time){
redisTemplate.opsForValue().set(key, value, time, TimeUnit.MINUTES);
}
/**
* 如果 key 存在则覆盖,并返回旧值.
* 如果不存在,返回null 并添加
* @param key
* @param value
* @return
*/
public String getAndSet(String key,String value){
return (String) redisTemplate.opsForValue().getAndSet(key, value);
}
/**
* 删除 key-value
* @param key
* @return
*/
public boolean delete(String key){
return redisTemplate.delete(key);
}
/**
* 模糊删除key
* @param key
* @return
*/
public Long deleteLike(String key){
Set<Object> keys = redisTemplate.keys(key + "*");
Long deleteNum = redisTemplate.delete(keys);
log.info("{}, redis中模糊 "+key+" 被清空");
return deleteNum;
}
//hash操作
/**
* 添加 Hash 键值对
* @param key
* @param hashKey
* @param value
*/
public void putHash(String key, String hashKey, String value){
redisTemplate.opsForHash().put(key, hashKey, value);
}
/**
* 批量添加 hash 的 键值对
* 有则覆盖,没有则添加
* @param key
* @param map
*/
public void putAllHash(String key, Map<String,String> map){
redisTemplate.opsForHash().putAll(key, map);
}
/**
* 删除指定 hash 的 HashKey
* @param key
* @param hashKeys
* @return 删除成功的 数量
*/
public Long deleteHash(String key, String ...hashKeys){
return redisTemplate.opsForHash().delete(key, hashKeys);
}
/**
* 获取指定 key 下的 hashkey
* @param key
* @param hashKey
* @return
*/
public Object getHashKey(String key,String hashKey){
return redisTemplate.opsForHash().get(key, hashKey);
}
/**
* 验证指定 key 下 有没有指定的 hashkey
* @param key
* @param hashKey
* @return
*/
public boolean hashKey(String key,String hashKey){
return redisTemplate.opsForHash().hasKey(key, hashKey);
}
/**
* 获取 key 下的 所有 hashkey 字段名
* @param key
* @return
*/
public Set<Object> hashKeys(String key){
return redisTemplate.opsForHash().keys(key);
}
//List 操作
/**
* 指定 list 从左入栈
* @param key
* @return 当前队列的长度
*/
public Long leftPush(String key,Object value){
return redisTemplate.opsForList().leftPush(key, value);
}
/**
* 指定 list 从左出栈
* 如果列表没有元素,会堵塞到列表一直有元素或者超时为止
* @param key
* @return 出栈的值
*/
public Object leftPop(String key){
return redisTemplate.opsForList().leftPop(key);
}
/**
* 从左边依次入栈
* 导入顺序按照 Collection 顺序
* 如: a b c => c b a
* @param key
* @param values
* @return
*/
public Long leftPushAll(String key,Collection<Object> values){
return redisTemplate.opsForList().leftPushAll(key, values);
}
/**
* 指定 list 从右入栈
* @param key
* @return 当前队列的长度
*/
public Long rightPush(String key,Object value){
return redisTemplate.opsForList().rightPush(key, value);
}
/**
* 指定 list 从右出栈
* 如果列表没有元素,会堵塞到列表一直有元素或者超时为止
* @param key
* @return 出栈的值
*/
public Object rightPop(String key){
return redisTemplate.opsForList().rightPop(key);
}
/**
* 从右边依次入栈
* 导入顺序按照 Collection 顺序
* 如: a b c => a b c
* @param key
* @param values
* @return
*/
public Long rightPushAll(String key, Collection<Object> values){
return redisTemplate.opsForList().rightPushAll(key, values);
}
/**
* 根据下标获取值
* @param key
* @param index
* @return
*/
public Object popIndex(String key,long index){
return redisTemplate.opsForList().index(key, index);
}
/**
* 获取指定列表长度
* @param key
* @return
*/
public Long listSize(String key){
return redisTemplate.opsForList().size(key);
}
/**
* 获取列表 指定范围内的所有值
* @param key
* @param start
* @param end
* @return
*/
public List<Object> listRange(String key, long start, long end){
return redisTemplate.opsForList().range(key, start, end);
}
/**
* 删除 key 中 值为 value 的 count 个数.
* @param key
* @param count
* @param value
* @return 成功删除的个数
*/
public Long listRemove(String key,long count,Object value){
return redisTemplate.opsForList().remove(key, count, value);
}
/**
* 删除 列表 [start,end] 以外的所有元素
* @param key
* @param start
* @param end
*/
public void listTrim(String key,long start,long end){
redisTemplate.opsForList().trim(key, start, end);
}
/**
* 如果已经存在返回false,否则返回true
*
* @param key
* @param value
* @return
*/
public Boolean setNx(Object key, Object value, Long expireTime, TimeUnit mimeUnit) {
if (key == null || value == null) {
return false;
}
redisTemplate.setEnableTransactionSupport(true);
redisTemplate.multi();
redisTemplate.opsForValue().setIfAbsent(key,value);
redisTemplate.expire(key,expireTime, mimeUnit);
List<Object> exec = redisTemplate.exec();// 这里result会返回事务内每一个操作的结果,如果setIfAbsent操作失败后,result[0]会为false。
if(true == (boolean) exec.get(0)){
return true;
}else{
return false;
}
}
/**
* 加锁
*
* @param key
* @param waitTime 等待时间
* @param expireTime 过期时间
*/
public Boolean lock(String key, Long waitTime, Long expireTime) {
String value = UUID.randomUUID().toString().replaceAll("-", "").toLowerCase();
Boolean flag = setNx(key, value, expireTime, TimeUnit.SECONDS);
// 尝试获取锁 成功返回
if (flag) {
return flag;
} else {
// 获取失败
// 现在时间
long newTime = System.currentTimeMillis();
// 等待过期时间
long loseTime = newTime + waitTime;
// 不断尝试获取锁成功返回
while (System.currentTimeMillis() < loseTime) {
Boolean testFlag = setNx(key, value, expireTime, TimeUnit.SECONDS);
if (testFlag) {
return testFlag;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
return false;
}
}