Python操作Redis的redis模块对字符串(string)的主要操作函数包括:SET、GET、GETSET、SETEX、SETNX、MSET、MSETNX、INCR(INCRBY,DECR,DECRBY在python中庸同一个函数incr实现)、APPEND。其他的一些方法在Python的redis无法实现redis命令行下的操作效果,诸如SETRANGE、STRLEN等命令无法实现,代码注释内容有所体现。
函数说明
- SET: 为指定的键(key)设置值(value), set(self, name, value, **kwargs)。
- GET:获取指定键(key)绑定的值(value),get(self, name)。
- GETSET: 为指定的键(key)设置新的值(value),并返回旧的值(old Value),getset(self, name, value)
- SETEX:为指定的键(key)设置过期以秒(second)计的过期时间,setex(self, name, value, time)
- SETNX: 键(key)不存在时,为键(key)指定值(value),setnx(self, name, value)
- MSET:一次性设置多个键-值(key-value)对,函数设置的键-值对(即mapping所指内容)数据要以Python字典数据类型传入,mset(self, mapping)
- MSETNX:键-值(key-value)对不存在时,设置键-值(key-value)对,msetnx(self, mapping),mapping值参考6。
- INCR:自增函数,默认步长为1,通过对步长(amount)大小以及征服的控制实现了INCRBY(amount>=1)、DECR(amount=-1)、DECRBY(amount<=-1)等函数功能,incr(self, name, amount=1)
- 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
)