Redis数据类型

吃可爱长大的小学妹 提交于 2020-03-01 16:01:27

Redis数据类型

  1. 常见数据类型如下:(参考Http://redisdoc.com/)

    1. String/字符串
    2. Hash/哈希
    3. List/列表
    4. Set/集合
    5. Zset/sorted set有序集合
  2. KEY的操作:(小写key代表键名,小写db代表数据库角标,小写second代表秒数)

    1. KEYS *:查看当前数据库的所有键值,若没有返回空(empty list or set)
    2. EXISTS key:存在key的值返回1,不存在返回0
    3. MOVE key db:移动key到其他数据库(db),key不存在返回0,数据库不存在报错(error) ERR index out of range
    4. EXPIRE key second:给制定key设置过期时间,key不存在则失败返回0
    5. TTL key:查看制定key的剩余时间(-2~正无穷大),永久返回-1,已过期返回-2
  3. String/字符串的操作:单值单value

    1. SET key xxx/GET key/DEL key/APPEND key xxx/STRLEN key:设值(可覆盖),取值(不存在返回nil),删除(成功返回1失败返回0),追加(key存在返回其长度,不存在则相当于新增),求长度(不存在返回0)
    2. INCR key/DECR key/INCRBY key n/DECRBY key n:给指定key+1/-1/+n/-n(若不存在则新增key,且值为1或n)
    3. GETRANGE key m n/SETRANGE key n xxx:取指定key的[m,n]的值,从0开始,-1代表最后一位。-2代表倒数第二位/将指定key的值从第n为开始设置为xxx,会覆盖回追加
    4. SETEX key second xxx/ SETNX key xxx:(setex:set with expire)设置带过期时间的值(可覆盖可新增)/(setnx:set if not exist)先判断key是否存在,不存在时设值,存在失败返回0
    5. MSET key1 xxx key2 xxx key3 xxx …/MGET key1 key2 key 3 …/MSETNX key1 xxx key2 xxx key3 xxx… :设置多个key的值/取得多个key的值/设置多个key的值(全部不存在时才设置,否则返回0)
  4. GETSET key xxx:先取值再设置值,若不不存在key则返回nil后设置值

  5. List/列表:单值多value

    1. LPUSH key v1 v2 v3 …/rpush key v1 v2 v3 …/lrange key m n:从左往右添加/从右往左添加/从左往右取值,[m,n]
    2. LPOP key/RPOP key :从左边出一个值/从右边出一个值
    3. LINDEX key index:(lindex key index)通过索引获取列表中角标为index的元素
    4. LLEN key:获取指定key的长度
    5. LREM key value :再key中删N个值相同的value,不够则全删,返回删除的个数
    6. LTRIM key m n:截取指定范围[m,n]的值后再赋值给这个key,越界则截取[m,n]覆盖的值
    7. RPOPLPUSH key1 key2:移除key1列表的最后一个元素,并将该元素添加到另一个列表key2(不存在则新建),并返回
    8. LSET key index xxx:将xxx插如key中的index位置,若index越界则报错(error) ERR index out of range
    9. LINSERT key before/after xxx1 xxx2:将xxx2插入到xxx1的前面/后面,若xxx1不存在则插入失败返回-1
    10. 总结:List是一个字符串链表,left、right都可以插入添加;如果键不存在,创建新的链表;如果键已存在,新增内容;如果值全移除,对应的键也就消失了。链表的操作无论是头和尾效率都极高,但是对中间元素进行操作时,效率就很低了。
  6. Hash/哈希KV模式不变,但V是一个键值对

    1. HSET hashname key xxx/HGET hashname key/HMSET hashname key1 xx1 key2 x2 …/HMGET hashname key1 key2 …/HGETALL hashname/HDEL hashname

      127.0.0.1:6379> HSET h1 k1 v1
      (integer) 1
      127.0.0.1:6379> HGET h1 k1
      "v1"
      127.0.0.1:6379> HMSET h1 k2 v2 k3 v3 k4 v4
      OK
      127.0.0.1:6379> HMGET h1 k1 k2 k3 k4
      1) "v1"
      2) "v2"
      3) "v3"
      4) "v4"
      127.0.0.1:6379> HGETALL h1
      1) "k1"
      2) "v1"
      3) "k2"
      4) "v2"
      5) "k3"
      6) "v3"
      7) "k4"
      8) "v4"
      127.0.0.1:6379> HDEL h1 k4
      (integer) 1
      127.0.0.1:6379> HGETALL h1
      1) "k1"
      2) "v1"
      3) "k2"
      4) "v2"
      5) "k3"
      6) "v3"
      127.0.0.1:6379> 
      
    2. HLEN hashname:

      127.0.0.1:6379> HGETALL h1
      1) "k1"
      2) "v1"
      3) "k2"
      4) "v2"
      5) "k3"
      6) "v3"
      127.0.0.1:6379> HLEN h1
      (integer) 3
      127.0.0.1:6379> 
      
    3. HEXISTS hashname key:

    127.0.0.1:6379> HEXISTS h1 k1
    (integer) 1
    127.0.0.1:6379> HEXISTS h1 k5
    (integer) 0
    127.0.0.1:6379> HEXISTS h2 k5
    (integer) 0
    127.0.0.1:6379> HEXISTS h2
    (error) ERR wrong number of arguments for 'hexists' command
    127.0.0.1:6379> HEXISTS k1
    (error) ERR wrong number of arguments for 'hexists' command
    
    1. HKEYS hashname/HVALS hashname
    127.0.0.1:6379> HKEYS h1
    1) "k1"
    2) "k2"
    3) "k3"
    127.0.0.1:6379> HVALS h1
    1) "v1"
    2) "v2"
    3) "v3"
    127.0.0.1:6379> 
    
    1. HINCRBY hashname key n/HINCRBYFLOAT hashname key n.m:
    127.0.0.1:6379> hget h1 k4
    "2"
    127.0.0.1:6379> HINCRBY h1 k4
    (error) ERR wrong number of arguments for 'hincrby' command
    127.0.0.1:6379> HINCRBY h1 k4 8
    (integer) 10
    127.0.0.1:6379> HGET h1 k4
    "10"
    127.0.0.1:6379> HINCRBYFLOAT h1 k4 2.5
    "12.5"
    127.0.0.1:6379> HGET h1 k4
    "12.5"
    127.0.0.1:6379>
    
    1. HSETNX hashname key xxx:key不存在赋值,存在则无效

      127.0.0.1:6379> HSETNX h1 k6 20
      (integer) 1
      127.0.0.1:6379> HGET h1 k6
      "20"
      127.0.0.1:6379> HSETNX h1 k6 25
      (integer) 0
      127.0.0.1:6379> HGET h1 k6
      "20"
      127.0.0.1:6379>
      
  7. Set/集合:(单值多value,同List)

    1. SADD key xx1 xx2 …/SMEMBERS key/sismember:
    127.0.0.1:6379> SADD s1 v1 
    (integer) 1
    127.0.0.1:6379> SADD s1 v1 
    (integer) 0
    127.0.0.1:6379> SADD s2 a2 b2 c2
    (integer) 3
    127.0.0.1:6379> SMEMBERS s1
    1) "v1"
    127.0.0.1:6379> SMEMBERS s2
    1) "a2"
    2) "c2"
    3) "b2"
    
    127.0.0.1:6379> SISMEMBER s1 v1
    (integer) 1
    127.0.0.1:6379> SISMEMBER s1 v2
    (integer) 0
    127.0.0.1:6379> SISMEMBER s2 a1
    (integer) 0
    127.0.0.1:6379> SISMEMBER s2 a2
    (integer) 1
    127.0.0.1:6379>
    
    1. SCARD:获取集合里面的元素个数

      127.0.0.1:6379> SCARD s1
      (integer) 1
      127.0.0.1:6379> SCARD s2
      (integer) 3
      127.0.0.1:6379>
      
    2. SREM key xxx:删除集合中元素

    127.0.0.1:6379> SREM s1 v1
    (integer) 1
    127.0.0.1:6379> SREM s1 v1
    (integer) 0
    127.0.0.1:6379> SMEMBERS s1
    (empty list or set)
    127.0.0.1:6379> 
    
    1. SRANDMEMBER key 某个整数(随机出几个数)
    127.0.0.1:6379> SRANDMEMBER s2
    "b2"
    127.0.0.1:6379> SRANDMEMBER s2
    "a2"
    127.0.0.1:6379> SRANDMEMBER s2
    "a2"
    127.0.0.1:6379> SRANDMEMBER s2
    "b2"
    127.0.0.1:6379> SRANDMEMBER s2
    "c2"
    127.0.0.1:6379>
    
    1. SPOP key :随机出栈
    127.0.0.1:6379> SPOP s2
    "a2"
    127.0.0.1:6379> SPOP s2
    "c2"
    127.0.0.1:6379> SPOP s2
    "b2"
    127.0.0.1:6379> SMEMBERS s2
    (empty list or set)
    127.0.0.1:6379>
    
    1. SMOVE key1 key2 xxx:将key1里的某个值xxx赋给key2
    127.0.0.1:6379> SMEMBERS s1
    1) "v1"
    127.0.0.1:6379> SMEMBERS s2
    1) "v2"
    2) "c2"
    3) "a1"
    4) "b2"
    127.0.0.1:6379> SMOVE s1 s2 v1
    (integer) 1
    127.0.0.1:6379> SMEMBERS s2
    1) "v2"
    2) "c2"
    3) "a1"
    4) "b2"
    5) "v1"
    127.0.0.1:6379> SMEMBERS s1
    (empty list or set)
    
    127.0.0.1:6379> SMEMBERS s1
    (empty list or set)
    127.0.0.1:6379> SMOVE s1 s2 v1
    (integer) 0
    127.0.0.1:6379> sadd s1 v1
    (integer) 1
    127.0.0.1:6379> SMOVE s1 s3 v1
    (integer) 1
    127.0.0.1:6379>
    
    1. 数学集合类:SXXX key1 key2(SDIFF:差集、SINTER:交集、SUNION:并集)

      127.0.0.1:6379> SMEMBERS s1
      1) "v1"
      2) "v2"
      3) "v3"
      127.0.0.1:6379> SMEMBERS s2
      1) "v2"
      2) "c2"
      3) "a1"
      4) "b2"
      5) "v1"
      
      127.0.0.1:6379> SDIFF s1 s2
      1) "v3"
      127.0.0.1:6379> SINTER s1 s2
      1) "v1"
      2) "v2"
      127.0.0.1:6379> SUNION s1 s2
      1) "v3"
      2) "v1"
      3) "v2"
      4) "a1"
      5) "c2"
      6) "b2"
      127.0.0.1:6379> 
      
      
  8. Zset/sorted set有序集合:

    1. ZADD zsetname score1 xx1 score2 xx2 … / ZRANGE zsetname m n / ZRANGE zsetname m n withscores

      127.0.0.1:6379> ZADD z1 60 d 70 c 80 b 90 a
      (integer) 4
      127.0.0.1:6379> ZADD z1 60 d 70 c 80 b 90 a
      (integer) 0
      127.0.0.1:6379> ZRANGE z1 0 -1
      1) "d"
      2) "c"
      3) "b"
      4) "a"
      127.0.0.1:6379> ZRANGE z1 0 -1 withscores
      1) "d"
      2) "60"
      3) "c"
      4) "70"
      5) "b"
      6) "80"
      7) "a"
      8) "90"
      127.0.0.1:6379>
      
    2. ZRANGEBYSCORE :

      1. ZRANGEBYSCORE zsetname m n:查出score为[m,n]的value
      127.0.0.1:6379> ZRANGEBYSCORE z1 60 80
      1) "d"
      2) "c"
      3) "b"
      127.0.0.1:6379> 
      
      1. ZRANGEBYSCORE zsetname m n withscores:查出score为[m,n]的value,带scores
      127.0.0.1:6379> ZRANGEBYSCORE z1 60 80 withscores
      1) "d"
      2) "60"
      3) "c"
      4) "70"
      5) "b"
      6) "80"
      127.0.0.1:6379> 
      
      1. ZRANGEBYSCORE zsetname (m (n:“(”表示开区间
      127.0.0.1:6379> ZRANGEBYSCORE z1 (60 (80 withscores
      1) "c"
      2) "70"
      127.0.0.1:6379> ZRANGEBYSCORE z1 (60 (80 
      1) "c"
      127.0.0.1:6379> ZRANGEBYSCORE z1 (60 80 
      1) "c"
      2) "b"
      127.0.0.1:6379> ZRANGEBYSCORE z1 60 (80 
      1) "d"
      2) "c"
      127.0.0.1:6379>
      
    3. ZRANGEBYSCORE zsetname m n limit index k:limit 作用是返回限制、index是开始下标步、k是几步

      127.0.0.1:6379> ZRANGEBYSCORE z1  0 100 withscores
       1) "g"
       2) "30"
       3) "f"
       4) "40"
       5) "e"
       6) "50"
       7) "d"
       8) "60"
       9) "c"
      10) "70"
      11) "b"
      12) "80"
      13) "a"
      14) "90"
      127.0.0.1:6379> ZRANGEBYSCORE z1 40 80 limit 1 3
      1) "e"
      2) "d"
      3) "c"
      127.0.0.1:6379>   
      
    4. ZREM zsetname xxx:删除元素xxx为score下对应的value值

      127.0.0.1:6379> ZRANGEBYSCORE z1  0 100 withscores
       1) "g"
       2) "30"
       3) "f"
       4) "40"
       5) "e"
       6) "50"
       7) "d"
       8) "60"
       9) "c"
      10) "70"
      11) "b"
      12) "80"
      13) "a"
      14) "90"
      127.0.0.1:6379> ZREM z1 g
      (integer) 1
      127.0.0.1:6379> ZRANGEBYSCORE z1  0 100 withscores
       1) "f"
       2) "40"
       3) "e"
       4) "50"
       5) "d"
       6) "60"
       7) "c"
       8) "70"
       9) "b"
      10) "80"
      11) "a"
      12) "90"
      127.0.0.1:6379>
      
    5. ZCARD zsetname(获取集合中元素个数)/ZCOUNT zsetname m n (获取分数[m,n]内元素个数)/ZRANK zsetname xxx(获取xxx在key中的下标位置)/ ZSCORE zsetname xxx(按照值xxx获得对应的分数):

      127.0.0.1:6379> ZCARD z1
      (integer) 6
      127.0.0.1:6379> ZCOUNT z1 60 80
      (integer) 3
      127.0.0.1:6379> ZRANK z1 b
      (integer) 4
      127.0.0.1:6379> ZRANK z1 d
      (integer) 2
      127.0.0.1:6379> ZSCORE z1 b
      "80"
      127.0.0.1:6379> ZSCORE z1 80
      (nil)
      127.0.0.1:6379> ZSCORE z1 c
      "70"
      127.0.0.1:6379>
      
    6. ZRANK zsetname xxx / ZREVRANK zsetname xxx:获得下标值,逆序获得下标值

      127.0.0.1:6379> ZRANK z1 a
      (integer) 5
      127.0.0.1:6379> ZREVRANK z1 a
      (integer) 0
      127.0.0.1:6379> ZRANK z1 b
      (integer) 4
      127.0.0.1:6379> ZREVRANK z1 b
      (integer) 1
      127.0.0.1:6379>
      
    7. ZREVRANGE zsetname m n / ZREVRANGE zsetname m n withscores:

      127.0.0.1:6379> ZREVRANGE z1 0 -1 
      1) "a"
      2) "b"
      3) "c"
      4) "d"
      5) "e"
      6) "f"
      127.0.0.1:6379> ZREVRANGE z1 0 -1 withscores
       1) "a"
       2) "90"
       3) "b"
       4) "80"
       5) "c"
       6) "70"
       7) "d"
       8) "60"
       9) "e"
      10) "50"
      11) "f"
      12) "40"
      127.0.0.1:6379>
      
    8. ZREVRANGEBYSCORE zsetname m n / ZRANGEBYSCORE zsetname m n / ZREVRANGEBYSCORE zsetname m n withscores:

      127.0.0.1:6379> ZREVRANGEBYSCORE z1 90 60
      1) "a"
      2) "b"
      3) "c"
      4) "d"
      127.0.0.1:6379> ZRANGEBYSCORE z1 60 90
      1) "d"
      2) "c"
      3) "b"
      4) "a"
      127.0.0.1:6379> 
      
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!