Created
April 5, 2016 02:21
-
-
Save leafsummer/0218bed8aea6976c9940c7f7ce3ba8c1 to your computer and use it in GitHub Desktop.
singleton pattern redis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RedisClient(redis.StrictRedis): | |
""" | |
Singleton pattern | |
http://stackoverflow.com/questions/42558/python-and-the-singleton-pattern | |
""" | |
_instance = {} | |
def __init__(self, server): | |
redis.StrictRedis.__init__(self, **server) | |
def __new__(cls, *args): | |
if not cls._instance.get(str(args)): | |
cls._instance[str(args)] = super(RedisClient, cls).__new__(cls) | |
return cls._instance[str(args)] | |
def singleton(_cls): | |
inst = {} | |
def getinstance(*args, **kwargs): | |
if _cls not in inst: | |
inst[_cls] = _cls(*args, **kwargs) | |
return inst[_cls] | |
return getinstance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment