您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Python使用redis pool的单例实现方式介绍

2024/3/25 2:57:17发布23次查看
本文实例讲述了python使用redis pool的一种单例实现方式。分享给大家供大家参考,具体如下:
为适应多个redis实例共享同一个连接池的场景,可以类似于以下单例方式实现:
import redis class redisdbconfig: host = '127.0.0.1' port = 6379 dbid = 0 def 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_status class 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使用redis pool的单例实现方式介绍。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product