国产探花免费观看_亚洲丰满少妇自慰呻吟_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')
 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 金川县| 松桃| 嫩江县| 湘潭市| 蒲城县| 光泽县| 三门县| 开原市| 理塘县| 通海县| 泽州县| 永城市| 纳雍县| 新野县| 庆云县| 成武县| 固镇县| 徐水县| 贺兰县| 麦盖提县| 曲周县| 白朗县| 方正县| 黄大仙区| 南部县| 潞城市| 安丘市| 连山| 惠安县| 塔城市| 乌海市| 禹州市| 兴化市| 建昌县| 旬邑县| 怀化市| 镇平县| 浦城县| 民县| 闵行区| 长沙市|