@Component public class RedisUtils extends Object
| 限定符和类型 | 字段和说明 |
|---|---|
org.springframework.data.redis.core.RedisTemplate |
redisTemplate
RedisTemplate实例化对象
|
| 构造器和说明 |
|---|
RedisUtils(org.springframework.data.redis.core.RedisTemplate redisTemplate)
通过构造函数注入RedisTemplate
|
| 限定符和类型 | 方法和说明 |
|---|---|
void |
deleteByPrex(String key)
模糊删除,keys*
|
void |
deleteBySuffix(String key)
模糊删除,*keys
|
void |
deleteObject(Collection collection)
删除集合对象
|
void |
deleteObject(String key)
删除单个对象
|
Boolean |
expire(String key,
long timeout,
TimeUnit unit)
设置过期时间
|
Boolean |
expireAt(String key,
Date date)
设置过期时间
|
int |
getCacheInt(String key)
获得缓存的基本对象。
|
<T> List<T> |
getCacheList(String key)
获得缓存的list对象
|
<T> Map<String,T> |
getCacheMap(String key)
获得缓存的Map
|
<T> Set<T> |
getCacheSet(String key)
获得缓存的set
|
long |
getExpire(String key,
TimeUnit timeUnit)
获取剩余的过期时间(指定时间单位)
|
long |
getExpireMin(String key)
获取剩余的过期时间(分钟)
|
long |
getExpireSeconds(String key)
获取剩余的过期时间(秒)
|
<T> List<T> |
getListString(String key,
Class<T> clazz)
获得缓存的list对象
|
<T> T |
getObject(String key)
获得缓存的基本对象。
|
boolean |
hashKeys(String key)
判断key是否存在
|
Long |
incrBy(String key,
long increment)
增加(自增长), 负数则为自减
|
Collection<String> |
keys(String pattern)
获得缓存的基本对象列表
|
Set<String> |
keysPrex(String keyPrefix)
获取指定前缀的一系列key
使用scan命令代替keys, Redis是单线程处理,keys命令在KEY数量较多时,
操作效率极低【时间复杂度为O(N)】,该命令一旦执行会严重阻塞线上其它命令的正常请求
|
Boolean |
move(String key,
int dbIndex)
将当前数据库的 key 移动到给定的数据库 db 当中
|
Boolean |
persist(String key)
移除 key 的过期时间,key 将持久保持
|
String |
randomKey()
从当前数据库中随机返回一个 key
|
boolean |
removeAll(String keyPrefix)
删除指定前缀的一系列key
|
void |
rename(String oldKey,
String newKey)
修改 key 的名称
|
Boolean |
renameIfAbsent(String oldKey,
String newKey)
仅当 newkey 不存在时,将 oldKey 改名为 newkey
|
<T> org.springframework.data.redis.core.ValueOperations<String,T> |
resetCacheData(String key,
T value)
缓存对象数据,指定时间
|
<T> org.springframework.data.redis.core.ValueOperations<String,T> |
resetCacheData(String key,
T value,
long minutes)
缓存对象数据,指定时间
|
Set<String> |
sDifference(String key,
Collection<String> otherKeys)
获取key集合与多个集合的差集
|
Long |
sDifference(String key,
Collection<String> otherKeys,
String destKey)
key与多个key的差集存储到destKey中
|
Set<String> |
sDifference(String key,
String otherKey)
获取两个集合的差集
|
Long |
sDifference(String key,
String otherKey,
String destKey)
key集合与otherKey集合的差集存储到destKey中
|
<T> org.springframework.data.redis.core.ValueOperations<String,T> |
setCacheData(String key,
T value,
long minutes)
缓存对象数据,指定时间(分钟)
|
<T> org.springframework.data.redis.core.ListOperations<String,T> |
setCacheList(String key,
List<T> dataList)
缓存List数据(Redis本身的List)
|
<T> org.springframework.data.redis.core.HashOperations<String,String,T> |
setCacheMap(String key,
Map<String,T> dataMap)
缓存Map
|
<T> org.springframework.data.redis.core.BoundSetOperations<String,T> |
setCacheSet(String key,
Set<T> dataSet)
缓存Set
|
<T> Object |
setListString(String key,
List<T> list)
缓存List数据
|
<T> org.springframework.data.redis.core.ValueOperations<String,T> |
setObject(String key,
T value)
缓存基本的对象,Integer、String、实体类等
|
<T> org.springframework.data.redis.core.ValueOperations<String,T> |
setObject(String key,
T value,
Integer timeout,
TimeUnit timeUnit)
缓存基本的对象,Integer、String、实体类等,带过期时间
|
Set<String> |
sIntersect(String key,
Collection<String> otherKeys)
获取key集合与多个集合的交集
|
Set<String> |
sIntersect(String key,
String otherKey)
获取两个键值的交集
|
Long |
sIntersectAndStore(String key,
Collection<String> otherKeys,
String destKey)
key集合与多个集合的交集存储到destKey集合中
|
Long |
sIntersectAndStore(String key,
String otherKey,
String destKey)
key集合与otherKey集合的交集存储到destKey集合中
|
Set<String> |
sUnion(String key,
Collection<String> otherKeys)
获取key集合与多个集合的并集
|
Set<String> |
sUnion(String key,
String otherKeys)
获取两个集合的并集
|
Long |
sUnionAndStore(String key,
Collection<String> otherKeys,
String destKey)
key集合与多个集合的并集存储到destKey中
|
Long |
sUnionAndStore(String key,
String otherKey,
String destKey)
key集合与otherKey集合的并集存储到destKey中
|
org.springframework.data.redis.connection.DataType |
type(String key)
返回 key 所储存的值的类型
|
Boolean |
zAdd(String key,
String value,
double score)
添加元素,有序集合是按照元素的score值由小到大排列
|
Double |
zIncrementScore(String key,
String value,
double delta)
增加元素的score值,并返回增加后的值
|
Set<String> |
zRange(String key,
long start,
long end)
获取集合的元素, 从小到大排序
|
Long |
zRank(String key,
Object value)
返回元素在集合的排名,有序集合是按照元素的score值由小到大排列
|
Long |
zReverseRank(String key,
Object value)
返回元素在集合的排名,按元素的score值由大到小排列
|
public org.springframework.data.redis.core.RedisTemplate redisTemplate
public RedisUtils(org.springframework.data.redis.core.RedisTemplate redisTemplate)
redisTemplate - RedisTemplate实例化对象public <T> org.springframework.data.redis.core.ValueOperations<String,T> setObject(String key, T value)
key - 缓存的键value - 缓存的值public <T> org.springframework.data.redis.core.ValueOperations<String,T> setObject(String key, T value, Integer timeout, TimeUnit timeUnit)
key - 缓存的键value - 缓存的值timeout - 时间timeUnit - 时间颗粒度public <T> T getObject(String key)
key - 缓存的键public void deleteObject(String key)
key - redis缓存的键public void deleteObject(Collection collection)
collection - 集合public <T> org.springframework.data.redis.core.ListOperations<String,T> setCacheList(String key, List<T> dataList)
key - 缓存的键dataList - 待缓存的List数据public <T> org.springframework.data.redis.core.BoundSetOperations<String,T> setCacheSet(String key, Set<T> dataSet)
key - 缓存的键dataSet - 缓存的数据public <T> org.springframework.data.redis.core.HashOperations<String,String,T> setCacheMap(String key, Map<String,T> dataMap)
key - redis键dataMap - map数据public int getCacheInt(String key)
key - 缓存的键public Collection<String> keys(String pattern)
pattern - 字符串前缀public <T> org.springframework.data.redis.core.ValueOperations<String,T> setCacheData(String key, T value, long minutes)
key - 缓存的键value - 缓存的值minutes - 分钟public <T> org.springframework.data.redis.core.ValueOperations<String,T> resetCacheData(String key, T value, long minutes)
key - 缓存的键value - 缓存的值minutes - 分钟public <T> org.springframework.data.redis.core.ValueOperations<String,T> resetCacheData(String key, T value)
key - 缓存的键value - 缓存的值minutes - 分钟public boolean hashKeys(String key)
key - redis键public void deleteBySuffix(String key)
key - redis键后缀public void deleteByPrex(String key)
模糊删除,keys*
key - redis键前缀public Set<String> keysPrex(String keyPrefix)
keyPrefix - redis键关键词public boolean removeAll(String keyPrefix)
keyPrefix - redis键关键词public long getExpire(String key, TimeUnit timeUnit)
key - redis键timeUnit - 时间单位public long getExpireMin(String key)
key - redis键public long getExpireSeconds(String key)
key - redis键public Boolean expire(String key, long timeout, TimeUnit unit)
key - redis键timeout - 时间unit - 时间单位public Boolean expireAt(String key, Date date)
key - redis键date - 时间对象public <T> Object setListString(String key, List<T> list)
key - 缓存的键list - 待缓存的List数据public <T> List<T> getListString(String key, Class<T> clazz)
key - 缓存的键clazz - 对象类型public Boolean move(String key, int dbIndex)
key - 缓存的键dbIndex - 新的redis数据库编号public String randomKey()
public void rename(String oldKey, String newKey)
oldKey - 旧的keynewKey - 新的keypublic Boolean renameIfAbsent(String oldKey, String newKey)
oldKey - 旧的keynewKey - 新的keypublic org.springframework.data.redis.connection.DataType type(String key)
key - 缓存的键public Set<String> sIntersect(String key, String otherKey)
key - 缓存的键otherKey - 缓存的另一个键public Set<String> sIntersect(String key, Collection<String> otherKeys)
key - 缓存的键otherKeys - 缓存的另一些键的集合public Long sIntersectAndStore(String key, String otherKey, String destKey)
key - 健otherKeys - 多个健集合destKey - 新的健public Long sIntersectAndStore(String key, Collection<String> otherKeys, String destKey)
key - 健otherKeys - 多个健集合destKey - 新的健public Set<String> sUnion(String key, String otherKeys)
key - 健otherKeys - 多个健集合public Set<String> sUnion(String key, Collection<String> otherKeys)
key - 健otherKeys - 多个健集合public Long sUnionAndStore(String key, String otherKey, String destKey)
key - 健otherKeys - 多个健集合destKey - 新的健public Long sUnionAndStore(String key, Collection<String> otherKeys, String destKey)
key - 健otherKeys - 多个健集合destKey - 新的健public Set<String> sDifference(String key, String otherKey)
key - 健otherKeys - 多个健集合public Set<String> sDifference(String key, Collection<String> otherKeys)
key - 健otherKeys - 多个健集合public Long sDifference(String key, String otherKey, String destKey)
key - 健otherKeys - 多个健集合destKey - 新的健public Long sDifference(String key, Collection<String> otherKeys, String destKey)
key - 健otherKeys - 多个健集合destKey - 新的健public Boolean zAdd(String key, String value, double score)
key - 健value - 值score - score值public Double zIncrementScore(String key, String value, double delta)
key - 健value - 值delta - delta值public Long zRank(String key, Object value)
key - 健value - 值public Long zReverseRank(String key, Object value)
key - 健value - 值Copyright © 2022. All rights reserved.