区块链技术博客
www.b2bchain.cn

Redis 五大数据类型的基本命令使用

这篇文章主要介绍了Redis 五大数据类型的基本命令使用的讲解,通过具体代码实例进行21204 讲解,并且分析了Redis 五大数据类型的基本命令使用的详细步骤与相关技巧,需要的朋友可以参考下https://www.b2bchain.cn/?p=21204

本文实例讲述了2、树莓派设置连接WiFi,开启VNC等等的讲解。分享给大家供大家参考文章查询地址https://www.b2bchain.cn/7039.html。具体如下:

文章目录

    • Docker安装redis
    • redis-benchmark 性能测试
    • 基础知识
    • 五大数据类型
      • Redis-key
      • String(字符串)
      • List(列表)
      • Set(集合)
      • Hash(哈希)
      • Zset(有序集合)

Docker安装redis

  1. 拉取redis镜像

docker pull redis:latest

  1. 运行redis

docker run -itd --name redis -p 6379:6379 redis

  1. 通过 redis-cli 连接测试使用 redis 服务

docker exec -it redis /bin/bash

CAIKE3>docker exec -it redis /bin/bash [email protected]:/data# redis-cli 127.0.0.1:6379> PING PONG 

redis-benchmark 性能测试

[email protected]:/data# redis-benchmark 
# 测试:100个并发连接 100000请求 redis-benchmark -h localhost -p 6379 -c 100 -n 100000 

结果如下:

[email protected]:/data# redis-benchmark -h localhost -p 6379 -c 100 -n 100000 ====== PING_INLINE ======   100000 requests completed in 1.01 seconds   100 parallel clients   3 bytes payload   keep alive: 1   host configuration "save": 3600 1 300 100 60 10000   host configuration "appendonly": no   multi-thread: no  0.00% <= 0.1 milliseconds 0.00% <= 0.2 milliseconds 0.14% <= 0.3 milliseconds 1.47% <= 0.4 milliseconds 49.53% <= 0.5 milliseconds 86.89% <= 0.6 milliseconds 94.19% <= 0.7 milliseconds 96.63% <= 0.8 milliseconds 98.08% <= 0.9 milliseconds 98.93% <= 1.0 milliseconds 99.34% <= 1.1 milliseconds 99.64% <= 1.2 milliseconds 99.77% <= 1.3 milliseconds 99.82% <= 1.4 milliseconds 99.83% <= 1.5 milliseconds 99.85% <= 1.6 milliseconds 99.85% <= 1.7 milliseconds 99.86% <= 1.8 milliseconds 99.87% <= 1.9 milliseconds 99.88% <= 2 milliseconds 99.90% <= 3 milliseconds 99.92% <= 4 milliseconds 99.99% <= 5 milliseconds 100.00% <= 5 milliseconds 98522.17 requests per second  ====== PING_BULK ======   100000 requests completed in 1.03 seconds   100 parallel clients   3 bytes payload   keep alive: 1   host configuration "save": 3600 1 300 100 60 10000   host configuration "appendonly": no   multi-thread: no  99.34% <= 1 milliseconds 99.97% <= 2 milliseconds 100.00% <= 2 milliseconds 96711.80 requests per second  ====== SET ======   100000 requests completed in 1.02 seconds   100 parallel clients   3 bytes payload   keep alive: 1   host configuration "save": 3600 1 300 100 60 10000   host configuration "appendonly": no   multi-thread: no  98.72% <= 1 milliseconds 99.90% <= 2 milliseconds 100.00% <= 2 milliseconds 98425.20 requests per second  ====== GET ======   100000 requests completed in 1.15 seconds   100 parallel clients   3 bytes payload   keep alive: 1   host configuration "save": 3600 1 300 100 60 10000   host configuration "appendonly": no   multi-thread: no  93.00% <= 1 milliseconds 98.74% <= 2 milliseconds 99.79% <= 3 milliseconds 99.94% <= 4 milliseconds 100.00% <= 5 milliseconds 100.00% <= 5 milliseconds 86805.56 requests per second  ====== INCR ======   100000 requests completed in 1.41 seconds   100 parallel clients   3 bytes payload   keep alive: 1   host configuration "save": 3600 1 300 100 60 10000   host configuration "appendonly": no   multi-thread: no  84.55% <= 1 milliseconds 93.73% <= 2 milliseconds 96.63% <= 3 milliseconds 97.85% <= 4 milliseconds 98.33% <= 5 milliseconds 98.82% <= 6 milliseconds 99.18% <= 7 milliseconds 99.43% <= 8 milliseconds 99.61% <= 9 milliseconds 99.69% <= 10 milliseconds 99.75% <= 11 milliseconds 99.82% <= 13 milliseconds 99.87% <= 14 milliseconds 99.93% <= 15 milliseconds 99.98% <= 16 milliseconds 100.00% <= 17 milliseconds 70972.32 requests per second  ====== LPUSH ======   100000 requests completed in 0.98 seconds   100 parallel clients   3 bytes payload   keep alive: 1   host configuration "save": 3600 1 300 100 60 10000   host configuration "appendonly": no   multi-thread: no  99.07% <= 1 milliseconds 100.00% <= 2 milliseconds 100.00% <= 2 milliseconds 102249.49 requests per second  ====== RPUSH ======   100000 requests completed in 0.98 seconds   100 parallel clients   3 bytes payload   keep alive: 1   host configuration "save": 3600 1 300 100 60 10000   host configuration "appendonly": no   multi-thread: no  99.44% <= 1 milliseconds 99.99% <= 2 milliseconds 100.00% <= 2 milliseconds 102459.02 requests per second  ====== LPOP ======   100000 requests completed in 1.07 seconds   100 parallel clients   3 bytes payload   keep alive: 1   host configuration "save": 3600 1 300 100 60 10000   host configuration "appendonly": no   multi-thread: no  96.74% <= 1 milliseconds 99.79% <= 2 milliseconds 99.90% <= 3 milliseconds 99.90% <= 4 milliseconds 99.90% <= 5 milliseconds 99.91% <= 6 milliseconds 99.94% <= 7 milliseconds 100.00% <= 7 milliseconds 93370.68 requests per second  ====== RPOP ======   100000 requests completed in 1.04 seconds   100 parallel clients   3 bytes payload   keep alive: 1   host configuration "save": 3600 1 300 100 60 10000   host configuration "appendonly": no   multi-thread: no  97.61% <= 1 milliseconds 99.64% <= 2 milliseconds 99.87% <= 3 milliseconds 99.97% <= 4 milliseconds 100.00% <= 4 milliseconds 95969.28 requests per second  ====== SADD ======   100000 requests completed in 1.01 seconds   100 parallel clients   3 bytes payload   keep alive: 1   host configuration "save": 3600 1 300 100 60 10000   host configuration "appendonly": no   multi-thread: no  98.97% <= 1 milliseconds 99.91% <= 2 milliseconds 99.96% <= 3 milliseconds 99.97% <= 4 milliseconds 99.99% <= 5 milliseconds 100.00% <= 6 milliseconds 100.00% <= 6 milliseconds 99403.58 requests per second  ====== HSET ======   100000 requests completed in 1.08 seconds   100 parallel clients   3 bytes payload   keep alive: 1   host configuration "save": 3600 1 300 100 60 10000   host configuration "appendonly": no   multi-thread: no  92.35% <= 1 milliseconds 98.18% <= 2 milliseconds 99.59% <= 3 milliseconds 99.88% <= 4 milliseconds 99.91% <= 5 milliseconds 99.92% <= 8 milliseconds 99.99% <= 9 milliseconds 100.00% <= 9 milliseconds 92592.59 requests per second  ====== SPOP ======   100000 requests completed in 1.06 seconds   100 parallel clients   3 bytes payload   keep alive: 1   host configuration "save": 3600 1 300 100 60 10000   host configuration "appendonly": no   multi-thread: no  98.43% <= 1 milliseconds 99.88% <= 2 milliseconds 100.00% <= 2 milliseconds 94428.70 requests per second  ====== ZADD ======   100000 requests completed in 1.14 seconds   100 parallel clients   3 bytes payload   keep alive: 1   host configuration "save": 3600 1 300 100 60 10000   host configuration "appendonly": no   multi-thread: no  92.07% <= 1 milliseconds 98.76% <= 2 milliseconds 99.72% <= 3 milliseconds 99.90% <= 4 milliseconds 99.97% <= 5 milliseconds 99.98% <= 6 milliseconds 99.99% <= 7 milliseconds 100.00% <= 8 milliseconds 87565.68 requests per second  ====== ZPOPMIN ======   100000 requests completed in 1.02 seconds   100 parallel clients   3 bytes payload   keep alive: 1   host configuration "save": 3600 1 300 100 60 10000   host configuration "appendonly": no   multi-thread: no  99.24% <= 1 milliseconds 100.00% <= 2 milliseconds 100.00% <= 2 milliseconds 97943.19 requests per second  ====== LPUSH (needed to benchmark LRANGE) ======   100000 requests completed in 1.01 seconds   100 parallel clients   3 bytes payload   keep alive: 1   host configuration "save": 3600 1 300 100 60 10000   host configuration "appendonly": no   multi-thread: no  98.45% <= 1 milliseconds 99.80% <= 2 milliseconds 99.97% <= 3 milliseconds 99.98% <= 4 milliseconds 100.00% <= 5 milliseconds 100.00% <= 5 milliseconds 99009.90 requests per second  ====== LRANGE_100 (first 100 elements) ======   100000 requests completed in 2.19 seconds   100 parallel clients   3 bytes payload   keep alive: 1   host configuration "save": 3600 1 300 100 60 10000   host configuration "appendonly": no   multi-thread: no  38.81% <= 1 milliseconds 96.56% <= 2 milliseconds 98.46% <= 3 milliseconds 99.21% <= 4 milliseconds 99.50% <= 5 milliseconds 99.61% <= 6 milliseconds 99.62% <= 7 milliseconds 99.67% <= 8 milliseconds 99.78% <= 9 milliseconds 99.88% <= 10 milliseconds 99.91% <= 11 milliseconds 100.00% <= 12 milliseconds 45745.65 requests per second  ====== LRANGE_300 (first 300 elements) ======   100000 requests completed in 4.91 seconds   100 parallel clients   3 bytes payload   keep alive: 1   host configuration "save": 3600 1 300 100 60 10000   host configuration "appendonly": no   multi-thread: no  0.07% <= 1 milliseconds 13.22% <= 2 milliseconds 91.21% <= 3 milliseconds 96.73% <= 4 milliseconds 97.76% <= 5 milliseconds 98.36% <= 6 milliseconds 98.82% <= 7 milliseconds 99.23% <= 8 milliseconds 99.52% <= 9 milliseconds 99.67% <= 10 milliseconds 99.73% <= 11 milliseconds 99.78% <= 12 milliseconds 99.81% <= 13 milliseconds 99.86% <= 14 milliseconds 99.91% <= 15 milliseconds 99.94% <= 16 milliseconds 99.96% <= 17 milliseconds 99.97% <= 18 milliseconds 99.99% <= 19 milliseconds 100.00% <= 19 milliseconds 20354.16 requests per second  ====== LRANGE_500 (first 450 elements) ======   100000 requests completed in 6.28 seconds   100 parallel clients   3 bytes payload   keep alive: 1   host configuration "save": 3600 1 300 100 60 10000   host configuration "appendonly": no   multi-thread: no  0.03% <= 1 milliseconds 0.74% <= 2 milliseconds 39.01% <= 3 milliseconds 93.95% <= 4 milliseconds 98.13% <= 5 milliseconds 99.05% <= 6 milliseconds 99.37% <= 7 milliseconds 99.52% <= 8 milliseconds 99.63% <= 9 milliseconds 99.74% <= 10 milliseconds 99.83% <= 11 milliseconds 99.86% <= 12 milliseconds 99.89% <= 13 milliseconds 99.91% <= 14 milliseconds 99.92% <= 15 milliseconds 99.92% <= 16 milliseconds 99.93% <= 17 milliseconds 99.95% <= 18 milliseconds 99.96% <= 19 milliseconds 99.98% <= 20 milliseconds 99.99% <= 21 milliseconds 100.00% <= 21 milliseconds 15936.25 requests per second  ====== LRANGE_600 (first 600 elements) ======   100000 requests completed in 7.71 seconds   100 parallel clients   3 bytes payload   keep alive: 1   host configuration "save": 3600 1 300 100 60 10000   host configuration "appendonly": no   multi-thread: no  0.02% <= 1 milliseconds 0.22% <= 2 milliseconds 4.87% <= 3 milliseconds 63.60% <= 4 milliseconds 95.25% <= 5 milliseconds 98.17% <= 6 milliseconds 98.78% <= 7 milliseconds 99.11% <= 8 milliseconds 99.34% <= 9 milliseconds 99.46% <= 10 milliseconds 99.57% <= 11 milliseconds 99.66% <= 12 milliseconds 99.74% <= 13 milliseconds 99.81% <= 14 milliseconds 99.87% <= 15 milliseconds 99.91% <= 16 milliseconds 99.95% <= 17 milliseconds 99.96% <= 18 milliseconds 99.97% <= 19 milliseconds 99.98% <= 20 milliseconds 99.99% <= 21 milliseconds 100.00% <= 22 milliseconds 100.00% <= 22 milliseconds 12973.53 requests per second  ====== MSET (10 keys) ======   100000 requests completed in 1.18 seconds   100 parallel clients   3 bytes payload   keep alive: 1   host configuration "save": 3600 1 300 100 60 10000   host configuration "appendonly": no   multi-thread: no  88.55% <= 1 milliseconds 97.72% <= 2 milliseconds 99.04% <= 3 milliseconds 99.34% <= 4 milliseconds 99.55% <= 5 milliseconds 99.64% <= 7 milliseconds 99.64% <= 9 milliseconds 99.69% <= 10 milliseconds 99.87% <= 11 milliseconds 99.90% <= 18 milliseconds 100.00% <= 19 milliseconds 100.00% <= 19 milliseconds 84674.01 requests per second 

redis 性能测试工具可选参数如下所示:

Redis 五大数据类型的基本命令使用

Redis 五大数据类型的基本命令使用
Redis 五大数据类型的基本命令使用

基础知识

redis默认有16个数据库

127.0.0.1:6379> config get databases # 命令行查看数据库数量databases 1) "databases" 2) "16" 

16个数据库为:DB 0~DB 15

默认使用DB 0 ,可以使用select n切换到DB n,dbsize可以查看当前数据库的大小,与key数量相关。

127.0.0.1:6379> SELECT 8     # 切换数据库 DB8 OK 127.0.0.1:6379[8]> DBSIZE     # 查看数据库大小 (integer) 0 # 不同数据库之间 数据是不能互通的,并且dbsize 是根据库中key的个数。 127.0.0.1:6379[8]> SET name kevin OK 127.0.0.1:6379[8]> GET name "kevin" 127.0.0.1:6379[8]> DBSIZE (integer) 1 127.0.0.1:6379[8]> KEYS * 1) "name" 127.0.0.1:6379[8]> SELECT 0 OK 127.0.0.1:6379> DBSIZE (integer) 0 127.0.0.1:6379> GET NAME   # DB0 中并不能获取DB8 中的键值对。 (nil) 

keys * :查看当前数据库中所有的key。

flushdb:清空当前数据库中的键值对。

flushall:清空所有数据库的键值对。

五大数据类型

Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理。它支持字符串、哈希表、列表、集合、有序集合,位图,hyperloglogs等数据类型。内置复制、Lua脚本、LRU收回、事务以及不同级别磁盘持久化功能,同时通过Redis Sentinel提供高可用,通过Redis Cluster提供自动分区。

Redis-key

在redis中无论什么数据类型,在数据库中都是以key-value形式保存,通过进行对Redis-key的操作,来完成对数据库中数据的操作。

  • exists key:判断键是否存在
  • del key:删除键值对
  • move key db:将键值对移动到指定数据库
  • expire key second:设置键值对的过期时间
  • persist key: 取消键值对的过期时间设置
  • type key:查看value的数据类型
127.0.0.1:6379>  keys *    # 查看当前数据库所有key (empty array) 127.0.0.1:6379> set name kevin OK 127.0.0.1:6379> set age 20 OK 127.0.0.1:6379> keys * 1) "age" 2) "name" 127.0.0.1:6379> move age 1   # 将键值对移动到指定数据库 (integer) 1 127.0.0.1:6379> EXISTS age    # 判断键是否存在 (integer) 0    # 不存在 127.0.0.1:6379> SELECT 1 OK 127.0.0.1:6379[1]> EXISTS age (integer) 1   # 存在 127.0.0.1:6379[1]> keys * 1) "age" 127.0.0.1:6379[1]> del age    # 删除键值对  (integer) 1   # 删除个数 127.0.0.1:6379[1]> keys * (empty array) 
127.0.0.1:6379[1]> select 0 OK 127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> set age 20 OK 127.0.0.1:6379> EXPIRE age 15   # 设置键值对的过期时间 (integer) 1 127.0.0.1:6379> ttl age   # 查看key的过期剩余时间 (integer) 12 127.0.0.1:6379> ttl age (integer) 10 127.0.0.1:6379> ttl age (integer) 9 127.0.0.1:6379> 127.0.0.1:6379> ttl age (integer) 6 127.0.0.1:6379> ttl age (integer) 2 127.0.0.1:6379> ttl age (integer) 1 127.0.0.1:6379> ttl age  # -2 表示key过期 (integer) -2 127.0.0.1:6379> get age (nil) 127.0.0.1:6379> keys * (empty array) 127.0.0.1:6379> set name kevin OK 127.0.0.1:6379> ttl name (integer) -1  # -1表示key未设置过期时间 127.0.0.1:6379> type name    # 查看value的数据类型 string 127.0.0.1:6379> EXPIRE name 30 (integer) 1 127.0.0.1:6379> ttl name (integer) 28 127.0.0.1:6379> persist name   # 取消key的过期时间设置 (integer) 1 127.0.0.1:6379> ttl name (integer) -1 

关于TTL命令

Redis的key,通过TTL命令返回key的过期时间,一般来说有3种:

  1. 当前key没有设置过期时间,所以会返回-1.
  2. 当前key有设置过期时间,而且key已经过期,所以会返回-2.
  3. 当前key有设置过期时间,且key还没有过期,故会返回key的正常剩余时间.

重命名RENAMERENAMENX

  1. RENAME key newkey:修改 key 的名称
  2. RENAMENX key newkey:仅当 newkey 不存在时,将 key 改名为 newkey 。

RENAME key newkey的测试:

127.0.0.1:6379> set name kevin OK 127.0.0.1:6379> set name2 cc OK 127.0.0.1:6379> RENAME name name2   # 如果newKey已经存在,则会覆盖 OK 127.0.0.1:6379> keys * 1) "name2" 127.0.0.1:6379> get name2 "kevin" 127.0.0.1:6379> set age1 20 OK 127.0.0.1:6379> RENAME age1 age2 OK 127.0.0.1:6379> get age2 "20" 

RENAMENX key newkey的测试

127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> set name1 kevin OK 127.0.0.1:6379> set name2 cc OK 127.0.0.1:6379> RENAMENX name1 name2   # 如果newKey已经存在,则不会进行rename操作 (integer) 0 127.0.0.1:6379> keys * 1) "name1" 2) "name2" 127.0.0.1:6379> set age1 20 OK 127.0.0.1:6379> RENAMENX age1 age2 (integer) 1 127.0.0.1:6379> keys * 1) "age2" 2) "name1" 3) "name2" 127.0.0.1:6379> get age2 "20" 

String(字符串)

  1. SET key value: 设置指定 key 的值
  2. GET key: 获取指定 key 的值。

这两个命令就不进行测试了。

  1. APPEND key value: 如果 key 已经存在并且是一个字符串, APPEND 命令将指定的 value 追加到该 key 原来值(value)的末尾。
127.0.0.1:6379> SET msg hello OK 127.0.0.1:6379> APPEND msg world   # 如果存在这个key,就在末尾进行append (integer) 10 127.0.0.1:6379> get msg "helloworld" 127.0.0.1:6379> keys * 1) "msg" 127.0.0.1:6379> APPEND msg2 aaa  # 如果不存在,相当于set key (integer) 3 127.0.0.1:6379> keys * 1) "msg" 2) "msg2" 127.0.0.1:6379> get msg2 "aaa" 
  1. DECR/INCR key: 将key中储存的数字值进行加减。只是针对于数值
127.0.0.1:6379> set number 1 OK 127.0.0.1:6379> INCR number (integer) 2 127.0.0.1:6379> get number "2" 127.0.0.1:6379> DECR number (integer) 1 127.0.0.1:6379> get number "1" 127.0.0.1:6379> set msg test OK 127.0.0.1:6379> INCR msg (error) ERR value is not an integer or out of range 
  1. INCRBY/DECRBY key n: 按指定的步长对数值进行加减
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> set number 1 OK 127.0.0.1:6379> INCRBY number 2 (integer) 3 127.0.0.1:6379> get number "3" 127.0.0.1:6379> DECRBY number 2 (integer) 1 127.0.0.1:6379> get number "1" 
  1. INCRBYFLOAT key n: 为数值加上浮点型数值
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> set number 1 OK 127.0.0.1:6379> INCRBYFLOAT number 0.2 "1.2" 127.0.0.1:6379> get number "1.2" 
  1. STRLEN key: 获取key保存值的字符串长度
127.0.0.1:6379> set msg hello OK 127.0.0.1:6379> STRLEN msg (integer) 5 
  1. GETRANGE key start end: 按起止位置获取字符串(闭区间,起止位置都取)
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> set msg "hello world" OK 127.0.0.1:6379> get msg "hello world" 127.0.0.1:6379> GETRANGE msg 3 9 "lo worl" 
  1. SETRANGE key offset value: 用指定的value 替换key中 offset开始的值
127.0.0.1:6379> set msg "hello world" OK 127.0.0.1:6379> SETRANGE msg 2 test (integer) 11 127.0.0.1:6379> get msg "hetestworld" 
  1. GETSET key value: 将给定 key 的值设为value ,并返回 key的旧值(old value)。
127.0.0.1:6379> set msg "hello world" OK 127.0.0.1:6379> GETSET msg test 
  1. SETNX key value: 仅当key不存在时进行set
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> set msg "hello world" OK 127.0.0.1:6379> SETNX msg test (integer) 0 127.0.0.1:6379> get msg "hello world" 127.0.0.1:6379> SETNX msg2 test (integer) 1 127.0.0.1:6379> get msg2 "test" 
  1. SETEX key seconds value: set 键值对并设置过期时间
127.0.0.1:6379> SETEX msg 10 "hello world" OK 127.0.0.1:6379> ttl msg (integer) 7 127.0.0.1:6379> ttl msg (integer) 6 127.0.0.1:6379> ttl msg (integer) 4 127.0.0.1:6379> ttl msg (integer) -2 
  1. MSET key1 value1 [key2 value2..]: 批量set键值对
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> MSET msg1 test1 msg2 test2 msg3 test3 OK 127.0.0.1:6379> get msg1 "test1" 127.0.0.1:6379> get msg2 "test2" 127.0.0.1:6379> get msg3 "test3" 
  1. MSETNX key1 value1 [key2 value2..]: 批量设置键值对,仅当参数中所有的key都不存在时执行.
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> MSETNX msg1 test1 msg2 test2 msg3 test3 (integer) 1 
  1. MGET key1 [key2..]: 批量获取多个key保存的值
127.0.0.1:6379> MGET msg1 msg2 msg3 1) "test1" 2) "test2" 3) "test3" 
  1. PSETEX key milliseconds value 和 SETEX 命令相似,但它以毫秒为单位设置 key 的生存时间
127.0.0.1:6379> PSETEX msg 50000 "hello wolrd" OK 127.0.0.1:6379> ttl msg (integer) 47 127.0.0.1:6379> get msg "hello wolrd" 127.0.0.1:6379> ttl msg (integer) 42 127.0.0.1:6379> ttl msg (integer) 41 127.0.0.1:6379> get msg "hello wolrd" 

List(列表)

  • Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)
  • 一个列表最多可以包含 2^32 - 1 个元素 (4294967295, 每个列表超过40亿个元素)。
  1. LPUSH/RPUSH key value1[value2..]: 从左边/右边向列表中PUSH值(一个或者多个)。 如果 key 不存在,一个空列表会被创建并执行 LPUSH 操作。 当 key 存在但不是列表类型时,返回一个错误。
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> LPUSH list v1 v2 v3 v4 # 从左到右的顺序依次插入到表头,列表的值将是 v4,v3,v2,v1 (integer) 4 127.0.0.1:6379> LRANGE list 0 -1 1) "v4" 2) "v3" 3) "v2" 4) "v1" 127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> LPUSH list v1 (integer) 1 127.0.0.1:6379> LPUSH list v2 (integer) 2 127.0.0.1:6379> LPUSH list v3 (integer) 3 127.0.0.1:6379> LPUSH list v4 (integer) 4 127.0.0.1:6379> LRANGE list 0 -1 # 从左到右遍历list的数据 1) "v4" 2) "v3" 3) "v2" 4) "v1" 
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> RPUSH list v1 v2 v3 v4  #从右到左的顺序依次插入到表头,列表的值将是  v1,v2,v3,v4 (integer) 4 127.0.0.1:6379> LRANGE list 0 -1  # get list ---> 普通的get是无法获取list值的 1) "v1" 2) "v2" 3) "v3" 4) "v4" 
  1. LRANGE key start end: 返回列表中指定区间内的元素,区间以偏移量 start 和 end 指定。 其中 0 表示列表的第一个元素, 1 表示列表的第二个元素,以此类推。 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素。
127.0.0.1:6379> RPUSH list v1 v2 v3 v4 (integer) 4 127.0.0.1:6379> LRANGE list 0 -1  # 获取全部元素 1) "v1" 2) "v2" 3) "v3" 4) "v4" 127.0.0.1:6379> LRANGE list 0 -2 1) "v1" 2) "v2" 3) "v3" 
  1. LPUSHX/RPUSHX key value: 向已存在的列名中push值(一个或者多个),列表不存在时操作无效。
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> LPUSHX list v1  # 列表不存在时操作无效 (integer) 0 127.0.0.1:6379> LRANGE list 0 -1 (empty array) 127.0.0.1:6379> RPUSHX list v2 (integer) 0 127.0.0.1:6379> LRANGE list 0 -1 (empty array) 127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> LPUSH list v1   (integer) 1 127.0.0.1:6379> LPUSHX list v2  # 列表存在时,操作有效 (integer) 2 127.0.0.1:6379> LRANGE list 0 -1 1) "v2" 2) "v1" 
  1. LINSERT key BEFORE|AFTER pivot value: 在列表的元素前或者后插入元素。当指定元素不存在于列表中时,不执行任何操作。当列表不存在时,被视为空列表,不执行任何操作。如果 key 不是列表类型,返回一个错误。
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> LPUSH list v1 v2 v3 v4 (integer) 4 127.0.0.1:6379> LRANGE list 0 -1 1) "v4" 2) "v3" 3) "v2" 4) "v1" 127.0.0.1:6379> LINSERT list after v2 v5   # 在v2元素后插入v5 (integer) 5 127.0.0.1:6379> LRANGE list 0 -1 1) "v4" 2) "v3" 3) "v2" 4) "v5" 5) "v1" 127.0.0.1:6379> LINSERT list before v2 v6    # 在v2元素前插入v6 (integer) 6 127.0.0.1:6379> LRANGE list 0 -1 1) "v4" 2) "v3" 3) "v6" 4) "v2" 5) "v5" 6) "v1" 127.0.0.1:6379> 
  1. LLEN key: 查看列表长度, 返回列表的长度。 如果列表 key 不存在,则 key 被解释为一个空列表,返回 0。 如果 key 不是列表类型,返回一个错误。
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> LPUSH list v1 v2 v3 v4 (integer) 4 127.0.0.1:6379> LLEN list (integer) 4 127.0.0.1:6379> LLEN list1   # 如果key不存在,则返回0 (integer) 0 
  1. LINDEX key index: 通过索引获取列表中的元素。你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> LPUSH list v1 v2 v3 v4 (integer) 4 127.0.0.1:6379> LRANGE list 0 -1 1) "v4" 2) "v3" 3) "v2" 4) "v1" 127.0.0.1:6379> LINDEX list 0 "v4" 127.0.0.1:6379> LINDEX list -1 "v1" 127.0.0.1:6379> LINDEX list -2 "v2" 
  1. LSET key index value 通过索引为元素设值
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> LPUSH list v1 v2 v3 v4 v5 (integer) 5 127.0.0.1:6379> LRANGE list 0 -1 1) "v5" 2) "v4" 3) "v3" 4) "v2" 5) "v1" 127.0.0.1:6379> LSET list 2 v6 OK 127.0.0.1:6379> LRANGE list 0 -1 1) "v5" 2) "v4" 3) "v6" 4) "v2" 5) "v1" 
  1. LPOP/RPOP key: 从最左边/最右边移除值 并返回
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> LPUSH list v1 v2 (integer) 2 127.0.0.1:6379> LPOP list  # 左侧(头部)弹出 "v2" 127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> LPUSH list v1 v2 (integer) 2 127.0.0.1:6379> RPOP list  # 右侧(尾部)弹出 "v1" 
  1. RPOPLPUSH source destination: 将列表的尾部(右)最后一个值弹出,并返回,然后加到另一个列表的头部.
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> LPUSH list v1 v2 v3 v4 (integer) 4 127.0.0.1:6379> LRANGE list 0 -1 1) "v4" 2) "v3" 3) "v2" 4) "v1" 127.0.0.1:6379> RPOPLPUSH list newlist  #  将mylist的最后一个值(k1)弹出,加入到newlist的头部 "v1" 127.0.0.1:6379> LRANGE newlist 0 -1 1) "v1" 
  1. LTRIM key start end: 通过下标截取指定范围内的列表
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> LPUSH list v1 v2 v3 v4 (integer) 4 127.0.0.1:6379> LRANGE list 0 -1   1) "v4" 2) "v3" 3) "v2" 4) "v1" 127.0.0.1:6379> LTRIM list 0 1  # 截取list中的 0~1部分 OK 127.0.0.1:6379> LRANGE list 0 -1 1) "v4" 2) "v3" 
  1. LREM key count value: List中是允许value重复的 count > 0:从头部开始搜索 然后删除指定的value 至多删除count个 count < 0:从尾部开始搜索… count = 0:删除列表中所有的指定value。
127.0.0.1:6379> LPUSH list v1 v2 v3 v4 v4 (integer) 5 127.0.0.1:6379> LREM list 1 v4 (integer) 1 127.0.0.1:6379> LRANGE list 0 -1 1) "v4" 2) "v3" 3) "v2" 4) "v1" 
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> LPUSH list v1 v2 v3 v2 v4 (integer) 5 127.0.0.1:6379> LRANGE list 0 -1 1) "v4" 2) "v2" 3) "v3" 4) "v2" 5) "v1" 127.0.0.1:6379> LREM list -1 v2 (integer) 1 127.0.0.1:6379> LRANGE list 0 -1 1) "v4" 2) "v2" 3) "v3" 4) "v1" 
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> LPUSH list v1 v2 v3 v3 v4 (integer) 5 127.0.0.1:6379> LRANGE list 0 -1 1) "v4" 2) "v3" 3) "v3" 4) "v2" 5) "v1" 127.0.0.1:6379> LREM list 0 v3 (integer) 2 127.0.0.1:6379> LRANGE list 0 -1 1) "v4" 2) "v2" 3) "v1" 
  1. BLPOP/BRPOP key1[key2] timout: 移出并获取列表的第一个/最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> LPUSH mylist k2 k2 k4 k2 k2 k2 (integer) 6 127.0.0.1:6379> LPUSH newlist k1 (integer) 1 127.0.0.1:6379> BLPOP newlist mylist 30  # 从newlist中弹出第一个值,mylist作为候选 1) "newlist" # newlist有值, 所以弹出newlist 2) "k1" 127.0.0.1:6379> BLPOP newlist mylist 30 1) "mylist"   # 由于newlist空了 从mylist中弹出 2) "k2" 127.0.0.1:6379> BLPOP newlist 30 (nil) (30.07s)  # 超时了 127.0.0.1:6379> BLPOP newlist 30  # 我们连接另一个客户端向newlist中push了test, 阻塞被解决。 1) "newlist" 2) "test" (2.86s) 

总结:

  1. list实际上是一个链表
  2. 如果key不存在,则创建新的链表;如果key存在,新增内容
  3. 如果移除了所有值,空链表,也代表不存在
  4. 在两边插入或者改动值,效率最高!修改中间元素,效率相对较低
  5. 应用:消息排队!消息队列(Lpush Rpop),栈(Lpush Lpop)

Set(集合)

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

  1. SADD key member1[member2..]: 向集合中无序增加一个/多个成员
127.0.0.1:6379> LPUSH newlist test (integer) 1 127.0.0.1:6379> SADD myset m1 m2 m3 m4 m4 (integer) 4 
  1. SCARD key: 获取集合的成员数
127.0.0.1:6379> SCARD myset (integer) 4 
  1. SMEMBERS key: 返回集合中所有的成员
127.0.0.1:6379> SMEMBERS myset 1) "m4" 2) "m2" 3) "m3" 4) "m1" 
  1. SISMEMBER key member: 查询member元素是否是集合的成员,如果成员元素是集合的成员,返回 1 。 如果成员元素不是集合的成员,或 key 不存在,返回 0。
127.0.0.1:6379> SISMEMBER myset m1 (integer) 1 127.0.0.1:6379> SISMEMBER myset m5 (integer) 0 
  1. SRANDMEMBER key [count]: 随机返回集合中count个成员,count缺省值为1
127.0.0.1:6379> SRANDMEMBER myset "m3" 127.0.0.1:6379> SRANDMEMBER myset 2 1) "m4" 2) "m2" 
  1. SPOP key [count]: 随机移除并返回集合中count个成员,count缺省值为1
127.0.0.1:6379> SMEMBERS myset 1) "m2" 2) "m4" 3) "m3" 4) "m1" 127.0.0.1:6379> SPOP myset "m4" 127.0.0.1:6379> SPOP myset 2 1) "m2" 2) "m1" 127.0.0.1:6379> SMEMBERS myset 1) "m3" 
  1. SMOVE source destination member: 将source集合的成员member移动到destination集合
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> SADD myset m1 m2 m3 m4 (integer) 4 127.0.0.1:6379> SADD newset m5 m6 (integer) 2 127.0.0.1:6379> SMOVE myset newset m1  # 将myset中m1成员移动到newset集合 (integer) 1 127.0.0.1:6379> SMEMBERS myset 1) "m3" 2) "m2" 3) "m4" 127.0.0.1:6379> SMEMBERS newset 1) "m5" 2) "m1" 3) "m6" 
  1. SREM key member1[member2..]: 移除集合中一个/多个成员
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> SADD myset m1 m2 m3 m4 (integer) 4 127.0.0.1:6379> SREM myset m1 m2  # myset中移除m1, m2元素 (integer) 2 127.0.0.1:6379> SMEMBERS myset 1) "m3" 2) "m4" 
  1. SDIFF key1[key2..]: 返回第一个集合与其他集合之间的差异,也可以认为说第一个集合中独有的元素。不存在的集合 key 将视为空集。
    例如:
key1 = {a,b,c,d} key2 = {c} key3 = {a,c,e} SDIFF key1 key2 key3 = {b,d} 
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> SADD key1 a b c d (integer) 4 127.0.0.1:6379> SADD key2 c (integer) 1 127.0.0.1:6379> SADD key3 a c e (integer) 3 127.0.0.1:6379> SDIFF key1 key2 key3 1) "b" 2) "d" 
  1. SDIFFSTORE destination key1[key2..]: 将给定集合之间的差集存储在指定的集合中。如果指定的集合 key 已存在,则会被覆盖。
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> SADD key1 a b c d (integer) 4 127.0.0.1:6379> SADD key2 c (integer) 1 127.0.0.1:6379> SADD key3 a c e (integer) 3 127.0.0.1:6379> SDIFFSTORE newset key1 key2 key3 (integer) 2 127.0.0.1:6379> SMEMBERS newset 1) "b" 2) "d" 
  1. SINTER key1 [key2..]: 返回给定所有给定集合的交集。 不存在的集合 key 被视为空集。 当给定集合当中有一个空集时,结果也为空集(根据集合运算定律)。
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> SADD setx m1 m2 m4 m6   (integer) 4 127.0.0.1:6379> SADD sety m2 m5 m6 (integer) 3 127.0.0.1:6379> SADD setz m1 m3 m6 (integer) 3 127.0.0.1:6379> SINTER setx sety setz  # 求 setx、sety、setx的交集 1) "m6" 127.0.0.1:6379> SINTER setx sety  # 求setx sety的交集 1) "m2" 2) "m6" 
  1. SINTERSTORE destination key1[key2..]:将给定集合之间的交集存储在指定的集合中。如果指定的集合已经存在,则将其覆盖。
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> SADD setx m1 m2 m4 m6 (integer) 4 127.0.0.1:6379> SADD sety m2 m5 m6 (integer) 3 127.0.0.1:6379> SADD setz m1 m3 m6 (integer) 3 127.0.0.1:6379> SINTERSTORE newset setx sety setz (integer) 1 127.0.0.1:6379> SMEMBERS newset 1) "m6" 
  1. SUNION key1 [key2..]: 返回给定集合的并集。不存在的集合 key 被视为空集。
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> SADD setx m1 m2 m4 m6 (integer) 4 127.0.0.1:6379> SADD sety m2 m5 m6 (integer) 3 127.0.0.1:6379> SADD setz m1 m3 m6 (integer) 3 127.0.0.1:6379> SUNION setx sety setz 1) "m3" 2) "m2" 3) "m5" 4) "m6" 5) "m4" 6) "m1" 127.0.0.1:6379> SUNION setx sety 1) "m2" 2) "m5" 3) "m6" 4) "m4" 5) "m1" 127.0.0.1:6379> 
  1. SUNIONSTORE destination key1 [key2..]:将给定集合的并集存储在指定的集合 destination 中。如果 destination 已经存在,则将其覆盖。
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> SADD setx m1 m2 m4 m6 (integer) 4 127.0.0.1:6379> SADD sety m2 m5 m6 (integer) 3 127.0.0.1:6379> SADD setz m1 m3 m6 (integer) 3 127.0.0.1:6379> SUNIONSTORE newset setx sety (integer) 5 127.0.0.1:6379> SMEMBERS newset 1) "m2" 2) "m5" 3) "m6" 4) "m4" 5) "m1" 
  1. SSCAN KEY [MATCH pattern] [COUNT count]: 在大量数据环境下,使用此命令遍历集合中元素,每次遍历部分

Hash(哈希)

Redis hash 是一个string类型的field和value的映射表,hash特别适合用于存储对象。可以将一个Hash表作为一个对象进行存储,表中存放对象的信息。

  1. HSET key field value: 将哈希表 key 中的字段 field 的值设为 value 。重复设置同一个field会覆盖,返回0
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> HSET studentx name kevin # 将studentx哈希表作为一个对象,设置name为kevin (integer) 1 127.0.0.1:6379> HSET studentx name cai  # 重复设置field进行覆盖,并返回0 (integer) 0 127.0.0.1:6379> HSET studentx age 20 # 设置studentx的age为20 (integer) 1 
  1. HMSET key field1 value1 [field2 value2..]: 同时将多个 field-value (域-值)对设置到哈希表 key 中。
127.0.0.1:6379> HMSET studentx sex 1 tel 123456789 OK 
  1. HSETNX key field value:只有在字段 field 不存在时,设置哈希表字段的值。
127.0.0.1:6379> HSETNX studentx name kkk  # HSETNX 设置已存在的field (integer) 0  # 失败 127.0.0.1:6379> HSETNX studentx email [email protected] (integer) 1  # 成功 
  1. HEXISTS key field:查看哈希表 key 中,指定的字段是否存在。
127.0.0.1:6379> HEXISTS studentx name # name字段在studentx中是否存在 (integer) 1 # 存在 127.0.0.1:6379> HEXISTS studentx addr (integer) 0  # 不存在 
  1. HGET key field value: 获取存储在哈希表中指定字段的值
127.0.0.1:6379> HGET studentx name "cai" 127.0.0.1:6379> HGET studentx age "20" 
  1. HMGET key field1 [field2..]: 获取所有给定字段的值
127.0.0.1:6379> HMGET studentx name age sex email 1) "cai" 2) "20" 3) "1" 4) "[email protected]" 
  1. HGETALL key:获取在哈希表key的所有字段和值
127.0.0.1:6379> HGETALL studentx  1) "name"  2) "cai"  3) "age"  4) "20"  5) "sex"  6) "1"  7) "tel"  8) "123456789"  9) "email" 10) "[email protected]" 
  1. HKEYS key: 获取哈希表key中所有的字段
127.0.0.1:6379> HKEYS studentx 1) "name" 2) "age" 3) "sex" 4) "tel" 5) "email" 
  1. HLEN key: 获取哈希表中字段的数量
127.0.0.1:6379> HLEN studentx (integer) 5 
  1. HVALS key: 获取哈希表中所有值
127.0.0.1:6379> HVALS studentx 1) "cai" 2) "20" 3) "1" 4) "123456789" 5) "[email protected]" 
  1. HDEL key field1 [field2..]: 删除哈希表key中一个/多个field字段
127.0.0.1:6379> HDEL studentx name age (integer) 2 127.0.0.1:6379> HGETALL studentx 1) "sex" 2) "1" 3) "tel" 4) "123456789" 5) "email" 6) "[email protected]" 
  1. HINCRBY key field n: 为哈希表 key 中的指定字段的整数值加上增量n,并返回增量后结果 一样只适用于整数型字段。
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> HSET studentx age 11 (integer) 1 127.0.0.1:6379> HSET studentx name kevin (integer) 1 127.0.0.1:6379> HINCRBY studentx age 1 (integer) 12 127.0.0.1:6379> HINCRBY studentx name 1 (error) ERR hash value is not an integer 
  1. HINCRBYFLOAT key field n: 为哈希表 key 中的指定字段的浮点数值加上增量 n。
127.0.0.1:6379> HSET studentx weight 50 (integer) 1 127.0.0.1:6379> HINCRBYFLOAT studentx weight 0.6 "50.6" 
  1. HSCAN key cursor [MATCH pattern] [COUNT count]:迭代哈希表中的键值对。

Zset(有序集合)

不同的是每个元素都会关联一个double类型的分数(score)。redis正是通过分数来为集合中的成员进行从小到大的排序。

score相同:按字典顺序排序

有序集合的成员是唯一的,但分数(score)却可以重复。

  1. ZADD key score member1 [score2 member2]: 向有序集合添加一个或多个成员,或者更新已存在成员的分数
127.0.0.1:6379> ZADD myzset 1 m1 2 m2 3 m3 (integer) 3 
  1. ZCARD key: 获取有序集合的成员数
127.0.0.1:6379> ZCARD myzset (integer) 3 
  1. ZCOUNT key min max:计算在有序集合中指定区间score的成员数
127.0.0.1:6379> ZCOUNT myzset 0 1  # 获取score在 [0,1]区间的成员数量 (integer) 1 127.0.0.1:6379> ZCOUNT myzset 0 2  # 获取score在 [0,2]区间的成员数量 (integer) 2 
  1. ZINCRBY key n member:有序集合中对指定成员的分数加上增量 n
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> ZADD myzset 1 m1 2 m2 3 m3 (integer) 3 127.0.0.1:6379> ZINCRBY myzset 1 m1 "2" 
  1. ZSCORE key member: 返回有序集中,成员的分数值
127.0.0.1:6379> ZSCORE myzset m1 "2" 
  1. ZRANK key member: 返回有序集合中指定成员的索引
127.0.0.1:6379> ZRANK myzset m1 (integer) 0 127.0.0.1:6379> ZRANK myzset m2 (integer) 1 
  1. ZRANGE key start end: 通过索引区间返回有序集合成指定区间内的成员
127.0.0.1:6379> ZRANGE myzset 0 -1 # 获取全部成员 1) "m1" 2) "m2" 3) "m3" 127.0.0.1:6379> ZRANGE myzset 0 1   # 获取索引在 0~1的成员 1) "m1" 2) "m2" 
  1. ZRANGEBYLEX key min max:通过字典区间返回有序集合的成员
127.0.0.1:6379> ZRANGEBYLEX myzset - + 1) "m1" 2) "m2" 3) "m3" 127.0.0.1:6379> ZRANGEBYLEX myzset - + LIMIT 0 2 1) "m1" 2) "m2" 127.0.0.1:6379> ZRANGEBYLEX myzset - + LIMIT 1 2 1) "m2" 2) "m3" 
  1. ZRANGEBYSCORE key min max:通过分数返回有序集合指定区间内的成员
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> ZADD myzset 1 m1 2 m2 3 m3 (integer) 3 127.0.0.1:6379> ZRANGEBYSCORE myzset 1 3 1) "m1" 2) "m2" 3) "m3" 127.0.0.1:6379> ZRANGEBYSCORE myzset 1  4 1) "m1" 2) "m2" 3) "m3" 127.0.0.1:6379> ZRANGEBYSCORE myzset 2  4 1) "m2" 2) "m3" 127.0.0.1:6379> ZRANGEBYSCORE myzset -inf +inf 1) "m1" 2) "m2" 3) "m3" 127.0.0.1:6379> ZRANGEBYSCORE myzset -inf +inf WITHSCORES 1) "m1" 2) "1" 3) "m2" 4) "2" 5) "m3" 6) "3" 
  1. ZLEXCOUNT key min max: 在有序集合中计算指定字典区间内成员数量
127.0.0.1:6379> ZLEXCOUNT myzset - + (integer) 3 127.0.0.1:6379> ZLEXCOUNT myzset [m2 [m3 (integer) 2 127.0.0.1:6379> ZLEXCOUNT myzset [m1 [m3 (integer) 3 127.0.0.1:6379> ZLEXCOUNT myzset (m1 [m3 (integer) 2 127.0.0.1:6379> ZLEXCOUNT myzset (m1 (m3 (integer) 1 
  1. ZREM key member1 [member2..]: 移除有序集合中一个/多个成员
127.0.0.1:6379> ZREM myzset m1 m2 (integer) 2 127.0.0.1:6379> ZCARD myzset (integer) 1 127.0.0.1:6379> ZRANGE myzset 0 -1 1) "m3" 
  1. ZREMRANGEBYLEX key min max: 移除有序集合中给定的字典区间的所有成员
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> ZADD myzset 1 m1 2 m2 3 m3 (integer) 3 127.0.0.1:6379> ZREMRANGEBYLEX myzset [m1 [m2  # 移除字典区间[m1,m2]中的所有成员 (integer) 2 127.0.0.1:6379> ZRANGE myzset 0 -1 1) "m3" 
  1. ZREMRANGEBYRANK key start stop: 移除有序集合中给定的排名区间的所有成员
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> ZADD myzset 1 m1 2 m2 3 m3 (integer) 3 127.0.0.1:6379> ZREMRANGEBYRANK myzset 0 1  # 移除排名0~1的所有成员 (integer) 2 127.0.0.1:6379> ZRANGE myzset 0 -1 1) "m3" 
  1. ZREMRANGEBYSCORE key min max: 移除有序集合中给定的分数区间的所有成员
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> ZADD myzset 1 m1 2 m2 3 m3 (integer) 3 127.0.0.1:6379> ZREMRANGEBYSCORE myzset 0 2  # 移除score在 [0,2]的成员 (integer) 2 127.0.0.1:6379> ZRANGE myzset 0 -1 1) "m3" 
  1. ZREVRANGE key start end: 返回有序集中指定区间内的成员,通过索引,分数从高到底
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> ZADD myzset 1 m1 2 m2 3 m3 (integer) 3 127.0.0.1:6379> ZREVRANGE myzset 0 3  # 按score递减排序,然后按索引,返回结果的 0~3 1) "m3" 2) "m2" 3) "m1" 127.0.0.1:6379> ZREVRANGE myzset 1 3  # 按score递减排序,然后按索引,返回结果的 1~3 1) "m2" 2) "m1" 
  1. ZREVRANGEBYSCORRE key max min: 返回有序集中指定分数区间内的成员,分数从高到低排序
127.0.0.1:6379> ZREVRANGEBYSCORE myzset 3 1 1) "m3" 2) "m2" 3) "m1" 127.0.0.1:6379> ZREVRANGEBYSCORE myzset 3 2 1) "m3" 2) "m2" 127.0.0.1:6379> ZREVRANGEBYSCORE myzset 3 2 WITHSCORES 1) "m3" 2) "3" 3) "m2" 4) "2" 
  1. ZREVRANGEBYLEX key max min: 返回有序集中指定字典区间内的成员,按字典顺序倒序
127.0.0.1:6379> ZRANGE myzset 0 -1 1) "m1" 2) "m2" 3) "m3" 127.0.0.1:6379> ZREVRANGEBYLEX myzset [m2 (m1 1) "m2" 127.0.0.1:6379> ZREVRANGEBYLEX myzset [m2 [m1 1) "m2" 2) "m1" 
  1. ZREVRANK key member: 返回有序集合中指定成员的排名,有序集成员按分数值递减(从大到小)排序
127.0.0.1:6379> ZREVRANK myzset m1 (integer) 2 127.0.0.1:6379> ZREVRANK myzset m3 (integer) 0 127.0.0.1:6379> ZREVRANK myzset m2 (integer) 1 
  1. ZINTERSTORE destination numkeys key1 [key2 ..]: 计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 key 中,numkeys:表示参与运算的集合数,将score相加作为结果的score
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> ZADD  zset1 1 m1 2 m2 3 m3 (integer) 3 127.0.0.1:6379> ZADD  zset2 1 m1 2 m2 3 m3 (integer) 3 127.0.0.1:6379> ZINTERSTORE newzset 2 zset1 zset2 (integer) 3 127.0.0.1:6379> ZRANGE newzset 0 -1 WITHSCORES 1) "m1" 2) "2" 3) "m2" 4) "4" 5) "m3" 6) "6" 
  1. ZUNIONSTORE destination numkeys key1 [key2..]: 计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 key 中.
127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> ZADD zset1 1 m1 2 m2 3 m3 (integer) 3 127.0.0.1:6379> ZADD zset2 3 m1 4 m2 5 m3 (integer) 3 127.0.0.1:6379> ZUNIONSTORE newzset 2 zset1 zset2 AGGREGATE MIN (integer) 3 127.0.0.1:6379> ZRANGE newzset 0 -1 WITHSCORES 1) "m1" 2) "1" 3) "m2" 4) "2" 5) "m3" 6) "3" 
  1. ZSCAN key cursor [MATCH pattern] [COUNT count]: 迭代有序集合中的元素(包括元素成员和元素分值)

应用案例:

  • set排序 存储班级成绩表 工资表排序!
  • 普通消息,1.重要消息 2.带权重进行判断
  • 排行榜应用实现,取Top N测试

本文转自互联网,侵权联系删除Redis 五大数据类型的基本命令使用

赞(0) 打赏
部分文章转自网络,侵权联系删除b2bchain区块链学习技术社区 » Redis 五大数据类型的基本命令使用
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

b2b链

联系我们联系我们