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

首頁 > 編程 > Python > 正文

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

2019-11-25 16:50:36
字體:
來源:轉載
供稿:網友

本文實例講述了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')

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 封开县| 嘉黎县| 婺源县| 安岳县| 南投市| 渭源县| 如东县| 高尔夫| 元朗区| 宝丰县| 奎屯市| 深州市| 阳西县| 辛集市| 团风县| 南溪县| 商南县| 凌源市| 泰宁县| 东阿县| 巴彦县| 西乡县| 福州市| 留坝县| 崇明县| 方城县| 双鸭山市| 谢通门县| 荆门市| 黔西县| 南京市| 和顺县| 三都| 那曲县| 安乡县| 龙泉市| 白朗县| 观塘区| 高密市| 南岸区| 灵川县|