国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python使用redis pool的一種單例實現方式

2020-01-04 17:32:27
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了Python使用redis pool的一種單例實現方式,結合實例形式分析了Python操作redis模塊實現共享同一個連接池的相關技巧,需要的朋友可以參考下
 

本文實例講述了Python使用redis pool的一種單例實現方式。分享給大家供大家參考,具體如下:

為適應多個redis實例共享同一個連接池的場景,可以類似于以下單例方式實現:

import redisclass RedisDBConfig:  HOST = '127.0.0.1'  PORT = 6379  DBID = 0def operator_status(func):  '''''get operatoration status  '''  def gen_status(*args, **kwargs):    error, result = None, None    try:      result = func(*args, **kwargs)    except Exception as e:      error = str(e)    return {'result': result, 'error': error}  return gen_statusclass RedisCache(object):  def __init__(self):    if not hasattr(RedisCache, 'pool'):      RedisCache.create_pool()    self._connection = redis.Redis(connection_pool = RedisCache.pool)  @staticmethod  def create_pool():    RedisCache.pool = redis.ConnectionPool(        host = RedisDBConfig.HOST,        port = RedisDBConfig.PORT,        db  = RedisDBConfig.DBID)  @operator_status  def set_data(self, key, value):    '''''set data with (key, value)    '''    return self._connection.set(key, value)  @operator_status  def get_data(self, key):    '''''get data by key    '''    return self._connection.get(key)  @operator_status  def del_data(self, key):    '''''delete cache by key    '''    return self._connection.delete(key)if __name__ == '__main__':  print RedisCache().set_data('Testkey', "Simple Test")  print RedisCache().get_data('Testkey')  print RedisCache().del_data('Testkey')  print RedisCache().get_data('Testkey')
 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 日喀则市| 宜宾县| 大邑县| 晋城| 望奎县| 怀远县| 稻城县| 榆树市| 赫章县| 仙桃市| 桂东县| 德令哈市| 正蓝旗| 鄂托克前旗| 江口县| 沐川县| 浮梁县| 武川县| 高陵县| 盐亭县| 故城县| 马山县| 麦盖提县| 东方市| 金堂县| 泽州县| 紫云| 会东县| 阿拉尔市| 泰州市| 湘潭县| 毕节市| 东海县| 望江县| 香河县| 宜宾市| 旬邑县| 搜索| 永嘉县| 南城县| 上栗县|