Redisノート整理(一):Redisインストール構成とデータ型操作


[TOC]
Redisの概要
 Redis     (BSD  ),            ,      ,           。
       、   、  、  、    ,  ,hyperloglogs     。
     、Lua  、LRU  、               ,    Redis Sentinel     ,  Redis Cluster      。
      ,Redis     “ / ”           ,                  。
   Redis   NoSQL  。
     :http://www.redis.io
     :http://redis.cn/
  • Redisの特徴
    Redis        ,               ,               。
    Redis        k-v     ,     list,set,zset,hash        。
    Redis       , master-slave         。
  • Redisの利点
        ——Redis     11w/s,     8.1w/s。
           ——Redis        Strings,Lists,Hashes,Sets Ordered Sets      。
       ——Redis           ,  Redis                 。
         ——Redis   public/subscribe,  ,key     。
  • Redisアプリケーション
                  ,eg    
    hash:    ,    
    string:   ,   
    (    select count(*) from...)
    sorted set:
    TopN,    
      github,
    stackoverflow 
       redis
  • Redisインストール構成
    Redisインストール
    Windowsの下で直接解凍して使うことができて、主にLinuxの下のインストールの方式を説明します:
      :
    tar -zxvf soft/redis-3.2.0.tar.gz -C app/
       :
    mv app/redis-3.2.0/ app/redis
      :
    make
      :
    make install PREFIX=/home/uplooking/app/redis

    Redis構成
        
        redis.conf     
      bind uplooking01
      daemonize yes(    )
      logfile /opt/redis-3.2.0/logs/redis.log(    ,      )
    
        
      make    , redis-3.2.0/src          redis     redis-server,            redis-cli。
      redis-3.2.0]$ src/redis-server redis.conf
        ,      make install              bin  ,              Redis.
    
         
      redis-3.2.0]$ src/redis-cli -h localhost -p 6379
        
      ps -ef | grep redis
          id   kill -9    。

    Redis構成表示
    RedisのプロファイルはRedisのインストールディレクトリの下にあり、ファイル名はredis.conf.コンフィギュレーション・アイテムはconfigコマンドで表示または設定できます.
      
         ,Redis config      :
       redis localost:6379> config get config_set_name
       eg: redis localost:6379> config get loglevel
       1) "loglevel"
       2) "notice"
         *        :redis localost:6379> config get *
    
      
            redis.conf     config set       
          :
      redis localost:6379> config set conf_setting_name new_value
      eg: redis localost:6379> config set loglevel "warning"
          redis localost:6379> config get loglevel
      1) "loglevel"
      2) "warning"  -->       ,         ,    

    Redis操作
    string(文字列)
       string redis      ,       memcached       ,  key    value。
       string         。   redis string        。  jpg           。
       string   Redis        ,        512MB。
      
       redis 127.0.0.1:6379> SET name "redis.net.cn"
    OK
       redis 127.0.0.1:6379> GET name
    "redis.net.cn"
                 Redis SET GET  。  name,     redis.net.cn。
      :        512MB。

    一般的なstring操作コマンドは次のとおりです.
    set key value                       key  (         )
                                    set name yaohuiying
    
    get key                             key  
                                    get name
    
    getrange key start end            key         
                                    getrange name 1 4
                                      :      0  ,      [start, end],    
    
    getset key value                   key    value,   key   (old value)
                                    getset name liuxiangqian
                                        yaohuiying
    
    mget key1 [key2..]                  (     )  key  
                                    mget name name1
    
    setex key seconds value           value   key,  key       seconds(     )
    
    setnx key value                    key      key  
                                       set,   set     ,   key      ,     ,
                                     key  ,      
    
    setrange key offset value        value      key        ,    offset  
                                     key    value      , offset     ,
                                    value   ,     
    
    strlen key                        key           
    
    mset key value [key value ...]               key-value 
    
    MSETNX key value [key value ...]             key-value ,        key    
    
    psetex key milliseconds value            SETEX    ,          key     ,
                                            SETEX    ,     
    
    incr key                             key           
                                         key   value  +1,  value           
    
    INCRBY key increment                 key             (increment)
                                              
    
    incrbyfloat key increment            key               (increment)
    
    decr key                             key         
    
    decrby key decrement key                         (decrement)
    
    append key value                      key            ,
                                        APPEND   value   key       

    hash(ハッシュ)
    Redis hash         。Redis hash   string   field value    ,hash          。
      
    redis 127.0.0.1:6379> HMSET w3ckey name "redis tutorial" description "redis basic commands for caching" likes 20 visitors 23000
    OK
    redis 127.0.0.1:6379> HGETALL w3ckey
    1) "name"
    2) "redis tutorial"
    3) "description"
    4) "redis basic commands for caching"
    5) "likes"
    6) "20"
    7) "visitors"
    8) "23000"
          hash              (name,description, likes, visitors) hash w3ckey 。   hgetall        。
      hash    232-1   (40  )。 

    一般的なコマンド操作は次のとおりです.
    hexists key field                 hash key ,        
                                    0    ,1   
    hget key field                    hash        
    
    hgetall key                              
    hincrby key field increment                  increment
    
    hincrbyfloat key field increment                 increment
    
    hkeys key                              
    hlen key                          key      
    hdel key field2 [field2]                    
    hmget key field1 [field2]                 
    hmset key f1 v1 [f2 v2]              file-value   key 
    hset key field value                key field   value
    hsetnx key field value            field    ,        
    hvals key                             
    hscan key cursor [MATCH pattern] [COUNT count]            

    リスト
       Redis           ,        。               (  )    (  )。
      
    redis 127.0.0.1:6379> lpush redis.net.cn redis
    (integer) 1
    redis 127.0.0.1:6379> lpush redis.net.cn mongodb
    (integer) 2
    redis 127.0.0.1:6379> lpush redis.net.cn rabitmq
    (integer) 3
    redis 127.0.0.1:6379> lrange redis.net.cn 0 10
    1) "rabitmq"
    2) "mongodb"
    3) "redis"
    redis 127.0.0.1:6379>
            232-1   (4294967295,        40  )。

    一般的な操作コマンドは次のとおりです.
      :  list   ,         ,          
    l--->left(  ),r---->right(  )
    
    blpop key1 [key2 ] timeout                    ,             【    s】
                                              
                                           key1 key2.。。    ,         ,      
                                           timeout;           
    
    brpop key1 [key2 ] timeout                     ,                   
                                        
                                     blpop,              
    
    brpoplpush source destination timeout            ,                   
                                             ,                           
                                            uplooking01:6379> brpoplpush season season1 100
                                            "spring"
                                            (7.59s)
    
    lindex key index                            
                                        :
                                    lindex key -1          
    
    linsert key before|after pivot value                 
                                            uplooking01:6379> lrange season 0 -1
                                            1) "winter"
                                            2) "autumn"
                                            3) "summer"
                                            uplooking01:6379> linsert season after summer spring
                                            (integer) 4
                                            uplooking01:6379> lrange season 0 -1
                                            1) "winter"
                                            2) "autumn"
                                            3) "summer"
                                            4) "spring"
    
    llen key                              ,    
    
    lpop key                                     
    
    lpush key value1 [value2]                     
    
    lpushx key value                                  
    
    lrange key start stop                       
                                    [start, stop]
                                         lrange         
                                    lrange key 0 -1
    
    lrem key count value                  
                                    count > 0:          value    ,1 ,    1   
                                    count < 0:          value    ,-2 ,    2   
                                    count = 0:        value    。
    
    lset key index value                        (list.set(i, value))
                                                   -1,        
    
    ltrim key start stop                     (trim),    ,              ,
                                          
                                    [start, stop]
                                    uplooking01:6379> lrange season 0 -1
                                    1) "winter"
                                    2) "autumn"
                                    3) "summer"
                                    4) "spring"
                                    uplooking01:6379> ltrim season 1 2
                                    OK
                                    uplooking01:6379> lrange season 0 -1
                                    1) "autumn"
                                    2) "summer"
    
    rpop key                                     
    
    rpoplpush source destination               ,               
    
    rpush key value1 [value2]                   
    
    rpushx key value                          

    set(集合)
    Redis Set string       。
                   ,    ,  ,        O(1)。
    sadd  
            string   ,key   set   ,    1,             0,key   set       。
    sadd key member
      
    redis 127.0.0.1:6379> sadd redis.net.cn redis
    (integer) 1
    redis 127.0.0.1:6379> sadd redis.net.cn mongodb
    (integer) 1
    redis 127.0.0.1:6379> sadd redis.net.cn rabitmq
    (integer) 1
    redis 127.0.0.1:6379> sadd redis.net.cn rabitmq
    (integer) 0
    redis 127.0.0.1:6379> smembers redis.net.cn
    1) "rabitmq"
    2) "mongodb"
    3) "redis"
        :      rabitmq      ,            ,            。
                232 - 1 (4294967295,        40     )。

    一般的な操作コマンドは次のとおりです.
    sadd key member1 [member2]                  
    
    scard key                               (size)
    
    sdiff key1 [key2]                          
                                      A={1, 2, 3, 4, 5}   B={2, 3, 6, 8}
                                    A∩B  = {2, 3}
                                    A∪B = {1, 2, 3, 4, 5, 6, 8}
                                    A-B(  )  = {1, 4, 5, 6, 8}
                                                {1, 4, 5}√
                                    uplooking01:6379> sdiff A B
                                    1) "1"
                                    2) "4"
                                    3) "5"
    
    sdiffstore dest key1 [key2]                   dest 
                                    uplooking01:6379> sdiffstore tmp A B
                                    (integer) 3
                                    uplooking01:6379> smembers tmp
                                    1) "1"
                                    2) "4"
                                    3) "5"
    
    sinter key1 [key2]                       
                                    uplooking01:6379> sinter A B
                                    1) "2"
                                    2) "3"
    
    sinterstore dest key1 [key2]                 dest 
    
    sismember key member              member       key   
                                    uplooking01:6379> sismember A 3
                                    (integer) 1
                                    uplooking01:6379
    
    smembers key                             
    
    smove src dest member            member   src   dest 
    
    spop key                                       
    
    srandmember key [count]                      
                                          key  count   ,  count,  1 
                                    uplooking01:6379> srandmember A
                                    "1"
                                    uplooking01:6379> srandmember A 2
                                    1) "3"
                                    2) "4"
    
    srem key member1 [member2]                  
    
    sunion key1 [key2]                         ---           
                                    uplooking01:6379> sunion A B
                                    1) "1"
                                    2) "2"
                                    3) "3"
                                    4) "4"
                                    5) "5"
                                    6) "6"
                                    7) "8"
    
    sunionstore dest key1 [key2]                 dest
    
    sscan key cursor [match pattern] [count count]              

    zset(整列集合)
       Redis zset set    string       ,         。              double     。redis                       。
       zset       ,   (score)     。
       zadd          ,             score
       zadd key score member 
      
    redis 127.0.0.1:6379> zadd redis.net.cn 0 redis
    (integer) 1
    redis 127.0.0.1:6379> zadd redis.net.cn 0 mongodb
    (integer) 1
    redis 127.0.0.1:6379> zadd redis.net.cn 0 rabitmq
    (integer) 1
    redis 127.0.0.1:6379> zadd redis.net.cn 0 rabitmq
    (integer) 0
    redis 127.0.0.1:6379> ZRANGEBYSCORE redis.net.cn 0 1000
    1) "redis"
    2) "mongodb"
    3) "rabitmq"

    一般的な操作コマンドは次のとおりです.
    zadd key score1 member1  [score2 member2]                     ,
                                                                 
                                              uplooking01:6379> zadd website 0.01 www.uplooking.com
                                              (integer) 1
                                              uplooking01:6379> zadd website 0.05 www.baidu.com
                                              (integer) 1
                                              uplooking01:6379> zadd website 0.1 www.google.com
                                              (integer) 1
                                              uplooking01:6379> zadd website 1 www.taobao.com
                                              (integer) 1
                                              uplooking01:6379> zadd website 0.005 www.jd.com
                                              (integer) 1
    
    zcard key                                         
                                            uplooking01:6379> zcard website
                                            (integer) 5
    
    zcount key min max                                        
                                                  [min, max]   
                                             uplooking01:6379> zcount website 0.01 0.1
                                             (integer) 3
    
    zincrby key increment member                            incr
                                          uplooking01:6379> zincrby website 0.01 www.uplooking.com
                                          "0.02"    
    
    zinterstore dest nkeys key [key..]                                dest 
    
    zlexcount key min max                                      
                                                  
                                                -  ,       ,  +         
                                            zlexcount key - +        size
                                            min===>[member1
                                            max===>[member2
                                                        [member1, member2]      
                                            redis> ZADD myzset 1 a 2 b 3 c 4 d 5 e 6 f 7 g
                                            (integer) 7
                                            redis> zrange myzset 0 -1
                                            1) "a"
                                            2) "b"
                                            3) "c"
                                            4) "d"
                                            5) "e"
                                            6) "f"
                                            7) "g"
                                            redis> ZLEXCOUNT myzset - +
                                            (integer) 7
                                            redis> ZLEXCOUNT myzset [c +
                                            (integer) 5
                                            redis> ZLEXCOUNT myzset - [c
                                            (integer) 3
                                            redis>      
    
    zrange key start stop [withscores]                           
                                                   zrange key 0 -1       
    
    zrangebylex key min max [limit offset count]                   
    
    zrangebyscore key min max [withscores] [limit]                    
    
    zrank key member                                      
    
    zrem key member [member ...]                           
                                              1,    ,  0,      
    
    zremrangebylex key min max                                 
    
    zremrangebyrank key start stop            key             
                                                  
    
    zremrangebyscore key start stop           key             
                                                score
    
    zrevrange key start stop [withscores]       key         ,    ,      
                                             zrange key start stop   
    
    zrevrangebysocre key max min [withscores]       key           ,    ,      
    
    zrevrank key memeber                      key        ,key          
    
    zscore key member                         key member   
    
    zunionstore dest numkeys key [key...]                   ,      dest 
    
    zscan key cursor [match pattern] [count count]            (           )
                                                    uplooking01:6379> zscan website 1
                                                    1) "0"
                                                    2) 1) "www.jd.com"
                                                       2) "0.0050000000000000001"
                                                       3) "www.uplooking.com"
                                                       4) "0.02"
                                                       5) "www.baidu.com"
                                                       6) "0.050000000000000003"
                                                       7) "www.taobao.com"
                                                       8) "1"

    Redis共通コマンド
    keys *
              key
    type key
            key     
    select [0-15] 
                  
    rename oldKey newKey
         oldKey    newKey
                :
           :
                 :redis-3.2.0]# src/redis-cli -h uplooking03 -a uplooking
           :
                     :
                auth uplooking(  )
      redis     :
        src/redis-cli -h uplooking01 -p 6379 shutdown 
    
    1、      
          keys *        key
          exists name   redis   name  key
          del name   key name
          expire confirm 100   confirm  key100   
          ttl confirm   confirm   key     
          select 0    0    redis       0~15  16    
          move confirm 1         key          ,
          persist confirm   confirm  key     
          randomkey             key
          rename key2 key3    key2  key3
          type key2   key     
    2、       
          ping PONG          
          echo           
          select 0~15       
          quit  /exit      
          dbsize           key   
          info   redis     
          config get dir/*          
          flushdb              key
          flushall