python redis

顯例:
python

import redis
class RedisUtils(object):
    def __init__(self):
        if not hasattr(RedisUtils, 'pool'):
            RedisUtils.getPool()  #建立redis鏈接pool
        self.coon = redis.Redis(connection_pool=RedisUtils.pool)
    @staticmethod
    def getPool():
        RedisUtils.pool = redis.ConnectionPool(host='192.168.18.199', port=6379, password='sss', db=10, decode_responses=True)
    def set(self, key, value, exps=None):
        if exps:
            self.coon.set(key, value, ex=30)
        else:
            self.coon.set(key, value)
    def get(self, key):
        return self.coon.get(key)
if __name__ == '__main__':
    # utils = RedisUtils()
    # utils.set("sea", "test")
    # utils.set("xixi", "test", 10)
    # print(utils.get("sea"))
    conn_redis = redis.Redis(host='192.168.18.199', port=6379, password='xxx', db=10)
    conn_redis.set('娘子', '啊哈')
    conn_redis.set('gg', '啊哈', ex=10)
    print(conn_redis.get("gg").decode("utf-8"))

 

 

 

不使用鏈接池:
 redis

import redis
conn_redis = redis.Redis(host='127.0.0.1', port=6379, password='123456', db=1)
conn_redis.set('娘子', '啊哈')
conn_redis.set('娘子', '啊哈',ex=30) #30s 過時

 

python 操做redis,使用鏈接池:ide

redis-py使用connection pool來管理對一個redis server的全部鏈接,避免每次創建、釋放鏈接的開銷。默認,每一個Redis實例都會維護一個本身的鏈接池。能夠直接創建一個鏈接池,而後做爲參數Redis,這樣就能夠實現多個Redis實例共享一個鏈接池。code

def getcoon():
      pool = redis.ConnectionPool(host='192.168.25.126', port=6379, password='123456', db=0)
      coon = redis.Redis(connection_pool=pool)
      coon.set('key', 'value')
      res = coon.get('key')
      return res

 

coon.set('sea', 'hahhahaha', ex=30)    # 過時時間單位s
set(name, value, ex=None, px=None, nx=False, xx=False)
在 Redis 中設置值,默認,不存在則建立,存在則修改。
參數:
ex - 過時時間(秒)
px - 過時時間(毫秒)
nx - 若是設置爲True,則只有name不存在時,當前set操做才執行
xx - 若是設置爲True,則只有name存在時,當前set操做才執行

 

 

redis 使用鏈接池操做server

class OPRedis(object):

    def __init__(self):
        if not hasattr(OPRedis, 'pool'):
            OPRedis.getRedisCoon()  #建立redis鏈接
        self.coon = redis.Redis(connection_pool=OPRedis.pool)

    @staticmethod
    def getRedisCoon():
        OPRedis.pool = redis.ConnectionPool(host=redisInfo['host'], password=redisInfo['password'], port=redisInfo['port'], db=redisInfo['db'])

    """
    string類型 {'key':'value'} redis操做
    """

    def setredis(self, key, value, time=None):
        #非空即真非0即真
        if time:
            res = self.coon.setex(key, value, time)
        else:
            res = self.coon.set(key, value)
        return res

    def getRedis(self, key):
        res = self.coon.get(key).decode()
        return res

    def delRedis(self, key):
        res = self.coon.delete(key)
        return res

    """
    hash類型,{'name':{'key':'value'}} redis操做
    """
    def setHashRedis(self, name, key, value):
        res = self.coon.hset(name, key, value)
        return res


    def getHashRedis(self, name, key=None):
        # 判斷key是否我爲空,不爲空,獲取指定name內的某個key的value; 爲空則獲取name對應的全部value
        if key:
            res = self.coon.hget(name, key)
        else:
            res = self.coon.hgetall(name)
        return res


    def delHashRedis(self, name, key=None):
        if key:
            res = self.coon.hdel(name, key)
        else:
            res = self.coon.delete(name)
        return res

redisInfo配置utf-8

 
redisInfo = {
    "host": '192.168.1.112',
    "password": '123456',
    "port": 6379,
    "db": 0
}
相關文章
相關標籤/搜索