RedisTemplate的List类型详解

RedisTemplate的List类型详解

List类型

  Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)。

索引查询

  通过索引获取列表中的元素

1
redisTemplate.opsForList().index(key, index)

范围查询

  获取列表指定范围内的元素(start开始位置, 0是开始位置,end 结束位置, -1返回所有)

1
redisTemplate.opsForList().range(key, start, end)

放在最前面

  存储在list的头部,添加一个就把它放在最前面的索引处

1
redisTemplate.opsForList().leftPush(key, value)

多值插入

  把多个值存入List中(value可以是多个值,也可以是一个Collection value)

1
redisTemplate.opsForList().leftPushAll(key, value)

存在插入

  List存在的时候再加入

1
redisTemplate.opsForList().leftPushIfPresent(key, value)

指定值前插入

  如果pivot处值存在则在pivot前面添加

1
redisTemplate.opsForList().leftPush(key, pivot, value)

先进先出添加

  按照先进先出的顺序来添加(value可以是多个值,或者是Collection var2)

1
redisTemplate.opsForList().rightPush(key, value) redisTemplate.opsForList().rightPushAll(key, value)

在指定值后插入

  在pivot元素的右边添加值

1
redisTemplate.opsForList().rightPush(key, pivot, value)

指定索引值

  设置指定索引处元素的值

1
redisTemplate.opsForList().set(key, index, value)

移除并获取列表中第一个元素

  移除并获取列表中第一个元素(如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止)

1
redisTemplate.opsForList().leftPop(key) redisTemplate.opsForList().leftPop(key, timeout, unit)

移除并获取列表最后一个元素

  移除并获取列表最后一个元素

1
redisTemplate.opsForList().rightPop(key) redisTemplate.opsForList().rightPop(key, timeout, unit)

将值放入第一个

   从一个队列的右边弹出一个元素并将这个元素放入另一个指定队列的最左边

1
redisTemplate.opsForList().rightPopAndLeftPush(sourceKey, destinationKey) redisTemplate.opsForList().rightPopAndLeftPush(sourceKey, destinationKey, timeout, unit)

删除集合中值等于value的元素

  删除集合中值等于value的元素(index=0, 删除所有值等于value的元素; index>0, 从头部开始删除第一个值等于value的元素; index<0, 从尾部开始删除第一个值等于value的元素)

1
redisTemplate.opsForList().remove(key, index, value)

将List列表进行剪裁

1
redisTemplate.opsForList().trim(key, start, end)

获取当前key的List列表长度

1
redisTemplate.opsForList().size(key)

Set类型

  Redis 的 Set 是 String 类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据。集合对象的编码可以是 intset 或者 hashtable。Redis 中集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1)。集合中最大的成员数为 232 - 1 (4294967295, 每个集合可存储40多亿个成员)。

添加元素

1
redisTemplate.opsForSet().add(key, values)

移除元素(单个值、多个值)

1
redisTemplate.opsForSet().remove(key, values)

删除并且返回一个随机的元素

1
redisTemplate.opsForSet().pop(key)

获取集合的大小

1
redisTemplate.opsForSet().size(key)

判断集合是否包含value

1
redisTemplate.opsForSet().isMember(key, value)

获取两个集合的交集(key对应的无序集合与otherKey对应的无序集合求交集)

1
redisTemplate.opsForSet().intersect(key, otherKey)

获取多个集合的交集(Collection var2)

1
redisTemplate.opsForSet().intersect(key, otherKeys)

key集合与otherKey集合的交集存储到destKey集合中(其中otherKey可以为单个值或者集合)

1
redisTemplate.opsForSet().intersectAndStore(key, otherKey, destKey)

key集合与多个集合的交集存储到destKey无序集合中

1
redisTemplate.opsForSet().intersectAndStore(key, otherKeys, destKey)

获取两个或者多个集合的并集(otherKeys可以为单个值或者是集合)

1
redisTemplate.opsForSet().union(key, otherKeys)

key集合与otherKey集合的并集存储到destKey中(otherKeys可以为单个值或者是集合)

1
redisTemplate.opsForSet().unionAndStore(key, otherKey, destKey)

获取两个或者多个集合的差集(otherKeys可以为单个值或者是集合)

1
redisTemplate.opsForSet().difference(key, otherKeys)

差集存储到destKey中(otherKeys可以为单个值或者集合)

1
redisTemplate.opsForSet().differenceAndStore(key, otherKey, destKey)

随机获取集合中的一个元素

1
redisTemplate.opsForSet().randomMember(key)

获取集合中的所有元素

1
redisTemplate.opsForSet().members(key)

随机获取集合中count个元素

1
redisTemplate.opsForSet().randomMembers(key, count)

获取多个key无序集合中的元素(去重),count表示个数

1
redisTemplate.opsForSet().distinctRandomMembers(key, count)

遍历set类似于Interator(ScanOptions.NONE为显示所有的)

1
redisTemplate.opsForSet().scan(key, options)

本文作者: Xu Yuhuan
本文链接: https://xuyuhuan.com/article/361d3ff9/
版权声明: 转载本博客的文章请注明原始出处和作者,谢谢。