package com.flhy.redis.utils;
import jodd.util.StringPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationUtils;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* StringRedisUtil 存在序列化問題,String2RedisUtil 为解决此问题
*/
@Component
public class StringRedisUtil {
@Autowired
private StringRedisTemplate redisTemplate;
public RedisTemplate<String, String> getRedisTemplate() {
return redisTemplate;
}
/**
* 获取指定key的过期时间
*
* @param key 缓存key
*/
public Long getExpire(final String key) {
return redisTemplate.getExpire(key);
}
/**
* 添加到带有 过期时间的 缓存
*
* @param key redis主键
* @param value 值
* @param time 过期时间
* @param timeUnit 过期时间单位
*/
public void setExpire(final String key, final String value, final long time, final TimeUnit timeUnit) {
redisTemplate.opsForValue().set(key, value, time, timeUnit);
}
/**
* 添加 缓存、值并设置过期时间,默认过期单位,秒
*
* @param key
* @param value
* @param time
*/
public void setExpire(final String key, final String value, final long time) {
setExpire(key, value, time, TimeUnit.SECONDS);
}
public void setExpire(final String key, long time) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
/**
* 单独给某个key设置过期时间(缓存中已存在的key)
*
* @param key
* @param time
* @param timeUnit
*/
public void expire(final String key, final long time, final TimeUnit timeUnit) {
redisTemplate.expire(key, time, timeUnit);
}
/**
* 一次性添加数组到 过期时间的 缓存,不用多次连接,节省开销
*
* @param keys the keys
* @param values the values
*/
public void setKeys(final String[] keys, final String[] values) {
//key和value数组之间索引对应
for (int i = 0; i < keys.length; i++) {
redisTemplate.opsForValue().set(keys[i], values[i]);
}
}
public Boolean expireAt(String key, Date date) {
return redisTemplate.expireAt(key, date);
}
/**
* 添加到缓存key-value格式
*
* @param key the key
* @param value the value
*/
public void setKey(final String key, final String value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* 判断key是否存在
*
* @param key 键
* @return true 存在 false不存在
*/
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
return false;
}
}
/**
* 模糊获取keyPatten的所有 key
*
* @param keyPatten the key patten
* @return the set
*/
public Set<String> getKeys(final String keyPatten) {
return redisTemplate.keys(keyPatten + "*");
}
/**
* 根据key获取对象value
*
* @param key the key
* @return the string
*/
public String getValue(final String key) {
return redisTemplate.opsForValue().get(key);
}
/**
* 根据key获取bitMap
*
* @param key the key
* @param offset the offset
* @return Boolean
*/
public Boolean getBit(final String key, final Integer offset) {
return redisTemplate.opsForValue().getBit(key, offset);
}
/**
* 设置key的bitMap
*
* @param key the key
* @param offset the offset
* @return Boolean
*/
public Boolean setBit(final String key, final Integer offset, final boolean value) {
return redisTemplate.opsForValue().setBit(key, offset, value);
}
/**
* 根据key获取对象value
*
* @param key the key
* @return the Object
*/
public Object getObjectValue(final String key) {
return redisTemplate.opsForValue().get(key);
}
/**
* 获取指定key原来的value并重新赋值
*
* @param key
* @param newValue 新value
* @return 旧的value
*/
public String getAndSet(final String key, final String newValue) {
return redisTemplate.opsForValue().getAndSet(key, newValue);
}
/**
* 根据keyList获取所有key的value对象
*
* @param keyList 集合key
* @return
*/
public List<String> multiGet(List<String> keyList) {
return redisTemplate.opsForValue().multiGet(keyList);
}
/**
* 针对hashMap数据结构的相关操作
*
* @return the hash operations
*/
public HashOperations<String, String, String> opsForHash() {
return redisTemplate.opsForHash();
}
/**
* 针对value数据结构的相关操作
*
* @return the hash operations
*/
public ValueOperations<String, String> opsForValue() {
return redisTemplate.opsForValue();
}
/**
* 对HashMap操作
*
* @param key 缓存redis中的key
* @param hashKey Map<key,value>数据结构中的key
* @param hashValue Map<key,value>数据结构中的value
*/
public void putHashValue(String key, String hashKey, String hashValue) {
opsForHash().put(key, hashKey, hashValue);
}
/**
* 获取hashMap结构中指定hashKey对应的hashValue值
*
* @param key 缓存的key
* @param hashKey Map<key,value>数据结构中的key
* @return the hash values
*/
public Object getHashValues(String key, String hashKey) {
return opsForHash().get(key, hashKey);
}
/**
* 删除指定key中hashMap中的若干元素
*
* @param key the key
* @param hashKeys the hash keys
*/
public void delHashValues(String key, String... hashKeys) {
opsForHash().delete(key, hashKeys);
}
/**
* 获取指定key的hashMap数据元素
*
* @param key the key
* @return the hash value
*/
public Map<String, String> getHashValue(String key) {
return opsForHash().entries(key);
}
/**
* 批量添加hashMap数据元素
*
* @param key the key
* @param map the map
*/
public void putHashValues(String key, Map<String, String> map) {
opsForHash().putAll(key, map);
}
/**
* 判断某个主键是否存在
*
* @param key the key
* @return the boolean
*/
public boolean exists(final String key) {
return redisTemplate.hasKey(key);
}
/**
* 删除key
*
* @param keys the keys
* @return the long
*/
public boolean del(final String... keys) {
boolean result = false;
for (String key : keys) {
result = redisTemplate.delete(key);
}
return result;
}
/**
* 对某个主键对应的值加一,value值必须是全数字的字符串
*
* @param key the key
* @return the long
*/
public long incrValue(final String key, final long addValue) {
return redisTemplate.opsForValue().increment(key, addValue);
}
/**
* 对某个主键对应的值加一,value值必须是全数字的字符串
*
* @param key the key
* @return the long
*/
public long incrValueAndExpire(final String key, final long addValue, long time) {
long status = redisTemplate.opsForValue().increment(key, addValue);
redisTemplate.expire(key, time, TimeUnit.SECONDS);
return status;
}
/**
* 对某个主键对应的值减一,value值必须是全数字的字符串
*
* @param key the key
* @return the long
*/
public long decrValue(final String key) {
return redisTemplate.opsForValue().decrement(key);
}
/**
* List数据结构相关操作
*
* @return the list operations
*/
public ListOperations<String, String> opsForList() {
return redisTemplate.opsForList();
}
/**
* redis List数据结构 : 将一个 value 插入到列表 key 的表头
*
* @param key the key
* @param value the value
* @return the long
*/
public Long leftPush(String key, String value) {
return opsForList().leftPush(key, value);
}
/**
* redis List数据结构 : 移除并返回列表 key 的头元素
*
* @param key the key
* @return the string
*/
public String leftPop(String key) {
return opsForList().leftPop(key);
}
/**
* redis List数据结构 :将一个或多个值 value 插入到列表 key 的表尾(最右边)。
*
* @param key the key
* @param value the value
* @return the long
*/
public Long rightPush(String key, String value) {
return opsForList().rightPush(key, value);
}
/**
* redis List数据结构 : 移除并返回列表 key 的末尾元素
*
* @param key the key
* @return the string
*/
public String rightPop(String key) {
return opsForList().rightPop(key);
}
/**
* redis List数据结构 : 返回列表 key 的长度 ;
* 如果 key 不存在,则 key 被解释为一个空列表,返回 0 ;
* 如果 key 不是列表类型,返回一个错误。
*
* @param key the key
* @return the long
*/
public Long listLength(String key) {
return opsForList().size(key);
}
/**
* redis List数据结构:移除指定个数count的value
*
* @param key 要移除元素的key
* @param count 移除个数,count==0,移除list中所有指定value的元素;
* count<0,从表尾向表头移动,移除count绝对值的value元素;
* count>0 ,从表头向表尾移动,移除count的value元素;
* @param value 移除的value
* @return
*/
public Long removeListValues(String key, Long count, String value) {
return opsForList().remove(key, count, value);
}
/**
* redis List数据结构 : 将列表 key 下标为 index 的元素的值设置为 value
*
* @param key the key
* @param index the index
* @param value the value
*/
public void listSetValue(String key, long index, String value) {
opsForList().set(key, index, value);
}
/**
* redis List数据结构 : 返回列表 key 中指定区间内的元素,区间以偏移量 start 和 end 指定。
*
* @param key the key
* @param start the start
* @param end the end
* @return the list
*/
public List<String> getList(String key, int start, int end) {
return opsForList().range(key, start, end);
}
/**
* redis List数据结构 : 批量存储
*
* @param key the key
* @param list the list
* @return the long
*/
public Long leftPushAll(String key, String[] list) {
return opsForList().leftPushAll(key, list);
}
/**
* redis List数据结构 : 将值 value 插入到列表 key 当中,位于值 index之后。
*
* @param key the key
* @param index the index
* @param value the value
*/
public void listInsertValue(String key, long index, String value) {
opsForList().set(key, index, value);
}
/**
* Set数据结构相关操作
*
* @return SetOperations
*/
public SetOperations<String, String> opsForSet() {
return redisTemplate.opsForSet();
}
/**
* 存储set集合类型数据
*
* @param key key值
* @param value value对象
*/
public void setAdd(String key, String... value) {
opsForSet().add(key, value);
}
/**
* 随机获取set集合中元素
*
* @param key key值
* @return
*/
public String randomSetMember(String key) {
return opsForSet().randomMember(key);
}
/**
* 获取set集合
*
* @param key
* @return
*/
public Set<String> getSetMember(String key) {
return opsForSet().members(key);
}
/**
* 移除指定set集合中的元素
*
* @param key 集合key
* @param value 移除元素
*/
public void removeSetMember(String key, String... value) {
redisTemplate.opsForSet().remove(key, value);
}
/**
* 获取set集合的大小
*
* @param key
* @return
*/
public Long getSetSize(String key) {
return redisTemplate.opsForSet().size(key);
}
/**
* set里面是否包含成员
*
* @param key
* @param value
* @return
*/
public Boolean isSetMember(String key, String value) {
return redisTemplate.opsForSet().isMember(key, value);
}
/**
* ZSet数据结构相关操作
*
* @return ZSetOperations
*/
public ZSetOperations<String, String> opsForZSet() {
return redisTemplate.opsForZSet();
}
/**
* redis zSet 添加元素
*
* @param key
* @param value
* @param score
*/
public Boolean zSetAdd(String key, String value, double score) {
return opsForZSet().add(key, value, score);
}
/**
* 给指定的成员增加 分数
*
* @param key
* @param value
* @param score
*/
public Double zSetAddScore(String key, String value, double score) {
return opsForZSet().incrementScore(key, value, score);
}
/**
* 根据Score的范围查找值
*
* @param key
* @param min
* @param max
* @return
*/
public Set<String> zSetRangeByScore(String key, double min, double max) {
return opsForZSet().rangeByScore(key, min, max);
}
/**
* 倒叙查询我的排名 从大到小排名第几
*
* @param key
* @param value
* @return
*/
public Long zSetReverseRankByScore(String key, String value) {
return opsForZSet().reverseRank(key, value);
}
/**
* 正叙查询我的排名 从大到小排名第几
*
* @param key
* @param value
* @return
*/
public Long zSetRankByScore(String key, String value) {
return opsForZSet().rank(key, value);
}
/**
* 根据Score的范围删除值
*
* @param key
* @param min
* @param max
*/
public void zSetRemRangeByScore(String key, double min, double max) {
opsForZSet().removeRangeByScore(key, min, max);
}
/**
* 升序删除一定范围的值
*
* @param key
* @param start
* @param end
*/
public void zSetRemoveRange(String key, Long start, Long end) {
opsForZSet().removeRange(key, start, end);
}
/**
* 批量移除
*
* @param key
* @param values
*/
public void zSetRemove(String key, String... values) {
opsForZSet().remove(key, values);
}
/**
* 根据key获取zSet的数量
*
* @param key
*/
public Long zSetCard(String key) {
return opsForZSet().zCard(key);
}
/**
* 根据score倒序范围排列, start, end为偏移量,开始位移和结束位移
*
* @param key
* @param start
* @param end
* @return
*/
public Set<String> zSetReverseRange(String key, Long start, Long end) {
return opsForZSet().reverseRange(key, start, end);
}
/**
* 根据Score升序范围排列, start, end为偏移量,开始位移和结束位移
*
* @param key
* @param start
* @param end
* @return
*/
public Set<String> zSetRange(String key, Long start, Long end) {
return opsForZSet().range(key, start, end);
}
/**
* 修改单个score的值,策略:先删除该score,然后新加该值
*
* @param key
* @param score
*/
public void zSetModifyScoreSet(String key, Double score, String value) {
zSetRemRangeByScore(key, score, score);
zSetAdd(key, value, score);
}
/**
* 判断zSet中是否有值
*
* @param key
* @param value
* @return
*/
public Boolean zSetIsMember(String key, String value) {
Long rank = opsForZSet().rank(key, value);
return rank != null;
}
/**
* 获取某个值对应的score
*
* @param key
* @param value
* @return
*/
public Double zSetScore(String key, String value) {
return opsForZSet().score(key, value);
}
private byte[] rawKey(String key) {
RedisSerializer<String> redisSerializer = (RedisSerializer<String>) redisTemplate.getKeySerializer();
return redisSerializer.serialize(key);
}
private byte[] rawValue(String value, RedisSerializer valueSerializer) {
return valueSerializer.serialize(value);
}
private List deserializeValues(List<byte[]> rawValues, RedisSerializer<Object> valueSerializer) {
if (valueSerializer == null) {
return rawValues;
}
return SerializationUtils.deserialize(rawValues, valueSerializer);
}
private Object deserializeValue(byte[] value, RedisSerializer<Object> valueSerializer) {
if (valueSerializer == null) {
return value;
}
return valueSerializer.deserialize(value);
}
/**
* 增加
*/
public Long hyperAdd(String key, String... value) {
return redisTemplate.opsForHyperLogLog().add(key, value);
}
/**
* 增加
*/
public Long hyperAddAndExpire(String key, long time, String... value) {
long status = redisTemplate.opsForHyperLogLog().add(key, value);
redisTemplate.expire(key, time, TimeUnit.SECONDS);
return status;
}
/**
* 计数
*/
public Long hyperCount(String... key) {
return redisTemplate.opsForHyperLogLog().size(key);
}
/**
* 合并去重
*/
public void hyperMerge(String destKey, String... sourceKey) {
redisTemplate.opsForHyperLogLog().union(destKey, sourceKey);
}
public String executeLua(String lua, String[] key, Object[] val) {
return Optional.ofNullable(redisTemplate.execute(
new DefaultRedisScript<>(lua, Object.class),
Arrays.asList(key),
val)).orElse(StringPool.ZERO).toString();
}
}
RedisUtil
最新推荐文章于 2025-06-16 15:02:52 发布