1. 程式人生 > >3.Python操作Redis:字串(String)

3.Python操作Redis:字串(String)

Python操作Redis的redis模組對字串(string)的主要操作函式包括:SET、GET、GETSET、SETEX、SETNX、MSET、MSETNX、INCR(INCRBY,DECR,DECRBY在python中庸同一個函式incr實現)、APPEND。其他的一些方法在Python的redis無法實現redis命令列下的操作效果,諸如SETRANGE、STRLEN等命令無法實現,程式碼註釋內容有所體現。

函式說明

  1. SET: 為指定的鍵(key)設定值(value), set(self, name, value, **kwargs)。
  2. GET:獲取指定鍵(key)繫結的值(value),get(self, name)。
  3. GETSET: 為指定的鍵(key)設定新的值(value),並返回舊的值(old Value),getset(self, name, value)
  4. SETEX:為指定的鍵(key)設定過期以秒(second)計的過期時間,setex(self, name, value, time)
  5. SETNX: 鍵(key)不存在時,為鍵(key)指定值(value),setnx(self, name, value)
  6. MSET:一次性設定多個鍵-值(key-value)對,函式設定的鍵-值對(即mapping所指內容)資料要以Python字典資料型別傳入,mset(self, mapping)
  7. MSETNX:鍵-值(key-value)對不存在時,設定鍵-值(key-value)對,msetnx(self, mapping),mapping值參考6。
  8. INCR:自增函式,預設步長為1,通過對步長(amount)大小以及征服的控制實現了INCRBY(amount>=1)、DECR(amount=-1)、DECRBY(amount<=-1)等函式功能,incr(self, name, amount=1)
  9. APPEND: 為指定的字串追加值,若不存在則直接建立, append(self, key, value)

程式碼示例

#!/usr/bin/python
import redis
import time

## Connect local redis service
client =redis.Redis(host='127.0.0.1'
,port=6379) print "Connection to server successfully!" client.set("tutorial-name","Redis tutorial") print "Stored srting in redis: ", print client.get("tutorial-name") # Get Key's children string(unable to achieve command GETRANGE) key = "key1" string ="This my test key" client.set(key,string) rangeString=client.substr(key,0,5) print "The child string for key ",key," child vlaue list",rangeString # Binding a new value to the key, return the old value(GETSET) oldVal=client.getset("tutorial-name","Redis") print "Get old value: ",oldVal print "New value:",client.get("tutorial-name") # getbit method not exists in the module(GETBIT) #client.set("tutorial-name","Redis tutorial") #print client.getbit("tutorial-name",2) # setex(self, name, value, time) command SETEX # Set the value of key ``name`` to ``value`` # that expires in ``time`` seconds # ttl(self) command TTL client.setex('mykey','mysql',10) time.sleep(2) timeLeft=client.ttl('mykey') print "Get left live time",timeLeft print "Get the value",client.get('mykey') #setnx Set the value of key ``name`` to ``value`` if key doesn't exist. (SETNX) client.setnx('mykey','PostgreSQL') client.setnx('mykey1','MongoDB') print "Get existed key's new value",client.get('mykey') print "Get new built value",client.get('mykey1') # no SETRANGE # no STRLEN # mset, Sets each key in the ``mapping`` dict to its corresponding value client.mset({'key1':'Redis','key2':'MySQl'}) print "Get key1 mapping value",client.get('key1') print "Get key2 mapping value",client.get('key2') # msetnx: Sets each key in the ``mapping`` dict to its corresponding value if # none of the keys are already set client.msetnx({'mykeyI':'Redis','mykeyII':'MySQl'}) print "Get mykeyI mapping value",client.get('mykeyI') print "Get mykeyII mapping value",client.get('mykeyII') # no PSETEX # incr method achieved both INCR and INCRBY command line in redis # incr, iIncrements the value of ``key`` by ``amount``. # If no key exists, the value will be initialized as ``amount`` # if not set the increment number, the default is 1. # We can set amount an negative value get DECR/DECRBY method. client.set('page_num',20) client.incr('page_num',-2) print "The increased value",client.get('page_num') # append(self,key,value), # Appends the string ``value`` to the value at ``key``. If ``key`` # doesn't already exist, create it with a value of ``value``. # Returns the new length of the value at ``key``. client.append('mykey',' DB2') print "Get appended string",client.get('mykey') hashVal = client.hgetall('profile') print hashVal #Empty db client.flushdb()

參考資料
1、Redis 字串(String)
2、Python redis文件(python互動模式下命令>>>help redis