1.封装简介

1.1封装内容罗列

Redis工具类封装文档 - 图1

2.Key相关操作

2.1Key的删除

序号 方法 描述 注意点
1 public boolean delete(_final Object key)_
- 参数

key值
- 返回值
true:删除成功
false:删除失败 |
| | | public long delete(_final Object… keys) |
- 参数
key值数组
- 返回值
删除key-value对的个数 | | | | public long delete
(final Collection<_?_> keys) |
- 参数
key值集合
- 返回值
删除key-value对的个数 | | | | public long deletePattern
(final Object pattern)_ |
- 参数
正则表达式
- 返回值
删除key-value对的个数 | | | | | | |

2.2Key的修改

2.2.1过期时间相关修改

序号 方法 描述 注意点
public boolean expire(_final Object key, long timeout, TimeUnit unit)_
- 参数

key值
时间值
时间单位
- 返回值
true:设置成功
false:设置失败 | ①key值不存在时设置过期时间会返回false | | | public boolean expireAt(_final Object key, Date date) |
- 参数
key值
日期
- 返回值
true:设置成功
false:设置失败 | 同上 | | | public boolean persist
(final Object key)_ |
- 参数
key值
- 返回值
true:设置成功
false:设置失败 | ①key不存在时,返回false |

2.2.2内容修改

序号 方法 描述 注意点
public void rename(_Object oldKey, Object newKey)_
- 参数

原始key名称、新的key名称
- 返回值
无 | | | | public boolean renameIfAbsent(_Object oldKey, Object newKey) |
- 参数
原始key名称、新的key名称
- 返回值
true:修改成功
false:修改失败 | 返回失败的情况:
①新的key名称在redis中已经存在
②旧的key值不存在 | | | public boolean move
(Object key, int dbIndex)_ |
- 参数
key值,要移向的数据库索引
- 返回值
true:移动成功
false:移动失败 | ①只能将 0 索引的数据库key 移到其他数据库下
②当key值不存或移动到本身所在0索引数据库下时报异常。 |

2.3Key查询

序号 方法 描述 注意点
public boolean existsKey(_final Object key)_
- 参数

key值
- 返回值
true:存在
false:不存在 | | | | public Set<_Object> keys(final Object pattern) |
- 参数
key值
- 返回值
匹配上的key值集合 | 若没有匹配任何key值,则返回空集合 | | | public int count
(final Object key) |
- 参数
key值
- 返回值
索引0数据库的该key的个数 | 个人认为这个方法毫无意义(但是原封装类中存在) | | | public String randomKey
() |
- 参数

- 返回值
随机一个数据库中的key值 | 若无key值,则返回null | | | public DataType type
(String key) |
- 参数
key值
- 返回值
DataType枚举类 | 无key时:NONE | | | public long getExpire
(final Object key) |
- 参数
key值
- 返回值
剩余 n 秒过期 | 永久key:-1
已过期key:-2 | | | public long getExpire
(Object key, TimeUnit unit) _ |
- 参数
key值
时间单位
- 返回值
剩余 n 个指定单位时间 过期 | 若剩余时间小于指定的一个时间单位,则返回0
如:剩余20s,指定单位分钟,则返回0 |

3.String相关操作

3.1添加操作

序号 方法 描述 注意
public boolean vSet(_final Object key, Object value)_
- 参数

key值
value值
- 返回值
true:设置成功
false:失败 | 已存在相同key,覆盖 | | | public boolean vSet(_final Object key, Object value, Long expireTime) |
- 参数
key值
value值
秒数
- 返回值
true:设置成功
false:失败 | | | | public boolean vSet
(final Object key, Object value, Long expireTime, TimeUnit timeUnit) |
- 参数
key值
value值
时间值
时间单位
- 返回值
true:设置成功
false:失败 | | | | public boolean vSet
(final Object key, Object value, Date expireDate) |
- 参数
key值
value值
日期
- 返回值
true:设置成功
false:失败 | | | | public boolean vSetIfAbsent
(final Object key, Object value) |
- 参数
key值
value值
- 返回值
true:设置成功
false:失败 | 如果key值已存在返回false设置失败 | | | public void vMultiSet
(Map<_?, ?_> maps) |
- 参数
map集合(key-value对)
- 返回值
无 | | | | public boolean vMultiSetIfAbsent
(Map<_?, ?_> maps) _ |
- 参数
map集合(key-value对)
- 返回值
true:插入成功
false:插入失败 | 仅当所有key-value对都是未出现过 才执行插入 |

3.2删除操作

string数据结构要删除的话,就是整个key-value对删除,见2.1即可

3.3 修改操作

序号 方法 描述 注意点
public boolean vSetUpdate(_final Object key, Long expireTime)_
- 参数

key值
秒数
- 返回值
true:修改成功
false:修改失败 | 这个是原来Dao的方法,现在可以统一使用Key操作中expire() | | | public boolean vSetUpdate(_final Object key, Long expireTime, TimeUnit timeUnit) |
- 参数
key值
时间值
时间单位
- 返回值
true:修改成功
false:修改失败 | 同上 | | | public boolean vSetUpdate
(final Object key, Date expireDate) |
- 参数
key值
日期
- 返回值
true:修改成功
false:修改失败 | 同上,使用expireAt() | | | public boolean vSetBit
(String key, long offset, boolean value) |
- 参数
key值
偏移量
true:1 false为0
- 返回值
true:修改成功
false:修改失败 |
设置ASCII码, 数字 1 的ASCII码是97, 转为二进制是’00110001’,指定offset=1,value=true则ascii码就是 ‘01110001’就是q了。
用处不大 | | | public void vSetRange(final Object key, Object value, long offset) |
- 参数
key值
value值
偏移量
- 返回值
无 | 若key的原始value为 “a”,若从 offset=1,value=bb,则value为””bb”” | | | public int vAppendValue
(final Object key, String value) | | | | | public long vIncrBy(final Object key, long increment) |
- 参数
key值
增量
- 返回值
修改后的值 | ①当需要执行减操作,传入 负值即可
②执行方法前,需要确保库中的值为整数 | | | public double vIncrByFloat
(final Object key, double increment) _ |
- 参数
key值
增量
- 返回值
修改后的值 | | | | | | |

3.4查询操作

序号 方法 描述 注意点
public Object vGet(_final Object key)_
- 参数

key值
- 返回值
value值 | | | | public Object vGetRange(_final Object key, long start, long end) | | | | | public Object vGetAndSet(final Object key, Object value) |
- 参数
key值
新value值
- 返回值
旧value值 | 若原来并没有key值,则会放回null,并设置这对key-value | | | public boolean vGetBit
(final Object key, long offset) | | | | | public List<_Object_> vMultiGet(Collection<_?_> keys) |
- 参数
key值集合
- 返回值
value值集合 | 若key值结合中又不存在的key,则对应的value集合位置为null | | | public long vSize
(final Object key) _ | | |

4.Hash相关操作

4.1添加操作

序号 方法 描述 注意点
public void hmSet(_Object key, Object hashKey, Object value)_
- 参数

key值
hashKey值
value值
- 返回值
无 | 当重复向一个hashKey中设置value时会覆盖 | | | public boolean hmPutIfAbsent(_Object key, Object hashKey, Object value) |
- 参数
key值
hashKey值
value值
- 返回值
true:设置成功
false:设置失败 | 当hashKey不存在时设置 | | | public void hmSetAll
(Object key, Map<_?, ?_> map)_ |
- 参数
key值
key-value对
- 返回值
空 | | | | | | |

4.2删除操作

序号 方法 描述 注意点
public Object hmDel(_Object key, Object hashKey)_
- 参数

key值
hashKey
- 返回值
删除的个数 | 原dao老方法 | | | public long hmDelete(_Object key, Object… fields)_ |
- 参数
key值
多个hashKey
- 返回值
删除的个数 | | | | | | | | | | | |

4.3修改操作

序号 方法 描述 注意点
public long hmIncrBy(_Object key, Object field, long increment)_
- 参数

key值
hashKey
增量
- 返回值
改变后的结果 | 自减的话,就传负数 | | | public void hmSetIncrement(_Object key, Object hashKey, Long value) |
- 参数
key值
hashKey
增量
- 返回值
| 老方法 | | | public double hmIncrByFloat
(String key, Object field, double increment)_ |
- 参数
key值
hashKey
增量
- 返回值
改变后的结果 | |

4.4查询操作

序号 方法 描述 注意点
public Map<_Object, Object> hmGet(Object key)_
- 参数

key值
- 返回值
key-value对 | | | | public Object hmGet(_Object key, Object hashKey) |
- 参数
key值
hashKey值
- 返回值
value值 | | | | public List
<_Object_> hmMultiGet(Object key, Collection<_?_> fields) |
- 参数
key值
hashKey值的集合
- 返回值
value值集合 |
| | | public boolean hmExists
(Object key, Object field) |
- 参数
key值
hashKey值
- 返回值
true:存在
false:不存在 | | | | public Set
<_Object_> hmKeys(Object key) |
- 参数
key值
- 返回值
hashkey集合 | | | | public long hmSize
(Object key) |
- 参数
key值
- 返回值
hashkey的数量 | | | | public List
<_Object_> hmValues(Object key)_ |
- 参数
key值
- 返回值
hashkey对应的value值的集合 | |

5.List相关操作

5.1添加操作

序号 方法 描述 注意点
public void lLeftPush(_Object key, Object value) _
- 参数

key值
value值
- 返回值
无 | | | | public long lLeftPushIfPresent(_Object key, Object value) |
- 参数
key值
value值
- 返回值
当前list的元素个数 | 若key存在则会push,否则不会push | | | public long lLeftPushAll
(Object key, Object… value) |
- 参数
key值
多个value值
- 返回值
当前list的元素个数 | | | | public long lLeftPushAll
(Object key, Collection<_?_> value) |
- 参数
key值
value值集合
- 返回值
当前list的元素个数 | | | | public void lLeftPush
(Object key, Object value, boolean bool) |
- 参数
key值
value值
是否删除重复元素
- 返回值
无 | | | | public void lRightPush
(Object key, Object value) |
- 参数
key值
value值
是否删除重复元素
- 返回值
无 | | | | public long lRightPushIfPresent
(Object key, Object value) |
- 参数
key值
value值
- 返回值
当前list的元素个数 | | | | public void lRightPush
(Object key, Object value, boolean bool) |
- 参数
key值
value值
是否删除重复元素
- 返回值
无 | | | | public long lRightPushAll
(Object key, Object… value) |
- 参数
key值
多个value值
- 返回值
当前list的元素个数 | | | | public long lRightPushAll
(Object key, Collection<_?_> value) |
- 参数
key值
value值集合
- 返回值
当前list的元素个数 | | | | public long lLeftPush
(Object key, Object pivot, Object value) |
- 参数
key值
在pivot值左侧插入
要插入的value
- 返回值
当前list的元素个数 | pivot寻找时是从左向右找到第一个即可 | | | public long lRightPush
(Object key, Object pivot, Object value) |
- 参数
key值
在pivot值右侧插入
要插入的value
- 返回值
当前list的元素个数 | 同上 | | | public void lSetByIndex
(Object key, long index, Object value)_ |
- 参数
key值
索引位置(0~n-1)
要插入的value
- 返回值
无 | | | | | | |

5.2删除操作

序号 方法 描述 注意点
public Object lLeftPop(_Object key)_
- 参数

key值
- 返回值
value值 | | | | public Object lRightPop(_Object key) |
- 参数
key值
- 返回值
value值 | | | | public Object lBlockLeftPop
(Object key, long timeout, TimeUnit unit) |
- 参数
key值
时间值
时间单位
- 返回值
弹出的值 | 就是当key值不存在会堵塞相应时间,进行等待 | | | public Object lBlockRightPop
(Object key, long timeout, TimeUnit unit) |
- 参数
key值
时间值
时间单位
- 返回值
弹出的值 | | | | public long lRemove
(Object key, long count) |
- 参数
key值
count无用参数
- 返回值
当前list中元素个数 | 老方法、清空所有为null的值,count无用,随便传入一个即可 | | | public long lRemove
(Object key, long count, Object value) |
- 参数
key值
要清除的个数
要清除的元素
- 返回值
当前list中元素个数 | ①count<0,是从右侧开始找
②count>=总重复元素时,清除所有 | | | public long lRemove
(Object key, Object value) |
- 参数
key值
要清除的元素
- 返回值
当前list中元素个数 | 清除所有value元素 | | | public void lTrim
(Object key, long start, long end)_ |
- 参数
key值
开始索引
结束索引
- 返回值
无 | 包含start和end索引的元素。 |

5.3修改操作

序号 方法 描述 注意点
public Object lRightPopAndLeftPush(_Object sourceKey, Object destinationKey)_
- 参数

原list的key值
新list的key值
- 返回值
被操作的值 | 若新list未创建,则会创建 | | | public Object lBlockRightPopAndLeftPush(_Object sourceKey, Object destinationKey, long timeout, TimeUnit unit) _ |
- 参数
原list的key值
新list的key值
阻塞时间值
阻塞时间单位
- 返回值
被操作的值 | 若新list未创建,则会阻塞等待,直到新list被创建 |

5.4查询操作

序号 方法 描述 注意点
public long lSize(_Object key) _
- 参数

key值
- 返回值
元素个数 | | | | public Object lRange(_Object k) |
- 参数
key值
- 返回值
object类型实际是个List | 老方法,没敢改 | | | public List
<_?_> lRange(Object key, long start, long end) |
- 参数
key值
起点
终点
- 返回值
List | 包含start和end | | | public Object lindexFirst
(Object key) |
- 参数
key值
- 返回值
第一个元素值 | | | | public Object lindex
(Object key, long index) |
- 参数
key值
索引位置
- 返回值
该索引的元素值 | | | | public long lLen
(Object key)_ |
- 参数
key值
- 返回值
元素个数 | |

6.Set相关操作

6.1添加操作

序号 方法 描述 注意点
public void sAdd(_Object key, Object value)_
- 参数

key值
value值
- 返回值
无 | 集合无重复元素,设置相同的value后,只会保留一个 | | | public long sAdd(_Object key, Object… values) _ |
- 参数
key值
多个value值
- 返回值
当前集合中的元素个数 | |

6.2删除操作

序号 方法 描述 注意点
public long sRemove(_Object key, Object… values)_
- 参数

key值
多个value值
- 返回值
删除元素个数 | | | | public Object sPop(_Object key)_ |
- 参数
key值
- 返回值
被删除的元素值 | 所被删除的元素是随机的 |

6.3修改操作

序号 方法 描述 注意点
public boolean sMove(_Object key, Object value, Object destKey)_
- 参数

源key值
value值
目标key值
- 返回值
true:移动成功
false:移动失败 | 当目标key未创建时,会自动创建 |

6.4查询操作

序号 方法 描述 注意点
public Set<_Object> sMembers(Object key)_
- 参数

key值
- 返回值
value的set集合 | 若key值不存在,返回的是空集合 | | | public long sSize(_Object key) |
- 参数
key值
- 返回值
set集合的元素个数 | | | | public boolean sIsMember
(Object key, Object value) |
- 参数
key值
value值
- 返回值
true: 是集合元素
false: 不是集合元素 | | | | public Object sRandomMember
(Object key) |
- 参数
key值
- 返回值
set集合中随机一个元素 | | | | public List
<_Object_> sRandomMembers(Object key, long count) |
- 参数
key值
需要获取元素个数
- 返回值
set集合中随机count个元素 | 集合的元素个数不一定要大于count | | | public Set
<_Object_> sDistinctRandomMembers(Object key, long count) |
- 参数
key值
需要获取元素个数
- 返回值
set集合中随机count个不重复元素 | count大于集合总元素个数时,只会将set集合的所有元素返回 | | 交集操作 | | | | | | public Set
<_Object_> sIntersect(Object key, Object otherKey) |
- 参数
A集合key
B集合key
- 返回值
A∩B的结果 | | | | public Set
<_Object_> sIntersect(Object key, Collection<_?_> otherKeys) |
- 参数
A集合key
多个集合key
- 返回值
A∩其他集合的结果 | 注意与其他的key交集,表示为:A∩B∩C∩……. | | | public long sIntersectAndStore
(Object key, Object otherKey, Object destKey) |
- 参数
A集合key
B集合key
结果key
- 返回值
A∩B后的元素个数 | | | | public long sIntersectAndStore
(Object key, Collection<_?_> otherKeys, Object destKey) |
- 参数
A集合key
多个集合key
结果key
- 返回值
A∩其他集合的元素个数 | | | 并集操作 | | | | | | public Set
<_Object_> sUnion(Object key, Object otherKeys) |
- 参数
A集合key
B集合key
- 返回值
A∪B的结果 | | | | public Set
<_Object_> sUnion(Object key, Collection<_?_> otherKeys) |
- 参数
A集合key
多个集合key
- 返回值
A∪其他集合的结果 | | | | public long sUnionAndStore
(Object key, Object otherKey, Object destKey) |
- 参数
A集合key
B集合key
结果key
- 返回值
A∪B后的元素个数 | | | | public long sUnionAndStore
(Object key, Collection<_?_> otherKeys, Object destKey) |
- 参数
A集合key
多个集合key
结果key
- 返回值
A∪其他集合的元素个数 | | | 差集操作 | | | | | | public Set
<_Object_> sDifference(Object key, Object otherKey) |
- 参数
A集合key
B集合key
- 返回值
A-B的结果 | | | | public Set
<_Object_> sDifference(Object key, Collection<_?_> otherKeys) |
- 参数
A集合key
多个集合key
- 返回值
A-其他集合的结果 | 与其他集合的差集:A-B-C-……. | | | public long sDifference
(Object key, Object otherKey, Object destKey) |
- 参数
A集合key
B集合key
结果key
- 返回值
A-B后的元素个数 | | | | public long sDifference
(Object key, Collection<_?_> otherKeys, Object destKey) _ |
- 参数
A集合key
多个集合key
结果key
- 返回值
A-其他集合的元素个数 | |

7.ZSet相关操作

7.1添加操作

序号 方法 描述 注意点
public void zAdd(_Object key, Object value, double scoure)_
- 参数

key值
value值
value值对应的分数
- 返回值
无 | 重复对同一个key的同一个value赋值,后者的score会覆盖前者 | | | public long zAdd(_String key, Set<_TypedTuple_<_?_>> values)_ |
- 参数
key值
value值集合
- 返回值
插入元素个数 | | | | | | |

7.2删除操作

序号 方法 描述 注意点
public long zRemove(_Object key, Object… values)_
- 参数

key值
一或多个value值
- 返回值
删除的个数 | | | | public long zRemoveRange(_Object key, long start, long end) |
- 参数
key值
起始索引
终点索引
- 返回值
删除的个数 | ①删除的元素包含start和end
②元素的索引号是根据分数升序排列的 | | | public long zRemoveRangeByScore
(Object key, double min, double max)_ |
- 参数
key值
最低分
最高分
- 返回值
删除的个数 | | | | | | | | | | | |

7.3修改操作

序号 方法 描述 注意点
public double zIncrementScore(_Object key, Object value, double delta)_
- 参数

key值
value值
增加分数
- 返回值
最后的分数 | |

7.4查询操作

序号 方法 描述 注意点
public Set<_Object> rangeByScore(Object key, double score, double score1)_
- 参数

key值
最小分数
最大分数
- 返回值
在分数范围的元素 | 老方法 | | | public Set<_Object> zRangeByScore(Object key, double min, double max) |
- 参数
key值
最小分数
最大分数
- 返回值
在分数范围的元素的value集合 | ①返回的只是 value值,没分数
②从小到大 | | | public Set
<_Object_> zReverseRangeByScore(Object key, double min, double max) |
- 参数
key值
最小分数
最大分数
- 返回值
在分数范围的元素的value集合 | 从大到小 | | | public Set
<_Object_> zRange(Object key, long start, long end) |
- 参数
key值
起始位置
终点位置
- 返回值
在索引范围的 value集合 | ①从小到大 | | | public Set
<_Object_> zReverseRange(Object key, long start, long end) |
- 参数
key值
起始位置
终点位置
- 返回值
在索引范围的 value集合 | ①从大到小 | | | public Set
<_TypedTuple_<_Object_>> zRangeWithScores(Object key, long start, long end) |
- 参数
key值
起始位置
终点位置
- 返回值
在索引范围的 value和score集合 | 分数从小到大 | | | public Set
<_TypedTuple_<_Object_>> zReverseRangeWithScores(Object key, long start, long end) |
- 参数
key值
起始位置
终点位置
- 返回值
在索引范围的 value和score集合 | 分数从大到小 | | | public Set
<_TypedTuple_<_Object_>> zRangeByScoreWithScores(Object key, double min, double max) |
- 参数
key值
最小分数
最大分数
- 返回值
在分数范围的元素的value和score集合 | 从小到大 | | | public Set
<_TypedTuple_<_Object_>> zReverseRangeByScoreWithScores( Object key, double min, double max) |
- 参数
key值
最小分数
最大分数
- 返回值
在分数范围的元素的value和score集合 | 从大到小 | | | public long zRank
(Object key, Object value) |
- 参数
key值
value值
- 返回值
此value的分数排名 | ①从小到大的排名
②从0开始 | | | public long zReverseRank
(String key, Object value) |
- 参数
key值
value值
- 返回值
此value的分数排名 | ①从大到小的排名 | | | public Set
<_TypedTuple_<_Object_>> zRangeByScoreWithScores(String key, double min, double max, long start, long end) |
- 参数
key值
最小分数
最大分数
起始位置
终点位置
- 返回值
返回value和score | 从小到大排 | | | public Set
<_Object_> zReverseRangeByScore(Object key, double min, double max, long start, long end) |
- 参数
key值
最小分数
最大分数
起始位置
终点位置
- 返回值
返回value和score | 从大到小 | | | public long zCount
(Object key, double min, double max) |
- 参数
key值
最小分数
最大分数
- 返回值
匹配到的数量 | | | | public long zSize
(Object key) _ |
- 参数
key值

  • 返回值
    总value数量 | | | | public long zZCard(_Object key) _ |
    - 参数
    key值

  • 返回值
    总value数量 | | | | public double zScore(_Object key, Object value) |
    - 参数
    key值
    value值
    - 返回值
    该value的分数 | 若没有value值,返回null | | | public long zUnionAndStore
    (Object key, Object otherKey, Object destKey) |
    - 参数
    集合A的key值
    集合B的key值
    准备存储到的key值
    - 返回值
    A∪B的元素个数 | 若key值相同,分数相加 | | | public long zUnionAndStore
    (Object key, Collection<_?_> otherKeys, Object destKey) |
    - 参数
    集合A的key值
    其他集合的key值结合
    准备存储到的key值
    - 返回值
    A∪其他key值的元素个数 | | | | public long zIntersectAndStore
    (Object key, Object otherKey, Object destKey) |
    - 参数
    集合A的key值
    集合B的key值
    准备存储到的key值
    - 返回值
    A∩B的元素个数 | key值相同,分数也是相加的 | | | public long zIntersectAndStore
    (Object key, Collection<_?_> otherKeys, Object destKey)_ |
    - 参数
    集合A的key值
    其他集合的key值结合
    准备存储到的key值
    - 返回值
    A∩其他key值的元素个数 | |