Redisson配置:
- https://blog.51cto.com/u_14014612/6606350
- https://blog.csdn.net/weixin_51297617/article/details/121184342
Redis String
/**
* @Author: huangyibo
* @Date: 2023/7/12 12:51
* @Description: Redisson String 工具类
*/
@Component
@Slf4j
public class RedissonStringUtil {
@Resource
private RedissonClient redissonClient;
/**
* 获取 字符串 的 Bucket
*
* @param name 名称
* @return 返回 值
*/
public RBucket<String> getBucket(String name) {
RBucket<String> bucket = redissonClient.getBucket(name, StringCodec.INSTANCE);
Assert.notNull(bucket, "bucket is null");
return bucket;
}
/**
* 同步缓存
*
* @param name 名称
* @param value 值
*/
public void set(String name, String value) {
this.getBucket(name).set(value);
}
/**
* 同步缓存
*
* @param name 名称
* @param value 值
* @param expire 过期时间
* @param timeUnit 过期单位
*/
public void setExpire(String name, String value, long expire, TimeUnit timeUnit) {
this.getBucket(name).set(value, expire, timeUnit);
}
/**
* 异步缓存
*
* @param name 名称
* @param value 值
*/
public void setAsync(String name, String value) {
this.getBucket(name).setAsync(value);
}
/**
* 异步缓存
*
* @param name 名称
* @param value 值
* @param expire 过期时间
* @param timeUnit 过期单位
*/
public void setAsyncExpire(String name, String value, long expire, TimeUnit timeUnit) {
this.getBucket(name).setAsync(value, expire, timeUnit);
}
/**
* 获取 redis 的缓存值
*
* @param name 名称
* @return 返回 值
*/
public String get(String name) {
return this.getBucket(name).get();
}
/**
* 删除当前的 name
*
* @param name 名称
* @return 返回 是否删除成功
*/
public boolean delete(String name) {
return this.getBucket(name).delete();
}
/**
* 验证 是否存在
*
* @param name 名称
* @return 返回 是否存在
*/
public boolean isExists(String name) {
return this.getBucket(name).isExists();
}
/**
* 查询key的剩余过期时间 String数据类型
* @param key
* @return
*/
public long ttl(String key) {
return this.getBucket(key).remainTimeToLive();
}
/**
* 查询key的剩余过期时间 所有数据类型
* @param key
* @return
*/
public long ttlAllKey(String key) {
return redissonClient.getKeys().remainTimeToLive(key);
}
/**
* 原子方法
* @param key
* @param value
* @return
*/
public Object getAndSet(String key, Object value) {
return redissonClient.getBucket(key).getAndSet(value);
}
}
Redis List
/**
* @Author: huangyibo
* @Date: 2023/7/12 14:08
* @Description: Redisson List 工具类
*/
@Component
public class RedissonListUtil {
@Resource
private RedissonClient redissonClient;
/**
* 获取 字符串 的 RSet
*
* @param name 名称
* @return 返回 值
*/
public RList<String> getList(String name) {
RList<String> list = redissonClient.getList(name, StringCodec.INSTANCE);
Assert.notNull(list, "list is null");
return list;
}
/**
* 新增 list 数据
*
* @param name 名称
* @param value 值
* @return 返回 是否新增成功
*/
public boolean set(String name, String value) {
return this.getList(name).add(value);
}
/**
* 新增 list 数据
*
* @param name 名称
* @param values 值
* @return 返回 是否新增成功
*/
public boolean set(String name, List<String> values) {
return this.getList(name).addAll(values);
}
/**
* 读取 list 的数据
*
* @param name 名称
* @return 返回 是否新增成功
*/
public List<String> readAll(String name) {
return this.getList(name).readAll();
}
/**
* 验证 是否包含
*
* @param name 名称
* @param value 值
* @return 返回 是否 包含
*/
public boolean contains(String name, String value) {
return this.getList(name).contains(value);
}
/**
* 验证 是否包含
*
* @param name 名称
* @param values 值
* @return 返回 是否 包含
*/
public boolean containsAll(String name, List<String> values) {
return new HashSet<>(this.getList(name)).containsAll(values);
}
/**
* 移出第一个元素并且返回
*
* @param name 名称
* @return 返回 移出元素
*/
public String removeFirst(String name) {
return this.getList(name).remove(0);
}
/**
* 移出最后一个元素并且返回
*
* @param name 名称
* @return 返回 移出元素
*/
public String removeLast(String name) {
int size = this.getList(name).size();
return this.getList(name).remove(size - 1);
}
/**
* 移出元素并且返回
*
* @param name 名称
* @param index 索引
* @return 返回 移出元素
*/
public String remove(String name, int index) {
return this.getList(name).remove(index);
}
}
Redis Hash
/**
* @Author: huangyibo
* @Date: 2023/7/12 12:58
* @Description: Redisson Hash 工具类
*/
@Component
public class RedissonHashUtil {
@Resource
private RedissonClient redissonClient;
/**
* 获取 字符串 的 RSet
*
* @param name 名称
* @return 返回 值
*/
public RMap<String, Object> getMap(String name) {
RMap<String, Object> map = redissonClient.getMap(name, StringCodec.INSTANCE);
Assert.notNull(map, "map is null");
return map;
}
/**
* 新增 map 数据
*
* @param name 名称
* @param key 键
* @param value 值
* @return 返回 返回结果
*/
public Object put(String name, String key, Object value) {
return this.getMap(name).put(key, value);
}
/**
* 新增 map 数据
*
* @param name 名称
* @param map 值
*/
public void putAll(String name, Map<String, Object> map) {
this.getMap(name).putAll(map);
}
/**
* 新增 map 数据
*
* @param name 名称
* @param key 键
* @param value 值
* @return 返回 返回结果
*/
public Object putAsync(String name, String key, Object value) {
return this.getMap(name).putAsync(key, value);
}
/**
* 新增 map 数据
*
* @param name 名称
* @param map 值
*/
public void putAllAsync(String name, Map<String, Object> map) {
this.getMap(name).putAllAsync(map);
}
/**
* 读取 map 的数据
*
* @param name 名称
* @return 返回 是否新增成功
*/
public Set<Map.Entry<String, Object>> entrySet(String name) {
return this.getMap(name).entrySet();
}
/**
* 读取 map 的数据
*
* @param name 名称
* @return 返回 是否 包含
*/
public Map<String, Object> readAllMap(String name) {
return this.getMap(name).readAllMap();
}
/**
* 验证 是否包含
*
* @param name 名称
* @param key 键
* @return 返回 是否 包含
*/
public boolean containsKey(String name, String key) {
return this.getMap(name).containsKey(key);
}
/**
* 验证 是否包含
*
* @param name 名称
* @param value 值
* @return 返回 是否 包含
*/
public boolean containsValue(String name, String value) {
return this.getMap(name).containsValue(value);
}
}
Redis Set
/**
* @Author: huangyibo
* @Date: 2023/7/12 14:11
* @Description: Redisson Set 工具类
*/
@Component
public class RedissonSetUtil {
@Resource
private RedissonClient redissonClient;
/**
* 获取 字符串 的 RSet
*
* @param name 名称
* @return 返回 值
*/
public RSet<String> getSet(String name) {
RSet<String> rSet = redissonClient.getSet(name, StringCodec.INSTANCE);
Assert.notNull(rSet, "rSet is null");
return rSet;
}
/**
* 新增 set 数据
*
* @param name 名称
* @param value 值
* @return 返回 是否新增成功
*/
public boolean set(String name, String value) {
return this.getSet(name).add(value);
}
/**
* 新增 set 数据
*
* @param name 名称
* @param values 值
* @return 返回 是否新增成功
*/
public boolean set(String name, List<String> values) {
return this.getSet(name).addAll(values);
}
/**
* 读取 set 的数据
*
* @param name 名称
* @return 返回 是否新增成功
*/
public Set<String> readAll(String name) {
return this.getSet(name).readAll();
}
/**
* 验证 是否包含
*
* @param name 名称
* @param value 值
* @return 返回 是否 包含
*/
public boolean contains(String name, String value) {
return this.getSet(name).contains(value);
}
/**
* 验证 是否包含
*
* @param name 名称
* @param values 值
* @return 返回 是否 包含
*/
public boolean containsAll(String name, List<String> values) {
return this.getSet(name).containsAll(values);
}
}
Redis ZSet
/**
* @Author: huangyibo
* @Date: 2023/7/12 12:41
* @Description: Redisson ZSet 工具类
*/
@Component
@Slf4j
public class RedissonZSetUtil {
@Resource
private RedissonClient client;
public static final String DEFAULT_SCORE_KEY = "default";
/**
* 默认保存时间
*/
private static final long DEFAULT_EXPIRE_TIME_SECONDS = 3600L;
/**
* 新增ZSet元素,存在则刷新
*
* @param refreshExpire 过期时间,不为null则重新赋值
*/
public <T> void zscoreAddAsync(String key, double score, T member, Long refreshExpire) {
RScoredSortedSet<Object> scoredSortedSet = client.getScoredSortedSet(key);
if (null != refreshExpire) {
scoredSortedSet.expire(Duration.ofSeconds(DEFAULT_EXPIRE_TIME_SECONDS));
}
scoredSortedSet.addAsync(score, member);
}
/**
* 批量新增
*/
public <T> void zScoreAddAsyncBatch(String key, Map<String, Double> map, long seconds) {
if (seconds <= 0) {
seconds = DEFAULT_EXPIRE_TIME_SECONDS;
}
RScoredSortedSet<Object> scoredSortedSet = client.getScoredSortedSet(key);
// 只能针对 key 设置过期时间,zset 中的元素不能单独设置.
scoredSortedSet.add(0, DEFAULT_SCORE_KEY);
scoredSortedSet.expire(Duration.ofSeconds(seconds));
RBatch batch = client.createBatch();
map.forEach((member, score) -> {
batch.getScoredSortedSet(key).addAsync(score, member);
});
batch.execute();
}
/**
* 读取指定 key 下所有 member, 按照 score 升序(默认)
*/
public Collection<Object> getZSetMembers(String key, int startIndex, int endIndex) {
RScoredSortedSet<Object> scoredSortedSet = client.getScoredSortedSet(key);
return scoredSortedSet.valueRange(startIndex, endIndex);
}
/**
* 取指定 key 下所有 member, 按照 score 降序
*/
public Collection<Object> getZSetMembersReversed(String key, int startIndex, int endIndex) {
RScoredSortedSet<Object> scoredSortedSet = client.getScoredSortedSet(key);
return scoredSortedSet.valueRangeReversed(startIndex, endIndex);
}
/**
* 读取 member和score, 按照 score 升序(默认)
*/
public Collection<ScoredEntry<Object>> getZSetEntryRange(String key, int startIndex, int endIndex) {
RScoredSortedSet<Object> scoredSortedSet = client.getScoredSortedSet(key);
return scoredSortedSet.entryRange(startIndex, endIndex);
}
/**
* 读取 member和score, 按照 score 降序
*/
public Collection<ScoredEntry<Object>> getZSetEntryRangeReversed(String key, int startIndex, int endIndex) {
RScoredSortedSet<Object> scoredSortedSet = client.getScoredSortedSet(key);
return scoredSortedSet.entryRangeReversed(startIndex, endIndex);
}
/**
* 读取指定 key 下 member 的 score
* 返回null 表示不存在
*/
public Double getZSetMemberScore(String key, String member) {
RScoredSortedSet<Object> scoredSortedSet = client.getScoredSortedSet(key);
if (!scoredSortedSet.isExists()) {
return null;
}
return scoredSortedSet.getScore(member);
}
/**
* 读取指定 key 下 memberList 的 score
* 返回null 表示不存在
*/
public Double getZSetMemberScore(String key, List<String> memberList) {
RScoredSortedSet<Object> scoredSortedSet = client.getScoredSortedSet(key);
if (!scoredSortedSet.isExists()) {
return null;
}
return scoredSortedSet.getScore(memberList);
}
/**
* 读取指定 key 下 member 的 rank 排名(升序情况)
* 返回null 表示不存在, 下标从0开始
*/
public Integer getZSetMemberRank(String key, String member) {
RScoredSortedSet<Object> scoredSortedSet = client.getScoredSortedSet(key);
if (!scoredSortedSet.isExists()) {
return null;
}
return scoredSortedSet.rank(member);
}
/**
* 异步删除指定 ZSet 中的指定 memberName 元素
*/
public void removeZSetMemberAsync(String key, String memberName) {
RScoredSortedSet<Object> scoredSortedSet = client.getScoredSortedSet(key);
if (!scoredSortedSet.isExists()) {
return;
}
scoredSortedSet.removeAsync(memberName);
}
/**
* 异步批量删除指定 ZSet 中的指定 member 元素列表
*/
public void removeZSetMemberAsync(String key, List<String> memberList) {
RScoredSortedSet<Object> scoredSortedSet = client.getScoredSortedSet(key);
if (!scoredSortedSet.isExists()) {
return;
}
RBatch batch = client.createBatch();
memberList.forEach(member -> batch.getScoredSortedSet(key).removeAsync(member));
batch.execute();
}
/**
* 统计ZSet分数范围内元素总数. 区间包含分数本身
* 注意这里不能用 -1 代替最大值
*/
public int getZSetCountByScoresInclusive(String key, double startScore, double endScore) {
RScoredSortedSet<Object> scoredSortedSet = client.getScoredSortedSet(key);
if (!scoredSortedSet.isExists()) {
return 0;
}
return scoredSortedSet.count(startScore, true, endScore, true);
}
/**
* 返回ZSet分数范围内 member 列表. 区间包含分数本身.
* 注意这里不能用 -1 代替最大值
*/
public Collection<Object> getZSetMembersByScoresInclusive(String key, double startScore, double endScore) {
RScoredSortedSet<Object> scoredSortedSet = client.getScoredSortedSet(key);
if (!scoredSortedSet.isExists()) {
return null;
}
return scoredSortedSet.valueRange(startScore, true, endScore, true);
}
/**
* 获取所有的指定前缀 keys
*/
public Set<String> getKeys(String prefix) {
Iterable<String> keysByPattern = client.getKeys().getKeysByPattern(prefix);
Set<String> keys = new HashSet<>();
for (String s : keysByPattern) {
keys.add(s);
}
return keys;
}
}
Redis ScoreSortedSet
/**
* @Author: huangyibo
* @Date: 2023/7/12 14:14
* @Description: Redisson ScoreSortedSet 工具类
*/
@Component
public class RedissonScoreSortedSetUtil {
@Resource
private RedissonClient redissonClient;
/**
* 获取 字符串 的 RScoredSortedSet
*
* @param key 名称
* @return 返回 值
*/
public RScoredSortedSet<String> getScoredSortedSet(String key) {
RScoredSortedSet<String> scoredSortedSet = redissonClient.getScoredSortedSet(key, StringCodec.INSTANCE);
Assert.notNull(scoredSortedSet, "scoredSortedSet is null");
return scoredSortedSet;
}
/**
* 新增 set 数据
*
* @param key 名称
* @param score 分数
* @param value 值
* @return 返回 是否新增成功
*/
public boolean set(String key, Double score, String value) {
return this.getScoredSortedSet(key).add(score, value);
}
/**
* 新增 set 数据
*
* @param key 名称
* @param valueMap 值
* @return 返回 是否新增成功
*/
public int set(String key, Map<String, Double> valueMap) {
return this.getScoredSortedSet(key).addAll(valueMap);
}
/**
* 读取 set 的数据
*
* @param key 名称
* @param startIndex 开始索引
* @param endIndex 结束索引
* @return 返回 是否新增成功
*/
public Collection<ScoredEntry<String>> entryRange(String key, int startIndex, int endIndex) {
return this.getScoredSortedSet(key).entryRange(startIndex, endIndex);
}
/**
* 验证 是否包含
*
* @param key 名称
* @param value 值
* @return 返回 是否 包含
*/
public boolean contains(String key, String value) {
return this.getScoredSortedSet(key).contains(value);
}
/**
* 验证 是否包含
*
* @param key 名称
* @param values 值
* @return 返回 是否 包含
*/
public boolean containsAll(String key, List<String> values) {
return this.getScoredSortedSet(key).containsAll(values);
}
}
Redis SortedSet
/**
* @Author: huangyibo
* @Date: 2023/7/12 14:16
* @Description: Redisson SortedSet 工具类
*/
@Component
public class RedisSortedSetUtil {
@Resource
private RedissonClient redissonClient;
/**
* 获取 字符串 的 RSortedSet
*
* @param key 名称
* @return 返回 值
*/
public RSortedSet<String> getSortedSet(String key) {
RSortedSet<String> sortedSet = redissonClient.getSortedSet(key, StringCodec.INSTANCE);
Assert.notNull(sortedSet, "sortedSet is null");
return sortedSet;
}
/**
* 新增 set 数据
*
* @param key 名称
* @param value 值
* @return 返回 是否新增成功
*/
public boolean set(String key, String value) {
return this.getSortedSet(key).add(value);
}
/**
* 新增 set 数据
*
* @param key 名称
* @param values 值
* @return 返回 是否新增成功
*/
public boolean set(String key, List<String> values) {
return this.getSortedSet(key).addAll(values);
}
/**
* 读取 set 的数据
*
* @param key 名称
* @return 返回 是否新增成功
*/
public Collection<String> readAll(String key) {
return this.getSortedSet(key).readAll();
}
/**
* 验证 是否包含
*
* @param key 名称
* @param value 值
* @return 返回 是否 包含
*/
public boolean contains(String key, String value) {
return this.getSortedSet(key).contains(value);
}
/**
* 验证 是否包含
*
* @param key 名称
* @param values 值
* @return 返回 是否 包含
*/
public boolean containsAll(String key, List<String> values) {
return this.getSortedSet(key).containsAll(values);
}
}
Redis BitMap
/**
* @Author: huangyibo
* @Date: 2023/7/12 14:12
* @Description: Redisson BitMap 工具类
*/
@Component
public class RedissonBitMapUtil {
@Resource
private RedissonClient redissonClient;
/**
* 获取 字符串 的 RSet
*
* @param name 名称
* @return 返回 值
*/
public RBitSet getBitSet(String name) {
RBitSet bitSet = redissonClient.getBitSet(name);
Assert.notNull(bitSet, "bitSet is null");
return bitSet;
}
/**
* 新增 set 数据
*
* @param name 名称
* @param bitIndex bit的索引,值 1/true
* @return 返回 是否新增成功
*/
public boolean set(String name, long bitIndex) {
return this.getBitSet(name).set(bitIndex);
}
/**
* 新增 set 数据
*
* @param name 名称
* @param fromIndex bit的索引,值 1/true
* @param toIndex bit的索引,值 1/true
* @return 返回 是否新增成功
*/
public void set(String name, long fromIndex, long toIndex) {
this.getBitSet(name).set(fromIndex, toIndex);
}
/**
* 新增 set 数据
*
* @param name 名称
* @param fromIndex bit的索引
* @param toIndex bit的索引
* @param value 值 1/true
* @return 返回 是否新增成功
*/
public void set(String name, long fromIndex, long toIndex, boolean value) {
this.getBitSet(name).set(fromIndex, toIndex, value);
}
/**
* 读取 set 的数据
*
* @param name 名称
* @param bitIndex bit的索引,值 1/true
* @return 返回 是否新增成功
*/
public boolean get(String name, long bitIndex) {
return this.getBitSet(name).get(bitIndex);
}
/**
* 验证 是否包含
*
* @param name 名称
* @param offset 偏移量
* @param increment 自增的值
* @return 返回 是否 包含
*/
public long incrementAndGetLong(String name, long offset, long increment) {
return this.getBitSet(name).incrementAndGetLong(offset, increment);
}
/**
* 验证 是否包含
*
* @param name 名称
* @param offset 偏移量
* @return 返回 是否 包含
*/
public long getLong(String name, long offset) {
return this.getBitSet(name).getLong(offset);
}
}
Redis HyperLoglog
/**
* @Author: huangyibo
* @Date: 2023/7/12 12:53
* @Description: Redisson HyperLoglog 工具类
*/
@Component
public class RedissonHyperLoglogUtil {
@Resource
private RedissonClient redissonClient;
/**
* 获取 RHyperLogLog
*
* @param name 名称
* @return 返回 值
*/
public RHyperLogLog<String> getHyperLogLog(String name) {
RHyperLogLog<String> hyperLogLog = redissonClient.getHyperLogLog(name, StringCodec.INSTANCE);
Assert.notNull(hyperLogLog, "hyperLogLog is null");
return hyperLogLog;
}
/**
* 添加元素
*
* @param name 名称
* @param value 值
* @return 是否 添加成功
*/
public boolean add(String name, String value) {
return this.getHyperLogLog(name).add(value);
}
/**
* 添加元素
*
* @param name 名称
* @param values 值
* @return 是否 添加成功
*/
public boolean addAll(String name, List<String> values) {
return this.getHyperLogLog(name).addAll(values);
}
/**
* 添加元素
*
* @param name 名称
* @return 是否 添加成功
*/
public long count(String name) {
return this.getHyperLogLog(name).count();
}
/**
* 添加元素
*
* @param name 名称
* @param otherLog 其他值
* @return 是否 添加成功
*/
public long count(String name, String... otherLog) {
return this.getHyperLogLog(name).countWith(otherLog);
}
}
参考: https://blog.csdn.net/weixin_51297617/article/details/121184342
https://behappyto.blog.csdn.net/article/details/123340362
https://behappyto.blog.csdn.net/article/details/123340417
https://behappyto.blog.csdn.net/article/details/123340439
https://behappyto.blog.csdn.net/article/details/123389094
https://behappyto.blog.csdn.net/article/details/123389138