Redisの基本データ構造、基本操作

7300 ワード

<?php

/*

    @author:luowen

    @time:2013-08-07



1.String       

        Set name luowen    name = luowen   

        Get name                name  



        Setnx name luowen   name   luowen     ,   0      1

        Mset name luowen age 23 salary 233333                 ,   

        Msetnx name maomao age 23 hoby basketball                  

        Mget name age salary        



        Getset name maomao   name  ,       maomao



        Setrange name 3 maomao   name 3             luomaomao

        Getrange name 3 6    name      luomaomao



        Append name .com   nane  .com    luowen.com 



        Incr age              24

        Incrby age 6  name  6        



        Decr  incr  

        Decrby  decrby  



        Strlen             





2.Hash           



        Hset user:001 name luowen          user     001  name     luowen

        Hsetnx user name maomao             name   ,     



        Hget user:001 name   hash  user 001  



        Hmset user:003 name maomao age 23     

        Hmget user:003 name age     user:003  



        Hincrby user:003 age 3      hash  age   3



        Hexists user:003 name   hash      name  



        Hlen user:003   hash          

        Hkeys user:003   hash      

        Hvals user:003   hash      

        Hgetall user:003          



        Hdel user:003 name  hash name      



3.list    (    )

    1. :       :    

    2.lpush     

        Lpush list1 “world” 

        lpush list1 ‘hello”



        Lrange list1 0 -1         0      

        Word

        hello



    3 rpush      

        rpush list2 “world” 

        rpush list2 “luowen” 



        lrange list2 0 -1

        world

        luowen

    4.linsert       

        Rpush list3 luowen

        Rpush list3 maomao

        Lrange list3 0 -1

        Luowen

        Maomao

        

        Linsert list3 before maomao love

        Lrange list3 0 -1

        Luowen

        Love

        maomao





        Linsert list3 after luowen love

        Lrange list3 0 -1

        Luowen

        Love

        Maomao



    5 lset        

        Rpush list5 luowen

        Rpush list5 maomao



        Lset list5 0 “deom”



        Demo

        maomao 



    6 lrem   list     

        Rpush list6 luowen

        Rpush list6 luowen1

        Rpush list6 luowen2

        Rpush list6 luowen3

        Rpush list6 luowen4



        Lrem list6 1 “luowen”

          list6    luowen  

    7.ltrim 

        Lpush list7 luowen1

        Lpush list7 luowen2

        Lpush list7 luowen3

        Lpush list7 luowen4

        Lpush list7 luowen5



        Ltrim list7 1 2 (1 2       )

        Lpush list7 luowen2

        Lpush list7 luowen3

    8 lpop             

        Lpush list8 luowen1

        Lpush list8 luowen2

        Lpush list8 luowen3



        Lpop list8 

        Lpush list8 luowen2

        Lpush list8 luowen3

    9 rpop             

        Lpush list8 luowen1

        Lpush list8 luowen2

        Lpush list8 luowen3



        rpop list8 

        Lpush list8 luowen1

        Lpush list8 luowen2

    10 rpoplpush        ,            

        

        List demo1 

        Demo1A

        Demo1B

        Demo1C



        List demo2

        Demo2A

        Demo2B

        Demo2C



        Rpoplpush demo1 demo2



        List demo1 

                Demo1A

        Demo1B



        List demo2

        Demo1C

        Demo2A

        Demo2B

        Demo2C

    11 lindex     list      

        List11

        one

        two

        

        lindex list11 1(list  )

        two

        lindex list11 0

        one

    12 llen             

4.set        

    1 sadd           

        Sadd myset1 luowen

    2 srem           

        Srem myset1 luowen

    3 smembers         

        Smembers myset1

    4 spop            ,    

    5 sdiff                   ,          

        Setdemo1

        One

        two

        

        setdemo2

        one 

        three    

        

        sdiff setdemo1 setdemo2

        two( setdemo2   )

        sdiff setdemo2 setdeo1

        three( setdemo1    )

    6 sdiffstroe               

        

        Sdiffstore setdemo1 setdemo2 setdemo3

         setdemo1 setdemo2       setdemo3 

    7 sinter         

    8 sinterstore                   

    9 sunion        

    10 sunionstore                   

    11 smove                    

        Eg smove myset1 mysetA two mysetB     two     mysetB 

    12 scard           

        Scard myset1  myset12     

    13 sismember            

        Sismember myset13 luowen   luowen   myset13    

    14 srandmember myset14     myset1     



    15 zadd          

        Zadd myzsent 1 luowen1

        Zadd myzsent 2 luowen2

        Zadd myzsent 3 luowen3

        Zadd myzsent 4 luowen4



        Zrange myzsent 0 -1 withscores

    16 zrem           

        Zrem myzsent luowen1   myzsent    luowen1

    17 zincrby myzsent luowen1 3 myzsent luown1      4

            ,    

    18 zrank   myzsent       

    19 zrevrank       

    20 zrangebyscore           

        Zrangebyscore mysetdeom 2 5 withscores

          mysetdemo 2-5    

    21 zcount          

        Zcount myset 2  4   2 4      

    22 zcard             

    23 zremrangbyrank             ,        

    24 zremrangbyscore          ,       



5 Redis    



    Key-values

    1 keys *        .      keys my*      my    

    2 exists       exists name     name       

    3 del     del name   name  

    4 expire        expire key time 

    5 ttl key         

    6 select database      

    7 move key dababase1  key  dao database1      

    8 persist          

    9 randomkey          

    10 rename       

    11 type key   key     



    Server

    1 ping ping                

    2 echo    echo demo    

    3 select       select 0-16    

    4 quit exit     

    5 dbsize           

    6 info          

    7 config get         

    8 flush db      

    9 flushall             

6 Redis     



    1           requirepass password

    2     auth            :          –a     

    3     :

        One:   master         slave

        Two:   salve     master          salve   

      salve

          salveof         ip    

                      masterauth   

    4 redis      

          :multi        

        Set age 10

        Set age 144

        -----------------------------------------------------------

                     

    Exec 

          age 144

          

        Discard    

        

        Watch       

    5 Redis    

           :  snapshotting (  )             save 500 32 500   32               

           : append only file                

          :  append only

                Appendfsync yes

                Appendfsync always      

                Appendfsync everysec          

                Appendfsync no       

    6        

          :

    Subscribe tv1 tv2        

          :

            Publish tv1 luweo

         :publish tv1              

    7     

           :               

       :           slave