Skip to content

Instantly share code, notes, and snippets.

@leafsummer
Created April 5, 2016 02:21
Show Gist options
  • Save leafsummer/0218bed8aea6976c9940c7f7ce3ba8c1 to your computer and use it in GitHub Desktop.
Save leafsummer/0218bed8aea6976c9940c7f7ce3ba8c1 to your computer and use it in GitHub Desktop.
singleton pattern redis
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