Python 操作redis

redis-py的API文档

一 String 字符串
import redis
class RedisStringTest(object):
        def __init__(self):
                self.r = redis.StrictRedis(host="localhost",port="6379",db = 0)
        def test_set(self,key,value):
                ''' set a record at a time'''
                return self.r.set(key,value)
        def test_get(self,key):
                ''' get a value at a time  '''
                return self.r.get(key)
        def test_mset(self,dic):
                ''' set multi records in dict form at a time'''
                return self.r.mset(dic)
        def test_mget(self,key_list):
                ''' get multi values for multi keys in list form at a time '''
                return self.r.mget(key_list)
        def test_del(self,key):
                ''' delete records: a record or a list of records'''
                return self.r.delete(key)
        def test_append(self,key,append_val):
                ''' append something to the end of the value of a certain key'''
                return self.r.append(key,append_val)
        def test_incr(self,key):
                ''' plus 1'''
                return self.r.incr(key)
        def test_decr(self,key):
                ''' minus 1'''
                return self.r.decr(key)

if __name__ == "__main__":
        strobj = RedisStringTest()
        strobj.test_set("s1",1)
        strobj.test_incr("s1")
        strobj.test_del("s2")
        print (strobj.test_mget(["s1","s2"]))
        strobj.test_decr("s1")
        print (strobj.test_get("s1"))

二 list 列表
import redis
class RedisListTest(object):
        def __init__(self):
                self.r = redis.StrictRedis(host="localhost",port="6379",db=0)
        def test_lpush(self,queue,values):
                ''' insert values to the left of the queue.
                    values can be a string or a list
                '''
                return self.r.lpush(queue,*values)   # notice the * to split value list to single value
        def test_rpush(self,queue,values):
                ''' insert values to the right of the queue.
                    values can be a string or a list
                '''
                return self.r.rpush(queue,*values)
        def test_lpushx(self,queue,value):
                ''' insert values to the left of the queue.
                    values must be a string
                '''
                return self.r.lpushx(queue,value)
        def test_rpushx(self,queue,value):
                ''' insert values to the right of the queue.
                    values must be a string
                '''
                return self.r.rpushx(queue,value)
        def test_lrange(self,queue,start_idx,end_idx):
                ''' get values in queue from start_idx to end_idx '''
                return self.r.lrange(queue,start_idx,end_idx)
        def test_ltrim(self,queue,start_idx,end_idx):
                ''' reserve part values of the queue
                    from start_idx to end_idx
                '''
                return self.r.ltrim(queue,start_idx,end_idx)
        def test_lpop(self,queue):
                ''' pop out the left value from the queue'''
                return self.r.lpop(queue)
        def test_rpop(self,queue):
                ''' pop out the right value from the queue'''
                return self.r.rpop(queue)

if __name__ == "__main__":
        listtest = RedisListTest()
        listtest.test_lpush("q1",[1,2,3,4,5])
        listtest.test_lpushx("q1",10)
        print (listtest.test_lrange("q1",0,-1))
        listtest.test_ltrim("q1",0,2)
        print (listtest.test_lrange("q1",0,-1))
        print (listtest.test_lpop("q1"))
        print (listtest.test_rpop("q1"))
三 set 集合
import redis
class RedisSetTest(object):
        def __init__(self):
                self.r = redis.StrictRedis(host="localhost",port="6379",db=0)
        def test_sadd(self,set_name,values):
                ''' add values to the set
                    values could be a string or a list
                '''
                return self.r.sadd(set_name,*values)
        def test_sremove(self,set_name,values):
                ''' remove values from the set
                    set could be a string or a list
                '''
                return self.r.srem(set_name,*values)
        def test_smembers(self,set_name):
                ''' show values in the set'''
                return self.r.smembers(set_name)
        def test_sinter(self,set1,set2):
                ''' return the common values both in set1 and set2'''
                return self.r.sinter(set1,set2)
        def test_sdiff(self,set1,set2):
                ''' return the values in set1 but not in set2'''
                return self.r.sdiff(set1,set2)
        def test_sunion(self,set1,set2):
                ''' return the unions of two sets'''
                return self.r.sunion(set1,set2)

if __name__ == "__main__":
        set_redis = RedisSetTest()
        #set_redis.test_sremove("set2",["cats","dogs","pigs"])
        set_redis.test_sadd("set1",["cats","dogs","pigs"])
        set_redis.test_sadd("set2",["cats","dogs","ants"])
        # show the members of set1

你可能感兴趣的:(Python 操作redis)