Introduction to Python's singleton implementation using Redis pool

Source: Internet
Author: User
Tags delete cache
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 ')
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.