
com.moon.spring.data.redis.RedisService Maven / Gradle / Ivy
package com.moon.spring.data.redis;
import com.moon.core.util.ListUtil;
import com.moon.core.util.Storage;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.data.redis.core.*;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
/**
* @author moonsky
*/
public class RedisService implements Storage {
private final RedisTemplate template;
private final ExceptionHandler strategy;
public RedisService(RedisTemplate redisTemplate) { this(redisTemplate, null); }
public RedisService(RedisTemplate redisTemplate, ExceptionStrategy exceptionStrategy) {
this(redisTemplate, (ExceptionHandler) exceptionStrategy);
}
public RedisService(RedisTemplate redisTemplate, ExceptionHandler exceptionStrategy) {
this.strategy = exceptionStrategy == null ? ExceptionStrategy.LOGGER_INFO : exceptionStrategy;
this.template = redisTemplate;
}
private void onException(Exception ex) { strategy.onException(ex); }
private boolean onExceptionThenFalse(Exception ex) {
onException(ex);
return false;
}
private long onExceptionThenZero(Exception ex) {
onException(ex);
return 0;
}
private T onExceptionThen(Exception ex, T result) {
onException(ex);
return result;
}
// ============================= ops ===============================
public ValueOperations value() { return template.opsForValue(); }
public ClusterOperations cluster() { return template.opsForCluster(); }
public GeoOperations geo() { return template.opsForGeo(); }
public HashOperations hash() { return template.opsForHash(); }
public ListOperations list() { return template.opsForList(); }
public SetOperations valueSet() { return template.opsForSet(); }
public StreamOperations stream() { return template.opsForStream(); }
public ZSetOperations zset() { return template.opsForZSet(); }
public BoundGeoOperations geo(K key) { return template.boundGeoOps(key); }
public BoundHashOperations hash(K key) { return template.boundHashOps(key); }
public BoundListOperations list(K key) { return template.boundListOps(key); }
public BoundSetOperations collect(K key) { return template.boundSetOps(key); }
public BoundStreamOperations stream(K key) { return template.boundStreamOps(key); }
public BoundValueOperations value(K key) { return template.boundValueOps(key); }
public BoundZSetOperations zset(K key) { return template.boundZSetOps(key); }
/**
* 指定缓存失效时间
*
* @param key 键
* @param time 时间(秒)
*
* @return 返回是否设置成功
*/
public boolean expire(K key, long time) {
try {
return time > 0 && falseIfNull(template.expire(key, time, TimeUnit.SECONDS));
} catch (Exception e) {
return onExceptionThenFalse(e);
}
}
/**
* 根据 key 获取过期时间
*
* @param key 键 不能为null
*
* @return 时间(秒) 返回0代表为永久有效
*/
public long getExpire(K key) { return zeroIfNull(template.getExpire(key, TimeUnit.SECONDS)); }
/**
* 判断key是否存在
*
* @param key 键
*
* @return true 存在 false不存在
*/
@Override
public boolean hasKey(K key) {
try {
return falseIfNull(template.hasKey(key));
} catch (Exception e) {
return onExceptionThenFalse(e);
}
}
/**
* 删除缓存
*
* @param key 指定 key
*
* @return 返回缓存中是否存在 key 对应的值
*/
public boolean delete(K key) { return key == null || falseIfNull(template.delete(key)); }
/**
* 删除缓存
*
* @param keys 可以传一个值 或多个
*/
@SafeVarargs
public final long delete(K... keys) {
if (keys != null && keys.length > 0) {
if (keys.length == 1) {
return delete(keys[0]) ? 1 : 0;
} else {
return zeroIfNull(template.delete(ListUtil.toList(keys)));
}
}
return 0;
}
/*
* ***********************************************************************************************************
* * value ***************************************************************************************************
* ***********************************************************************************************************
*/
/**
* 普通缓存放入
*
* @param key 键
* @param value 值
*
* @return true 成功 false失败
*/
@Override
public void set(K key, V value) {
valueSet(key, value);
}
/**
* 普通缓存放入
*
* @param key 键
* @param value 值
*
* @return true 成功 false失败
*/
public void valueSet(K key, V value) {
try {
value().set(key, value);
} catch (Exception e) {
onExceptionThenFalse(e);
}
}
/**
* 普通缓存放入并设置时间
*
* @param key 键
* @param value 值
* @param expireOfMilliseconds 时间(秒) time要大于0 如果time小于等于0 将设置无限期
*
* @return true成功 false 失败
*/
public boolean valueSet(K key, V value, long expireOfMilliseconds) {
return valueSet(key, value, expireOfMilliseconds, TimeUnit.MILLISECONDS);
}
/**
* 普通缓存放入并设置时间
*
* @param key 键
* @param value 值
* @param time 时间
* @param timeUnit 时间单位
*
* @return true成功 false 失败
*/
public boolean valueSet(K key, V value, long time, TimeUnit timeUnit) {
try {
if (time > 0) {
value().set(key, value, time, timeUnit);
} else {
valueSet(key, value);
}
return true;
} catch (Exception e) {
return onExceptionThenFalse(e);
}
}
/**
* 普通缓存获取
*
* @param key 键
*
* @return 值
*/
@Override
public V get(K key) { return valueGet(key); }
/**
* 普通缓存获取
*
* @param key 键
*
* @return 值
*/
public V valueGet(K key) { return key == null ? null : value().get(key); }
/**
* 查找或返回默认值
*
* @param key key
* @param defaultValue 默认值
*
* @return 查找或返回默认值
*/
public V getOrDefault(K key, V defaultValue) {
V cached = get(key);
return cached == null ? defaultValue : cached;
}
/**
* 从缓存中获取,获取为 null 则加载,并缓存
*
* @param key 键
* @param puller 加载器
*
* @return 值
*/
public V getOrPull(K key, Supplier puller) {
V cached = get(key);
if (cached == null) {
cached = puller.get();
valueSet(key, cached);
}
return cached;
}
/**
* 从缓存中获取,获取为 null 则加载,并指定缓存时间缓存
*
* @param key 键
* @param puller 加载器
* @param expireOfMilliseconds 时间
*
* @return 值
*/
public V getOrPull(K key, Supplier puller, long expireOfMilliseconds) {
V cached = get(key);
if (cached == null) {
cached = puller.get();
valueSet(key, cached, expireOfMilliseconds);
}
return cached;
}
/**
* 从缓存中获取,获取为 null 则加载,并指定缓存时间缓存
*
* @param key 键
* @param puller 加载器
* @param time 时间
* @param timeUnit 时间单位
*
* @return 值
*/
public V getOrPull(K key, Supplier puller, long time, TimeUnit timeUnit) {
V cached = get(key);
if (cached == null) {
cached = puller.get();
valueSet(key, cached, time, timeUnit);
}
return cached;
}
/**
* 删除缓存
*
* @param key 指定 key
*/
@Override
public void remove(K key) { delete(key); }
/**
* 递增
*
* @param key 键
*
* @return long
*/
public long increment(K key) { return zeroIfNull(value().increment(key)); }
/**
* 递增
*
* @param key 键
* @param delta 增量(建议大于0,若小于 0 等价于 decrement)
*
* @return long
*/
public long increment(K key, long delta) { return zeroIfNull(value().increment(key, delta)); }
/**
* 递减
*
* @param key 键
*
* @return long
*/
public long decrement(K key) { return zeroIfNull(value().decrement(key)); }
/**
* 递减
*
* @param key 键
* @param delta 增量(建议小于0,若大于 0 等价于 increment)
*
* @return long
*/
public long decrement(K key, long delta) { return zeroIfNull(value().increment(key, -delta)); }
/*
* ***********************************************************************************************************
* * hash ****************************************************************************************************
* ***********************************************************************************************************
*/
/**
* HashGet
*
* @param key 键 不能为null
* @param item 项 不能为null
*
* @return 值
*/
public Object hashGet(K key, String item) { return hash().get(key, item); }
/**
* 获取hashKey对应的所有键值
*
* @param key 键
*
* @return 对应的多个键值
*/
public Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy