The example in this article describes a single implementation of Python pool using Redis. Share to everyone for your reference, as follows:
To accommodate scenarios in which multiple Redis instances share the same pool of connections, you can do this in a single way similar to the following:
Import Redisclass redisdbconfig:host = ' 127.0.0.1 ' PORT = 6379 DBID = 0def Operator_status (func): ' ' ' Get Operatorat Ion 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 Redis Cache (object): Def __init__ (self): If not hasattr (Rediscache, ' pool '): Rediscache.create_pool () self._connecti On = Redis. Redis (Connection_pool = rediscache.pool) @staticmethod def create_pool (): Rediscache.pool = Redis. ConnectionPool (host = redisdbconfig.host, port = redisdbconfig.port, db = Redisdbconfig.dbid) @ope Rator_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 Redis Cache (). Set_data (' Testkey ', "simple Test") print Rediscache (). Get_data (' Testkey ') print Rediscache (). Del_data (' Testkey ') print Rediscache (). Get_data (' Testkey ')
More readers interested in Python related content can view this site topic: "Python function Tips Summary", "Python string manipulation Tips", "Python Introductory and Advanced Classic tutorial" and "Python file and directory Operations Tips Summary"
I hope this article is helpful for Python program design.