@Component public class RedisUtils extends Object
| 限定符和类型 | 字段和说明 |
|---|---|
static RedisUtils |
redisUtils |
| 构造器和说明 |
|---|
RedisUtils() |
| 限定符和类型 | 方法和说明 |
|---|---|
static Integer |
append(String key,
String value)
追加到末尾
|
static void |
delete(Collection<String> keys)
批量删除key
|
static void |
delete(String key)
删除key
|
static byte[] |
dump(String key)
序列化key
|
static Boolean |
expire(String key,
long timeout,
TimeUnit unit)
设置过期时间
|
static Boolean |
expireAt(String key,
Date date)
设置过期时间
|
static String |
get(String key)
获取指定 key 的值
|
static String |
getAndSet(String key,
String value)
将给定 key 的值设为 value ,并返回 key 的旧值(old value)
|
static Boolean |
getBit(String key,
long offset)
对 key 所储存的字符串值,获取指定偏移量上的位(bit)
|
static Long |
getExpire(String key)
返回 key 的剩余的过期时间
|
static Long |
getExpire(String key,
TimeUnit unit)
返回 key 的剩余的过期时间
|
static String |
getRange(String key,
long start,
long end)
返回 key 中字符串值的子字符
|
org.springframework.data.redis.core.StringRedisTemplate |
getRedisTemplate() |
static Boolean |
hasKey(String key)
是否存在key
|
static Long |
hDelete(String key,
Object... fields)
删除一个或多个哈希表字段
|
static boolean |
hExists(String key,
String field)
查看哈希表 key 中,指定的字段是否存在
|
static Object |
hGet(String key,
String field)
获取存储在哈希表中指定字段的值
|
static Map<Object,Object> |
hGetAll(String key)
获取所有给定字段的值
|
static Long |
hIncrBy(String key,
Object field,
long increment)
为哈希表 key 中的指定字段的整数值加上增量 increment
|
static Double |
hIncrByFloat(String key,
Object field,
double delta)
为哈希表 key 中的指定字段的整数值加上增量 increment
|
static Set<Object> |
hKeys(String key)
获取所有哈希表中的字段
|
static List<Object> |
hMultiGet(String key,
Collection<Object> fields)
获取所有给定字段的值
|
static void |
hPut(String key,
String hashKey,
String value) |
static void |
hPutAll(String key,
Map<String,String> maps) |
static Boolean |
hPutIfAbsent(String key,
String hashKey,
String value)
仅当hashKey不存在时才设置
|
static org.springframework.data.redis.core.Cursor<Map.Entry<Object,Object>> |
hScan(String key,
org.springframework.data.redis.core.ScanOptions options)
迭代哈希表中的键值对
|
static Long |
hSize(String key)
获取哈希表中字段的数量
|
static List<Object> |
hValues(String key)
获取哈希表中所有值
|
static Long |
incrBy(String key,
long increment)
增加(自增长), 负数则为自减
|
static Double |
incrByFloat(String key,
double increment) |
void |
init() |
static Set<String> |
keys(String pattern)
查找匹配的key
|
static String |
lBLeftPop(String key,
long timeout,
TimeUnit unit)
移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
|
static String |
lBRightPop(String key,
long timeout,
TimeUnit unit)
移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
|
static String |
lBRightPopAndLeftPush(String sourceKey,
String destinationKey,
long timeout,
TimeUnit unit)
从列表中弹出一个值,将弹出的元素插入到另外一个列表中并返回它; 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
|
static String |
lIndex(String key,
long index)
通过索引获取列表中的元素
|
static String |
lLeftPop(String key)
移出并获取列表的第一个元素
|
static Long |
lLeftPush(String key,
String value)
存储在list头部
|
static Long |
lLeftPush(String key,
String pivot,
String value)
如果pivot存在,再pivot前面添加
|
static Long |
lLeftPushAll(String key,
Collection<String> value) |
static Long |
lLeftPushAll(String key,
String... value) |
static Long |
lLeftPushIfPresent(String key,
String value)
当list存在的时候才加入
|
static Long |
lLen(String key)
获取列表长度
|
static List<String> |
lRange(String key,
long start,
long end)
获取列表指定范围内的元素
|
static Long |
lRemove(String key,
long index,
String value)
删除集合中值等于value得元素
|
static String |
lRightPop(String key)
移除并获取列表最后一个元素
|
static String |
lRightPopAndLeftPush(String sourceKey,
String destinationKey)
移除列表的最后一个元素,并将该元素添加到另一个列表并返回
|
static Long |
lRightPush(String key,
String value) |
static Long |
lRightPush(String key,
String pivot,
String value)
在pivot元素的右边添加值
|
static Long |
lRightPushAll(String key,
Collection<String> value) |
static Long |
lRightPushAll(String key,
String... value) |
static Long |
lRightPushIfPresent(String key,
String value)
为已存在的列表添加值
|
static void |
lSet(String key,
long index,
String value)
通过索引设置列表元素的值
|
static void |
lTrim(String key,
long start,
long end)
裁剪list
|
static Boolean |
move(String key,
int dbIndex)
将当前数据库的 key 移动到给定的数据库 db 当中
|
static List<String> |
multiGet(Collection<String> keys)
批量获取
|
static void |
multiSet(Map<String,String> maps)
批量添加
|
static boolean |
multiSetIfAbsent(Map<String,String> maps)
同时设置一个或多个 key-value 对,当且仅当所有给定 key 都不存在
|
static Boolean |
persist(String key)
移除 key 的过期时间,key 将持久保持
|
static String |
randomKey()
从当前数据库中随机返回一个 key
|
static Boolean |
releaseLock(String lockKey,
String clientId)
与 tryLock 相对应,用作释放锁
|
static void |
rename(String oldKey,
String newKey)
修改 key 的名称
|
static Boolean |
renameIfAbsent(String oldKey,
String newKey)
仅当 newkey 不存在时,将 oldKey 改名为 newkey
|
static Long |
sAdd(String key,
String... values)
set添加元素
|
static Set<String> |
sDifference(String key,
Collection<String> otherKeys)
获取key集合与多个集合的差集
|
static Long |
sDifference(String key,
Collection<String> otherKeys,
String destKey)
key集合与多个集合的差集存储到destKey中
|
static Set<String> |
sDifference(String key,
String otherKey)
获取两个集合的差集
|
static Long |
sDifference(String key,
String otherKey,
String destKey)
key集合与otherKey集合的差集存储到destKey中
|
static Set<String> |
sDistinctRandomMembers(String key,
long count)
随机获取集合中count个元素并且去除重复的
|
static void |
set(String key,
String value)
设置指定 key 的值
|
static boolean |
setBit(String key,
long offset,
boolean value)
设置ASCII码, 字符串'a'的ASCII码是97, 转为二进制是'01100001', 此方法是将二进制第offset位值变为value
|
static void |
setEx(String key,
String value,
long timeout,
TimeUnit unit)
将值 value 关联到 key ,并将 key 的过期时间设为 timeout
|
static boolean |
setIfAbsent(String key,
String value)
只有在 key 不存在时设置 key 的值
|
static Set<String> |
setMembers(String key)
获取集合所有元素
|
static void |
setRange(String key,
String value,
long offset)
用 value 参数覆写给定 key 所储存的字符串值,从偏移量 offset 开始
|
void |
setRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate redisTemplate) |
static Set<String> |
sIntersect(String key,
Collection<String> otherKeys)
获取key集合与多个集合的交集
|
static Set<String> |
sIntersect(String key,
String otherKey)
获取两个集合的交集
|
static Long |
sIntersectAndStore(String key,
Collection<String> otherKeys,
String destKey)
key集合与多个集合的交集存储到destKey集合中
|
static Long |
sIntersectAndStore(String key,
String otherKey,
String destKey)
key集合与otherKey集合的交集存储到destKey集合中
|
static Boolean |
sIsMember(String key,
Object value)
判断集合是否包含value
|
static Long |
size(String key)
获取字符串的长度
|
static Boolean |
sMove(String key,
String value,
String destKey)
将元素value从一个集合移到另一个集合
|
static String |
sPop(String key)
移除并返回集合的一个随机元素
|
static String |
sRandomMember(String key)
随机获取集合中的一个元素
|
static List<String> |
sRandomMembers(String key,
long count)
随机获取集合中count个元素
|
static Long |
sRemove(String key,
Object... values)
set移除元素
|
static org.springframework.data.redis.core.Cursor<String> |
sScan(String key,
org.springframework.data.redis.core.ScanOptions options) |
static Long |
sSize(String key)
获取集合的大小
|
static Set<String> |
sUnion(String key,
Collection<String> otherKeys)
获取key集合与多个集合的并集
|
static Set<String> |
sUnion(String key,
String otherKeys)
获取两个集合的并集
|
static Long |
sUnionAndStore(String key,
Collection<String> otherKeys,
String destKey)
key集合与多个集合的并集存储到destKey中
|
static Long |
sUnionAndStore(String key,
String otherKey,
String destKey)
key集合与otherKey集合的并集存储到destKey中
|
static Boolean |
tryLock(String lockKey,
String clientId,
long seconds)
该加锁方法仅针对单实例 Redis 可实现分布式加锁
对于 Redis 集群则无法使用
支持重复,线程安全
|
static org.springframework.data.redis.connection.DataType |
type(String key)
返回 key 所储存的值的类型
|
static Long |
zAdd(String key,
Set<org.springframework.data.redis.core.ZSetOperations.TypedTuple<String>> values) |
static Boolean |
zAdd(String key,
String value,
double score)
添加元素,有序集合是按照元素的score值由小到大排列
|
static Long |
zCount(String key,
double min,
double max)
根据score值获取集合元素数量
|
static Double |
zIncrementScore(String key,
String value,
double delta)
增加元素的score值,并返回增加后的值
|
static Long |
zIntersectAndStore(String key,
Collection<String> otherKeys,
String destKey)
交集
|
static Long |
zIntersectAndStore(String key,
String otherKey,
String destKey)
交集
|
static Set<String> |
zRange(String key,
long start,
long end)
获取集合的元素, 从小到大排序
|
static Set<String> |
zRangeByScore(String key,
double min,
double max)
根据Score值查询集合元素
|
static Set<org.springframework.data.redis.core.ZSetOperations.TypedTuple<String>> |
zRangeByScoreWithScores(String key,
double min,
double max)
根据Score值查询集合元素, 从小到大排序
|
static Set<org.springframework.data.redis.core.ZSetOperations.TypedTuple<String>> |
zRangeByScoreWithScores(String key,
double min,
double max,
long start,
long end) |
static Set<org.springframework.data.redis.core.ZSetOperations.TypedTuple<String>> |
zRangeWithScores(String key,
long start,
long end)
获取集合元素, 并且把score值也获取
|
static Long |
zRank(String key,
Object value)
返回元素在集合的排名,有序集合是按照元素的score值由小到大排列
|
static Long |
zRemove(String key,
Object... values) |
static Long |
zRemoveRange(String key,
long start,
long end)
移除指定索引位置的成员
|
static Long |
zRemoveRangeByScore(String key,
double min,
double max)
根据指定的score值的范围来移除成员
|
static Set<String> |
zReverseRange(String key,
long start,
long end)
获取集合的元素, 从大到小排序
|
static Set<String> |
zReverseRangeByScore(String key,
double min,
double max)
根据Score值查询集合元素, 从大到小排序
|
static Set<String> |
zReverseRangeByScore(String key,
double min,
double max,
long start,
long end) |
static Set<org.springframework.data.redis.core.ZSetOperations.TypedTuple<String>> |
zReverseRangeByScoreWithScores(String key,
double min,
double max)
根据Score值查询集合元素, 从大到小排序
|
static Set<org.springframework.data.redis.core.ZSetOperations.TypedTuple<String>> |
zReverseRangeWithScores(String key,
long start,
long end)
获取集合的元素, 从大到小排序, 并返回score值
|
static Long |
zReverseRank(String key,
Object value)
返回元素在集合的排名,按元素的score值由大到小排列
|
static org.springframework.data.redis.core.Cursor<org.springframework.data.redis.core.ZSetOperations.TypedTuple<String>> |
zScan(String key,
org.springframework.data.redis.core.ScanOptions options) |
static Double |
zScore(String key,
Object value)
获取集合中value元素的score值
|
static Long |
zSize(String key)
获取集合大小
|
static Long |
zUnionAndStore(String key,
Collection<String> otherKeys,
String destKey) |
static Long |
zUnionAndStore(String key,
String otherKey,
String destKey)
获取key和otherKey的并集并存储在destKey中
|
static Long |
zZCard(String key)
获取集合大小
|
public static RedisUtils redisUtils
@PostConstruct public void init()
public void setRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate redisTemplate)
public org.springframework.data.redis.core.StringRedisTemplate getRedisTemplate()
public static Boolean tryLock(String lockKey, String clientId, long seconds)
lockKey - 加锁键clientId - 加锁客户端唯一标识(采用UUID)seconds - 锁过期时间public static Boolean releaseLock(String lockKey, String clientId)
lockKey - 加锁键clientId - 加锁客户端唯一标识(采用UUID)public static void delete(String key)
key - public static void delete(Collection<String> keys)
keys - public static byte[] dump(String key)
key - public static Boolean expire(String key, long timeout, TimeUnit unit)
key - timeout - unit - public static Boolean move(String key, int dbIndex)
key - dbIndex - public static Long getExpire(String key, TimeUnit unit)
key - unit - public static String randomKey()
public static Boolean renameIfAbsent(String oldKey, String newKey)
oldKey - newKey - public static org.springframework.data.redis.connection.DataType type(String key)
key - public static String getRange(String key, long start, long end)
key - start - end - public static String getAndSet(String key, String value)
key - value - public static Boolean getBit(String key, long offset)
key - offset - public static List<String> multiGet(Collection<String> keys)
keys - public static boolean setBit(String key, long offset, boolean value)
key - offset - 位置value - 值,true为1, false为0public static void setEx(String key, String value, long timeout, TimeUnit unit)
key - value - timeout - 过期时间unit - 时间单位, 天:TimeUnit.DAYS 小时:TimeUnit.HOURS 分钟:TimeUnit.MINUTES
秒:TimeUnit.SECONDS 毫秒:TimeUnit.MILLISECONDSpublic static boolean setIfAbsent(String key, String value)
key - value - public static void setRange(String key, String value, long offset)
key - value - offset - 从指定位置开始覆写public static boolean multiSetIfAbsent(Map<String,String> maps)
maps - public static Long incrBy(String key, long increment)
key - increment - public static Double incrByFloat(String key, double increment)
key - increment - public static List<Object> hMultiGet(String key, Collection<Object> fields)
key - fields - public static Boolean hPutIfAbsent(String key, String hashKey, String value)
key - hashKey - value - public static Long hDelete(String key, Object... fields)
key - fields - public static boolean hExists(String key, String field)
key - field - public static Long hIncrBy(String key, Object field, long increment)
key - field - increment - public static Double hIncrByFloat(String key, Object field, double delta)
key - field - delta - public static org.springframework.data.redis.core.Cursor<Map.Entry<Object,Object>> hScan(String key, org.springframework.data.redis.core.ScanOptions options)
key - options - public static List<String> lRange(String key, long start, long end)
key - start - 开始位置, 0是开始位置end - 结束位置, -1返回所有public static Long lLeftPushAll(String key, Collection<String> value)
key - value - public static Long lLeftPushIfPresent(String key, String value)
key - value - public static Long lLeftPush(String key, String pivot, String value)
key - pivot - value - public static Long lRightPushAll(String key, Collection<String> value)
key - value - public static Long lRightPushIfPresent(String key, String value)
key - value - public static Long lRightPush(String key, String pivot, String value)
key - pivot - value - public static void lSet(String key, long index, String value)
key - index - 位置value - public static String lBLeftPop(String key, long timeout, TimeUnit unit)
key - timeout - 等待时间unit - 时间单位public static String lBRightPop(String key, long timeout, TimeUnit unit)
key - timeout - 等待时间unit - 时间单位public static String lRightPopAndLeftPush(String sourceKey, String destinationKey)
sourceKey - destinationKey - public static String lBRightPopAndLeftPush(String sourceKey, String destinationKey, long timeout, TimeUnit unit)
sourceKey - destinationKey - timeout - unit - public static Long lRemove(String key, long index, String value)
key - index - index=0, 删除所有值等于value的元素; index>0, 从头部开始删除第一个值等于value的元素;
index<0, 从尾部开始删除第一个值等于value的元素;value - public static void lTrim(String key, long start, long end)
key - start - end - public static Boolean sMove(String key, String value, String destKey)
key - value - destKey - public static Boolean sIsMember(String key, Object value)
key - value - public static Set<String> sIntersect(String key, String otherKey)
key - otherKey - public static Set<String> sIntersect(String key, Collection<String> otherKeys)
key - otherKeys - public static Long sIntersectAndStore(String key, String otherKey, String destKey)
key - otherKey - destKey - public static Long sIntersectAndStore(String key, Collection<String> otherKeys, String destKey)
key - otherKeys - destKey - public static Set<String> sUnion(String key, String otherKeys)
key - otherKeys - public static Set<String> sUnion(String key, Collection<String> otherKeys)
key - otherKeys - public static Long sUnionAndStore(String key, String otherKey, String destKey)
key - otherKey - destKey - public static Long sUnionAndStore(String key, Collection<String> otherKeys, String destKey)
key - otherKeys - destKey - public static Set<String> sDifference(String key, String otherKey)
key - otherKey - public static Set<String> sDifference(String key, Collection<String> otherKeys)
key - otherKeys - public static Long sDifference(String key, String otherKey, String destKey)
key - otherKey - destKey - public static Long sDifference(String key, Collection<String> otherKeys, String destKey)
key - otherKeys - destKey - public static List<String> sRandomMembers(String key, long count)
key - count - public static Set<String> sDistinctRandomMembers(String key, long count)
key - count - public static org.springframework.data.redis.core.Cursor<String> sScan(String key, org.springframework.data.redis.core.ScanOptions options)
key - options - public static Boolean zAdd(String key, String value, double score)
key - value - score - public static Long zAdd(String key, Set<org.springframework.data.redis.core.ZSetOperations.TypedTuple<String>> values)
key - values - public static Double zIncrementScore(String key, String value, double delta)
key - value - delta - public static Long zRank(String key, Object value)
key - value - public static Long zReverseRank(String key, Object value)
key - value - public static Set<String> zRange(String key, long start, long end)
key - start - 开始位置end - 结束位置, -1查询所有public static Set<org.springframework.data.redis.core.ZSetOperations.TypedTuple<String>> zRangeWithScores(String key, long start, long end)
key - start - end - public static Set<String> zRangeByScore(String key, double min, double max)
key - min - 最小值max - 最大值public static Set<org.springframework.data.redis.core.ZSetOperations.TypedTuple<String>> zRangeByScoreWithScores(String key, double min, double max)
key - min - 最小值max - 最大值public static Set<org.springframework.data.redis.core.ZSetOperations.TypedTuple<String>> zRangeByScoreWithScores(String key, double min, double max, long start, long end)
key - min - max - start - end - public static Set<String> zReverseRange(String key, long start, long end)
key - start - end - public static Set<org.springframework.data.redis.core.ZSetOperations.TypedTuple<String>> zReverseRangeWithScores(String key, long start, long end)
key - start - end - public static Set<String> zReverseRangeByScore(String key, double min, double max)
key - min - max - public static Set<org.springframework.data.redis.core.ZSetOperations.TypedTuple<String>> zReverseRangeByScoreWithScores(String key, double min, double max)
key - min - max - public static Set<String> zReverseRangeByScore(String key, double min, double max, long start, long end)
key - min - max - start - end - public static Long zCount(String key, double min, double max)
key - min - max - public static Double zScore(String key, Object value)
key - value - public static Long zRemoveRange(String key, long start, long end)
key - start - end - public static Long zRemoveRangeByScore(String key, double min, double max)
key - min - max - public static Long zUnionAndStore(String key, String otherKey, String destKey)
key - otherKey - destKey - public static Long zUnionAndStore(String key, Collection<String> otherKeys, String destKey)
key - otherKeys - destKey - public static Long zIntersectAndStore(String key, String otherKey, String destKey)
key - otherKey - destKey - public static Long zIntersectAndStore(String key, Collection<String> otherKeys, String destKey)
key - otherKeys - destKey - Copyright © 2022. All rights reserved.