redis基本コマンド学習一(strings、hashes、lists)

8949 ワード

stringsタイプと操作
string          。    redis   string         ,    jpg           。           string      byte   ,      1G   ,    string      :
struct sdshdr {
long len;
long free;
char buf[];
};
len   buf      。
free            ,           string           ,           byte   ,            
buf    char               ,    char   c#   byte     ,      。
   string            int   .   incr    ,      string   , redis                memcached。    redis   string        memcached       ,        :
127.0.0.1:6379> SET name "fyl"   :   key values
127.0.0.1:6379> get name         :   key  values
"fyl"
127.0.0.1:6379> SETNX name "fyl_new"   :    key     ,   0, nx   not exist    。
(integer) 0
127.0.0.1:6379> get name    :key    ,    
"fyl"

127.0.0.1:6379> SETEX info 5 "hello"  :   key values,  5    
OK
127.0.0.1:6379> get info
"hello"
127.0.0.1:6379> get info
(nil)

127.0.0.1:6379> set mail "[email protected]"
OK
127.0.0.1:6379> get mail
"[email protected]"
127.0.0.1:6379>  SETRANGE mail 4 "google.com"   : key mail 4     
(integer) 14
127.0.0.1:6379> get mail
"[email protected]"

127.0.0.1:6379> mset name haha age 11   :       key
OK
127.0.0.1:6379> mget name age  :      key
1) "haha"
2) "11"


127.0.0.1:6379> MSETNX key1 11 key2 22 :key        
(integer) 1
127.0.0.1:6379> mget key1 key2
1) "11"
2) "22"
127.0.0.1:6379> MSETNX key1 33 key3 fds  :key key1   ,      
(integer) 0
127.0.0.1:6379> mget key1 key3
1) "11"
2) (nil)

127.0.0.1:6379> GETSET key1 haha   : key   ,     values
"11"
127.0.0.1:6379> get key1
"haha"
127.0.0.1:6379> GETSET key4 haha  : key   ,     values,key    ,   
(nil)

127.0.0.1:6379> get mail
"[email protected]"
127.0.0.1:6379> GETRANGE mail 0 2   : value   
"fyl"
127.0.0.1:6379> GETRANGE mail -10 -1  :range     0  ,   -1   
"google.com"

127.0.0.1:6379> mget name age k :     key,key       nil
1) "haha"
2) "11"
3) (nil)

127.0.0.1:6379> get age
"11"
127.0.0.1:6379> INCR age   : value ++   
(integer) 12
127.0.0.1:6379> get age
"12"
127.0.0.1:6379> INCRBY age 5  :   5 ++
(integer) 17
127.0.0.1:6379> INCRBY age1 5  : key     ,   key   value  0
(integer) 5
127.0.0.1:6379> DECR age :value--   ,key      -1
(integer) 16
127.0.0.1:6379> DECRBY age 3 :   3--
(integer) 13


127.0.0.1:6379> set name haha
OK
127.0.0.1:6379> APPEND name @126.com  :    key         value,          。
(integer) 12
127.0.0.1:6379> get name
"[email protected]"

127.0.0.1:6379> get name
"[email protected]"
127.0.0.1:6379> STRLEN name  :    key   value     。
(integer) 12
hashesタイプおよび操作
Redis hash     string     field   value     .    、       O(1) (  )。hash           。                string   。         hash            ,              。            hash         zipmap(    small hash)    。   zipmap       hash table,   zipmap       hash          hash               。   zipmap    ,  ,     O(n),          field       。     zipmap      ,             O(1)。   field    value          , Redis         zipmap        hash   .               
hash-max-zipmap-entries 64 #       64  
hash-max-zipmap-value 512 #   value     512   
127.0.0.1:6379> hset myhash field1 Hello  :   hash field     ,   key    ,    。
(integer) 1
127.0.0.1:6379> hsetnx myhash field "Hello" :   hash field     ,   key    ,    。    field     ,   0, nx  not exist    。
(integer) 1
127.0.0.1:6379> hsetnx myhash field "Hello"
(integer) 0
127.0.0.1:6379> hmset myhash field1 Hello field2 World :     hash     field。
OK
127.0.0.1:6379> HGET myhash field1
"Hello"
127.0.0.1:6379> HGET myhash field2
"World"
127.0.0.1:6379> HGET myhash field3
(nil)
127.0.0.1:6379> HMGET myhash field1 field2 field3
1) "Hello"
2) "World"
3) (nil)


127.0.0.1:6379> hset myhash field3 20
(integer) 1
127.0.0.1:6379> hget myhash field3
"20"
127.0.0.1:6379> HINCRBY myhash field3 -5  :    hash filed      。
(integer) 15
127.0.0.1:6379> hget myhash field3
"15"
127.0.0.1:6379> HEXISTS myhash field3  :  hash key    
(integer) 1
127.0.0.1:6379> HEXISTS myhash field4
(integer) 0
127.0.0.1:6379> HLEN myhash  :     hash   field   。
(integer) 3
127.0.0.1:6379> hdel myhash field1  :     hash   field 。
(integer) 1
127.0.0.1:6379> HLEN myhash
(integer) 2

127.0.0.1:6379> HKEYS myhash  :   hash     field
1) "field2"
2) "field3"
127.0.0.1:6379> HVALS myhash  :    hash     value
1) "World"
2) "15"

127.0.0.1:6379> HGETALL myhash  :     hash      filed   value
1) "field2"
2) "World"
3) "field3"
4) "15"
listsタイプおよびアクション
list        ,      push、 pop、            ,     key         。
Redis   list                 string        。        (2  32   )。       push,pop                   。    list      ,       。
      list   pop          ,   [lr]pop    list    ,   list   ,     ,      nil。        b[lr]pop        ,         ,        nil。          pop  ,         。             list          。      thread           pop                         。                ,            。     ,              :
127.0.0.1:6379> LPUSH mylist "word"
(integer) 1
127.0.0.1:6379> LPUSH mylist "hello"
(integer) 2
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello"
2) "word"
            world,    world          hello。   lrange      mylist    
127.0.0.1:6379> RPUSH mylist "word"   :    
(integer) 3
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello"
2) "word"
3) "word"

127.0.0.1:6379> LINSERT mylist before "word" "fyl"  : list "word"     "fyl"
(integer) 4
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello"
2) "fyl"
3) "word"
4) "word"

127.0.0.1:6379> LSET mylist 3 "good" :  list  ,   0  
OK
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello"
2) "fyl"
3) "word"
4) "good"
127.0.0.1:6379> LSET mylist -2 "haha"
OK
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello"
2) "fyl"
3) "haha"
4) "good"


127.0.0.1:6379> RPUSH m5 "hello"
(integer) 1
127.0.0.1:6379> RPUSH m5 "hello"
(integer) 2
127.0.0.1:6379> RPUSH m5 "qwe"
(integer) 3
127.0.0.1:6379> RPUSH m5 "hello"
(integer) 4
127.0.0.1:6379> LRANGE m5 0 -1
1) "hello"
2) "hello"
3) "qwe"
4) "hello"
127.0.0.1:6379> LREM m5 2 "hello"  :  key    list     count    value      ,count>0  ,          
(integer) 2
127.0.0.1:6379> LRANGE m5 0 -1
1) "qwe"
2) "hello"

127.0.0.1:6379> RPUSH m6 "hello"
(integer) 1
127.0.0.1:6379> RPUSH m6 "hello"
(integer) 2
127.0.0.1:6379> RPUSH m6 "qwe"
(integer) 3
127.0.0.1:6379> RPUSH m6 "hello"
(integer) 4
127.0.0.1:6379> LRANGE m6 0 -1
1) "hello"
2) "hello"
3) "qwe"
4) "hello"
127.0.0.1:6379> LREM m6 -2 "hello"  :count<0  ,          !!count=0  ,    
(integer) 2
127.0.0.1:6379> LRANGE m6 0 -1
1) "hello"
2) "qwe"

127.0.0.1:6379> RPUSH m7 "one"
(integer) 1
127.0.0.1:6379> RPUSH m7 "two"
(integer) 2
127.0.0.1:6379> RPUSH m7 "three"
(integer) 3
127.0.0.1:6379> RPUSH m7 "four"
(integer) 4
127.0.0.1:6379> LRANGE m7 0 -1
1) "one"
2) "two"
3) "three"
4) "four"
127.0.0.1:6379> LTRIM m7 2 -1   :     key         
OK
127.0.0.1:6379> LRANGE m7 0 -1
1) "three"
2) "four"
127.0.0.1:6379> LPUSH m7 "two"  :list      
(integer) 3
127.0.0.1:6379> LRANGE m7 0 -1
1) "two"
2) "three"
3) "four"
127.0.0.1:6379> RPOP m7  :list      value,   
"four"
127.0.0.1:6379> LRANGE m7 0 -1
1) "two"
2) "three"
127.0.0.1:6379> LPOP m7 :list      value,   
"two"
127.0.0.1:6379> LRANGE m7 0 -1
1) "three"


127.0.0.1:6379> LRANGE m6 0 -1
1) "hello"
2) "qwe"
127.0.0.1:6379> LRANGE m7 0 -1
1) "three"
2) "four"
127.0.0.1:6379> RPOPLPUSH m6 m7 :     list                list    ,           ,        .      list           nil
"qwe"
127.0.0.1:6379> LRANGE m6 0 -1
1) "hello"
127.0.0.1:6379> LRANGE m7 0 -1
1) "qwe"
2) "three"
3) "four"
127.0.0.1:6379> LINDEX m7 1 :      key   list   index      
"three"
127.0.0.1:6379> LLEN m7  :   key    list    
(integer) 3