• Redis数据类型


    1.String字符串

      string是redis最基本的类型,你可以理解成与Memcached一模一样的类型,一个key对应一个value。

      string类型是二进制安全的。意思是redis的string可以包含任何数据。比如jpg图片或者序列化的对象。

      string类型是Redis最基本的数据类型,一个redis中字符串value最多可以是512M。

    1)Redis Key

     

    #keys *

    127.0.0.1:6379> keys *

    (empty list or set)

     

    #set k1 v1

    127.0.0.1:6379> set k1 v1

    OK

     

    #exists key:判断某个key是否存在

    127.0.0.1:6379> EXISTS k1

    (integer) 1

     

    #move key db:移动key到别处

    127.0.0.1:6379> MOVE k1 1

    (integer) 1

    127.0.0.1:6379> keys *

    1) "k3"

    2) "k2"

    127.0.0.1:6379> select 1

    OK

    127.0.0.1:6379[1]> keys *

    1) "k1"

     

    #expire key:为给定的key设置过期时间

    #ttl key:查看还有多少秒过期,-1表示永不过期,-2表示已过期

    127.0.0.1:6379> keys *

    1) "k3"

    2) "k2"

    3) "k1"

    127.0.0.1:6379> EXPIRE k1 10

    (integer) 1

    127.0.0.1:6379> ttl k1

    (integer) 4

    127.0.0.1:6379> keys *

    1) "k3"

    2) "k2"

     

    #type key:查看key是什么类型

    127.0.0.1:6379> type k1

    string

     

    2)Redis String

     

    #set|get|del|append|strlen

    127.0.0.1:6379> set k1 v1

    OK

    127.0.0.1:6379> set k2 v2

    OK

    127.0.0.1:6379> set k3 v3

    OK

    127.0.0.1:6379> get k1

    "v1"

    127.0.0.1:6379> del k1

    (integer) 1

    127.0.0.1:6379> get k1

    (nil)

    127.0.0.1:6379> APPEND k2 xxxx

    (integer) 6

    127.0.0.1:6379> get k2

    "v2xxxx"

    127.0.0.1:6379> STRLEN k2

    (integer) 6

     

    #Incr|decr|incrby|decrby

    #注:value值一定要是数字才能进行加减

    127.0.0.1:6379> set k1 1

    OK

    127.0.0.1:6379> INCR k1

    (integer) 2

    127.0.0.1:6379> get k1

    "2"

    127.0.0.1:6379> DECR k1

    (integer) 1

    127.0.0.1:6379> get k1

    "1"

    127.0.0.1:6379> INCRBY k1 10

    (integer) 11

    127.0.0.1:6379> get k1

    "11"

    127.0.0.1:6379> DECRBY k1 10

    (integer) 1

     

    #getrange key start end:获取指定区间范围内的值,类似between......and的关系.从零到负一表示全部

    #setrange key start str:设置指定区间范围内的值,格式是setrange key值 具体值

    127.0.0.1:6379> set k1 abcdefgh

    OK

    127.0.0.1:6379> GETRANGE k1 0 -1

    "abcdefgh"

    127.0.0.1:6379> GETRANGE k1 0 2

    "abc"

    127.0.0.1:6379> SETRANGE k1 1 xxx

    (integer) 8

    127.0.0.1:6379> GETRANGE k1 0 -1

    "axxxefgh"

     

    #setex key time value:设置带过期时间的key,动态设置

    #setnx key value:key 不存在时设置 key 的值。

    127.0.0.1:6379> SETEX k1 15 hello

    OK

    127.0.0.1:6379> get k1

    "hello"

    127.0.0.1:6379> ttl k1

    (integer) 9

    127.0.0.1:6379> ttl k1

    (integer) -2

    127.0.0.1:6379> get k1

    (nil)

    127.0.0.1:6379> SETNX k1 hello

    (integer) 1

    127.0.0.1:6379> get k1

    "hello"

    127.0.0.1:6379> setnx k1 hello2

    (integer) 0

    127.0.0.1:6379> get k1

    "hello"

    127.0.0.1:6379> SETNX k6 hello2

    (integer) 1

    127.0.0.1:6379> get k6

    "hello2"

     

    #mset key1 value1 key2 value2 ...:同时设置一个或多个 key-value

    #mget key1 key2 ...:获取所有(一个或多个)给定 key 的值

    #msetnx k1 value1 key2 value2...:同时设置一个或多个 key-value 对,当且仅当所有给定 key 都不存在

    127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3

    OK

    127.0.0.1:6379> mget k1 k2 k3

    1) "v1"

    2) "v2"

    3) "v3"

    127.0.0.1:6379> MSETNX k3 v4 k4 v4

    (integer) 0

    127.0.0.1:6379> MSETNX k4 v4 k5 v5

    (integer) 1

     

    #getset key value:将给定 key 的值设为 value ,并返回 key 的旧值(old value)

    127.0.0.1:6379> get k1

    "v1"

    127.0.0.1:6379> getset k1 hello

    "v1"

    127.0.0.1:6379> get k1

    "hello"

    2.List列表

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

     

    #lpush key value1 value2...|rpush key value1 value2...|lrange key start end[0 -1为全部]

    127.0.0.1:6379> LPUSH list1 1 2 3 4 5

    (integer) 5

    127.0.0.1:6379> LRANGE list1 0 -1

    1) "5"

    2) "4"

    3) "3"

    4) "2"

    5) "1"

    127.0.0.1:6379> RPUSH list2 1 2 3 4 5

    (integer) 5

    127.0.0.1:6379> LRANGE list2 0 -1

    1) "1"

    2) "2"

    3) "3"

    4) "4"

    5) "5"

     

    #LPOP list|RPOP list

    127.0.0.1:6379> LPOP list1

    "5"

    127.0.0.1:6379> LPOP list2

    "1"

    127.0.0.1:6379> RPOP list1

    "1"

    127.0.0.1:6379> RPOP list2

    "5"

     

    #lindex key index:按照索引下标获得元素(从上到下)

    127.0.0.1:6379> LRANGE list1 0 -1

    1) "4"

    2) "3"

    3) "2"

    127.0.0.1:6379> LRANGE list2 0 -1

    1) "2"

    2) "3"

    3) "4"

    127.0.0.1:6379> LINDEX list1 3

    (nil)

    127.0.0.1:6379> LINDEX list1 2

    "2"

    127.0.0.1:6379> LINDEX list2 2

    "4"

     

    #llen key

    127.0.0.1:6379> LLEN list1

    (integer) 3

     

    #LREM key num value:leftright删除num 个值等于value的元素,返回的值为实际删除的数量

    #LREM list3 0 值,表示删除全部给定的值。零个就是全部值

    127.0.0.1:6379> RPUSH list3 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5

    (integer) 15

    127.0.0.1:6379> LREM list3

    (error) ERR wrong number of arguments for 'lrem' command

    127.0.0.1:6379> LREM list3 2 3

    (integer) 2

    127.0.0.1:6379> LRANGE list3 0 -1

     1) "1"

     2) "1"

     3) "1"

     4) "2"

     5) "2"

     6) "2"

     7) "3"

     8) "4"

     9) "4"

    10) "4"

    11) "5"

    12) "5"

    13) "5"

     

    #LTRIM key start end:截取指定索引区间的元素

    127.0.0.1:6379> LPUSH list1 1 2 3 4 5 6

    (integer) 6

    127.0.0.1:6379> LRANGE list1 0 -1

    1) "6"

    2) "5"

    3) "4"

    4) "3"

    5) "2"

    6) "1"

    127.0.0.1:6379> LTRIM list1 2 3

    OK

    127.0.0.1:6379> LRANGE list1 0 -1

    1) "4"

    2) "3"

     

    #RPOPLPUSH key1 key2:移除列表的最后一个元素,并将该元素添加到另一个列表并返回

    127.0.0.1:6379> LPUSH list1 1 2 3

    (integer) 3

    127.0.0.1:6379> LPUSH list2 4 5 6

    (integer) 3

    127.0.0.1:6379> LRANGE list1 0 -1

    1) "3"

    2) "2"

    3) "1"

    127.0.0.1:6379> LRANGE list2 0 -1

    1) "6"

    2) "5"

    3) "4"

    127.0.0.1:6379> RPOPLPUSH list1 list2

    "1"

    127.0.0.1:6379> LRANGE list1 0 -1

    1) "3"

    2) "2"

    127.0.0.1:6379> LRANGE list2 0 -1

    1) "1"

    2) "6"

    3) "5"

    4) "4"

     

    #LSET key index value:index位置的值更换为value

    127.0.0.1:6379> LRANGE list1 0 -1

    1) "3"

    2) "2"

    127.0.0.1:6379> LSET list1 1 xxx

    OK

    127.0.0.1:6379> LRANGE list1 0 -1

    1) "3"

    2) "xxx"

     

    #LINSERT key before|after index value:list某个已有值的前|后再添加具体值

    127.0.0.1:6379> LPUSH list1 1 2 3 4 5

    (integer) 5

    127.0.0.1:6379> LRANGE list1 0 -1

    1) "5"

    2) "4"

    3) "3"

    4) "2"

    5) "1"

    127.0.0.1:6379> LINSERT list1 before 3 java

    (integer) 6

    127.0.0.1:6379> LRANGE list1 0 -1

    1) "5"

    2) "4"

    3) "java"

    4) "3"

    5) "2"

    6) "1"

    127.0.0.1:6379> LINSERT list1 after  3 javaweb

    (integer) 7

    127.0.0.1:6379> LRANGE list1 0 -1

    1) "5"

    2) "4"

    3) "java"

    4) "3"

    5) "javaweb"

    6) "2"

    7) "1"

    List总结

    它是一个字符串链表,leftright都可以插入添加;

    如果键不存在,创建新的链表;

    如果键已存在,新增内容;

    如果值全移除,对应的键也就消失了。

    链表的操作无论是头和尾效率都极高,但假如是对中间元素进行操作,效率就很惨淡了。

    3.Set集合

      Redis的Set是string类型的无序集合。它是通过HashTable实现的。

     

    #sadd|smembers|sismember

    127.0.0.1:6379> sadd set1 1 1 2 2 3 3

    (integer) 3

    127.0.0.1:6379> SMEMBERS set1

    1) "1"

    2) "2"

    3) "3"

    127.0.0.1:6379> SISMEMBER set1 1

    (integer) 1

    127.0.0.1:6379> SISMEMBER set1 a

    (integer) 0

     

    #SCARD:获取集合里面的元素个数

    127.0.0.1:6379> SCARD set1

    (integer) 3

     

    # SREM key value:删除集合中元素

    127.0.0.1:6379> SMEMBERS set1

    1) "1"

    2) "2"

    3) "3"

    127.0.0.1:6379> SREM set1 1

    (integer) 1

    127.0.0.1:6379> SMEMBERS set1

    1) "2"

    2) "3"

     

    # SRANDMEMBER key num:随机出几个值

    #num:超过最大数量就全部取出.如果写的值是负数,比如-3 ,表示需要取出3个,但是可能会有重复值

    127.0.0.1:6379> FLUSHALL

    OK

    127.0.0.1:6379> SADD set1 1 2 3 4 5

    (integer) 5

    127.0.0.1:6379> SMEMBERS set1

    1) "1"

    2) "2"

    3) "3"

    4) "4"

    5) "5"

    127.0.0.1:6379> SRANDMEMBER set1 2

    1) "5"

    2) "2"

    127.0.0.1:6379> SRANDMEMBER set1 2

    1) "3"

    2) "4"

    127.0.0.1:6379> SRANDMEMBER set1 2

    1) "3"

    2) "1"

    127.0.0.1:6379> SRANDMEMBER set1 2

    1) "3"

    2) "1"

     

    #SPOP key:随机出栈

    127.0.0.1:6379> SMEMBERS set1

    1) "1"

    2) "2"

    3) "3"

    4) "4"

    5) "5"

    127.0.0.1:6379> SPOP set1

    "4"

    127.0.0.1:6379> SPOP set1

    "1"

    127.0.0.1:6379> SPOP set1

    "2"

    127.0.0.1:6379> SMEMBERS set1

    1) "3"

    2) "5"

     

    #SMOVE key1 key2:key1里的某个值赋给key2

    127.0.0.1:6379> FLUSHALL

    OK

    127.0.0.1:6379> SADD set1 1 2 3 4 5

    (integer) 5

    127.0.0.1:6379> SMEMBERS set1

    1) "1"

    2) "2"

    3) "3"

    4) "4"

    5) "5"

    127.0.0.1:6379> smove set1 set2 1

    (integer) 1

    127.0.0.1:6379> SMEMBERS set1

    1) "2"

    2) "3"

    3) "4"

    4) "5"

    127.0.0.1:6379> SMEMBERS set2

    1) "1"

     

    #SDIFF key1 key2:ey1里面而不在后面任何一个key2里面的项

    #SINTER key1 key2:key1中且在key2中含有

    #SUNION key1 key2:key1中且在key2中的并集

    127.0.0.1:6379> FLUSHALL

    OK

    127.0.0.1:6379> SADD set1 1 2 3 4 5

    (integer) 5

    127.0.0.1:6379> SADD set2 4 5 6 7 8

    (integer) 5

    127.0.0.1:6379> SDIFF set1 set2

    1) "1"

    2) "2"

    3) "3"

    127.0.0.1:6379> SDIFF set2 set1

    1) "6"

    2) "7"

    3) "8"

    127.0.0.1:6379> SINTER set1 set2

    1) "4"

    2) "5"

    127.0.0.1:6379> SUNION set1 set2

    1) "1"

    2) "2"

    3) "3"

    4) "4"

    5) "5"

    6) "6"

    7) "7"

    8) "8"

     

    4.Hash哈希

      Redis hash 是一个键值对集合。

      Redis hash是一个string类型的field和value的映射表,hash特别适合用于存储对象。类似于Java的Map的<String,Object>。

     

     

    #hset|hget|hmset|hmget|hgetall|hdel

    127.0.0.1:6379> HSET hash1 k1 v1

    (integer) 1

    127.0.0.1:6379> HGET hash1 k1

    "v1"

    127.0.0.1:6379> HMSET hash2 id 1 name pluto age 24

    OK

    127.0.0.1:6379> HGETALL hash1

    1) "k1"

    2) "v1"

    127.0.0.1:6379> HGETALL hash2

    1) "id"

    2) "1"

    3) "name"

    4) "pluto"

    5) "age"

    6) "24"

    127.0.0.1:6379> HDEL hash1 k1

    (integer) 1

    127.0.0.1:6379>

     

    #HLEN:计算hash的长度

    127.0.0.1:6379> HLEN hash1

    (integer) 0

    127.0.0.1:6379> HLEN hash2

    (integer) 3

     

    #HEXISTS hashname key:key里面的某个值的key

    127.0.0.1:6379> HGETALL hash2

    1) "id"

    2) "1"

    3) "name"

    4) "pluto"

    5) "age"

    6) "24"

    127.0.0.1:6379> HEXISTS hash2 id

    (integer) 1

    127.0.0.1:6379> HEXISTS hash2 email

    (integer) 0

     

     

    #HKEYS|HVALS

    127.0.0.1:6379> HKEYS hash2

    1) "id"

    2) "name"

    3) "age"

    127.0.0.1:6379> HVALS hash2

    1) "1"

    2) "pluto"

    3) "24"

     

    #HINCRBY|HINCRBYFLOAT:增加整数|增加小数

    127.0.0.1:6379> HINCRBY hash2 age 2

    (integer) 26

    127.0.0.1:6379> HINCRBY hash2 age 2

    (integer) 28

    127.0.0.1:6379> HINCRBY hash2 age 2

    (integer) 30

    127.0.0.1:6379> hset hash2 score 91.5

    (integer) 1

    127.0.0.1:6379> HGETALL hash2

    1) "id"

    2) "1"

    3) "name"

    4) "pluto"

    5) "age"

    6) "30"

    7) "score"

    8) "91.5"

    127.0.0.1:6379> HINCRBYFLOAT hash2 score 0.5

    "92"

     

    #HSETNX hash key value:不存在赋值,存在了不赋值

    127.0.0.1:6379> HGETALL hash2

    1) "id"

    2) "1"

    3) "name"

    4) "pluto"

    5) "age"

    6) "30"

    7) "score"

    8) "92"

    127.0.0.1:6379> HSETNX hash2 name plutoo

    (integer) 0

    127.0.0.1:6379> HSETNX hash2 email pluto@pluto.com

    (integer) 1

    127.0.0.1:6379> HGETALL hash2

     1) "id"

     2) "1"

     3) "name"

     4) "pluto"

     5) "age"

     6) "30"

     7) "score"

     8) "92"

     9) "email"

    10) "pluto@pluto.com"

    5.Zset

      Redis zset 和 set 一样也是string类型元素的集合,且不允许重复的成员。

      不同的是每个元素都会关联一个double类型的分数。

      redis正是通过分数来为集合中的成员进行从小到大的排序。zset的成员是唯一的,但分数(score)却可以重复。

      set基础上,加一个score值。之前set是k1 v1 v2 v3,现在zset是k1 score1 v1 score2 v2。

     

    #ZADD |ZRANGE

    127.0.0.1:6379> ZADD zset1 60 v1 70 v2 80 v3 90 v4

    (integer) 4

    127.0.0.1:6379> ZADD zset1 60 v1 70 v2 80 v3 90 v4

    (integer) 0

    127.0.0.1:6379> ZRANGE zset1 0 -1

    1) "v1"

    2) "v2"

    3) "v3"

    4) "v4"

    127.0.0.1:6379> ZRANGE zset1 0 -1 withscores

    1) "v1"

    2) "60"

    3) "v2"

    4) "70"

    5) "v3"

    6) "80"

    7) "v4"

    8) "90"

     

    #ZRANGEBYSCORE key start end

    #ZRANGEBYSCORE key start (end:不包含end

    #ZRANGEBYSCORE key (start (end:不包含start end

    #ZRANGEBYSCORE key start end limit index num:返回限制,开始位置index 截取num个类似于分页

     

    127.0.0.1:6379> ZRANGEBYSCORE zset1 60 90

    1) "v1"

    2) "v2"

    3) "v3"

    4) "v4"

    127.0.0.1:6379> ZRANGEBYSCORE zset1 60 (90

    1) "v1"

    2) "v2"

    3) "v3"

    127.0.0.1:6379> ZRANGEBYSCORE zset1 (60 (90

    1) "v2"

    2) "v3"

    127.0.0.1:6379> ZRANGEBYSCORE zset1 60 90

    1) "v1"

    2) "v2"

    3) "v3"

    4) "v4"

    127.0.0.1:6379> ZRANGEBYSCORE zset1 60 90 limit 2 2

    1) "v3"

    2) "v4"

    127.0.0.1:6379> ZRANGEBYSCORE zset1 60 90 withscores

    1) "v1"

    2) "60"

    3) "v2"

    4) "70"

    5) "v3"

    6) "80"

    7) "v4"

    8) "90"

     

    #ZRANGE:删除元素,项的值可以是多个

    127.0.0.1:6379> ZRANGE zset1 0 -1

    1) "v1"

    2) "v2"

    3) "v3"

    4) "v4"

    127.0.0.1:6379> ZREM zset1 v1

    (integer) 1

    127.0.0.1:6379> ZRANGE zset1 0 -1

    1) "v2"

    2) "v3"

    3) "v4"

    127.0.0.1:6379> ZREM zset1 v2 v3

    (integer) 2

    127.0.0.1:6379> ZRANGE zset1 0 -1

    1) "v4"

     

    #ZCARD key:获取集合中元素个数

    #ZCOUNT key start end:获取分数区间内元素个数

    #ZRANK zset value:获取valuezset中的下标位置

    #ZSCORE zset value:按照值获得对应的分数

    127.0.0.1:6379> ZADD zset1 60 v1 70 v2 80 v3 90 v4 100 v5

    (integer) 5

    127.0.0.1:6379> ZCARD zset1

    (integer) 5

    127.0.0.1:6379> ZCOUNT zset1 60 80

    (integer) 3

    127.0.0.1:6379> ZRANK zset1 v2

    (integer) 1

    127.0.0.1:6379> zscore zset1 v1

    "60"

     

    #|ZRANK|ZREVRANK key values:正序|逆序获得下标索引值

    127.0.0.1:6379> ZRANGE zset1 0 -1

    1) "v1"

    2) "v2"

    3) "v3"

    4) "v4"

    5) "v5"

    127.0.0.1:6379> ZREVRANK zset1 v1

    (integer) 4

    127.0.0.1:6379> ZREVRANK zset1 v5

    (integer) 0

    127.0.0.1:6379> ZRANK zset1 v1

    (integer) 0

    127.0.0.1:6379> ZRANK zset1 v5

    (integer) 4

     

    #ZRANGE|ZREVRANGE :正序|逆序输出[key,value]

    127.0.0.1:6379> ZRANGE zset1 0 -1 withscores

     1) "v1"

     2) "60"

     3) "v2"

     4) "70"

     5) "v3"

     6) "80"

     7) "v4"

     8) "90"

     9) "v5"

    10) "100"

    127.0.0.1:6379> ZREVRANGE zset1 0 -1 withscores

     1) "v5"

     2) "100"

     3) "v4"

     4) "90"

     5) "v3"

     6) "80"

     7) "v2"

     8) "70"

     9) "v1"

    10) "60"

     

    #ZRANGEBYSCORE|ZREVRANGEBYSCORE  zset1 90 60 withscores:分数范围的正|反输出

    127.0.0.1:6379> ZRANGEBYSCORE zset1 60 90 withscores

    1) "v1"

    2) "60"

    3) "v2"

    4) "70"

    5) "v3"

    6) "80"

    7) "v4"

    8) "90"

    127.0.0.1:6379> ZREVRANGEBYSCORE zset1 60 90 withscores

    (empty list or set)

    127.0.0.1:6379> ZREVRANGEBYSCORE zset1 90 60 withscores

    1) "v4"

    2) "90"

    3) "v3"

    4) "80"

    5) "v2"

    6) "70"

    7) "v1"

    8) "60"

     

    参考文档:Http://redisdoc.com/

  • 相关阅读:
    php 元字符与转义
    php内置函数
    php系统常量
    WPF 批量修改控件属性
    对实体属性值赋值(DATASET转list)
    WPF 进度条实现
    WPF 异步刷新主界面
    c# 依赖注入Export ImportMany
    Oracle 正则匹配实现字符拆分
    Oracle 获取表对应列信息和索引信息
  • 原文地址:https://www.cnblogs.com/CSAH/p/13823875.html
Copyright © 2020-2023  润新知