Redis拾遗(二)

书接上文,继续说说redis的基本特性。

列表:

  1. 添加操作:
    1. rpush key value [value ...]
    2. lpush key value [value ...]
  2. 取元素:
    1. lrange key startindex endindex(lrange key 0 -1可取所有元素)
  3. 在元素前或后添加元素:
    1. linsert key before|after pivot value 返回值代表当前命令的长度
  4. 取指定索引下标元素
    1. lindex key index
  5. 获取列表长度
    1. llen key
  6. 删除操作:
    1. lpop key 
    2. rpop key
  7. 删除指定元素
    1. lrem key count value
      • lrem会从列表中查找等于value的元素根据count不同分情况删除:count>0,从左到右删除count个元素,count<0,从右到左删除count个元素,count=0,删除所有。
  8. 按照索引范围修剪列表
    1. ltrim key start end
  9. 修改:
    1. lset key index newValue
  10. 阻塞操作:
    1. blpop key [key ...] timeout
    2. brpop key [key ...] timeout
    3. timeout为阻塞时间,timeout=0则一直阻塞,直到列表中有数据
  11. 使用场景:
    1. 消息队列:lpush+brpop组合实现
    2. 栈:lpush+lpop
    3. 队列:lpush+rpop
    4. 有限集合:lpush+ltrim

集合:

  1. 添加元素:sadd key element [element ...] 返回添加成功的个数
  2. 删除元素:srem key element [element ...] 返回删除成功的个数
  3. 计算元素个数:scard key 时间复杂度:O(1)利用的是Redis内部的变量
  4. 判断元素是否在集合中:sismember key element 
  5. 随机从集合返回指定元素个数:srandmember key [count] count不写默认为1
  6. 从集合随机弹出元素:spop key
  7. 获取所有元素:smembers key
  8. 集合间操作:
    1. 求交集 :sinter key [key ...]
    2. 求并集:sunion key [key ...]
    3. 求差集:sdiff key [key ...]
    4. 将交集、并集、差集结果保存:sinterstore destination key [key ...]  差集并集同理。。
  9. 使用场景:
    1. 兴趣标签

    

有序集合:

猜你喜欢

转载自www.cnblogs.com/fly-to-the-sky/p/9653103.html