【python】使用redis pool的一种单例实现方式
来源:互联网 发布:淘宝店铺代码下载 编辑:程序博客网 时间:2024/06/05 09:09
为适应多个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')
0 0
- 【python】使用redis pool的一种单例实现方式
- [Unity3D]单例的一种实现方式
- Python单例模式的实现方式
- 单例模式(二)-------------一种更好的单例实现方式
- 使用Redis实现抢购的一种思路
- Redis存储Key的一种设计实现方式:模式匹配
- python实现redis客户端单例+hbase客户端单例
- 单例模式的一种实现方法
- 单例模式的一种实现方法
- 一种单例的实现方法
- C 语言单例的一种实现
- C++单例模式的一种实现
- python一种用staticmethod实现单例模式方法的探讨
- python一种用classmethod实现单例模式方法的探讨
- 创建单例winform应用程序的一种更好的方式
- 关于Python中一种回调方式的实现
- 使用 __new__ 实现 Python 的单例模式
- 循环赛问题 日程安排的一种排列方式 使用C++实现
- 职场中频繁跳槽引起的危害
- qt: exited with code -1073741515( 0xc0000135)
- OpenGL光照
- [翻译] AnimatedTransitionGallery
- 物流定位系统项目qt代码
- 【python】使用redis pool的一种单例实现方式
- 关于mongodb ,redis,memcache之间见不乱理还乱的关系和作用
- 鲍尔默密会快船老板娘,微软前CEO要玩球
- 教你如何迅速秒杀掉:99%的海量数据处理面试题
- 界面开发感悟
- WINCE6.0 + S3C2443 RTC initialization and problems of
- vector的reserve和resize
- 关于input标签缓存的问题。
- 浮华的世态,只会将一颗心,涂染得色彩缤纷,失去往日纯净的姿态