Last active
August 29, 2015 14:27
-
-
Save tawateer/2ecdffa8c78301703725 to your computer and use it in GitHub Desktop.
从 redis 多连接池中生成对象
This file contains hidden or 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
#-*- coding: utf-8 -*- | |
import redis | |
REDIS_HOST = "localhost" | |
REDIS_PORT = 6379 | |
REDIS_DB1 = 0 | |
REDIS_DB2 = 1 | |
REDIS_DB3 = 2 | |
def singleton(cls): | |
instances = {} | |
def _singleton(*args, **kw): | |
if cls not in instances: | |
instances[cls] = cls(*args, **kw) | |
return instances[cls] | |
return _singleton | |
@singleton | |
class RedisClient(object): | |
conn_pool = dict() | |
for redis_db in (REDIS_DB1, REDIS_DB2, REDIS_DB3): | |
conn_pool[redis_db] = redis.connection.BlockingConnectionPool( | |
host=REDIS_HOST, port=REDIS_PORT, | |
db=redis_db, timeout=60) | |
@classmethod | |
def get(cls, db): | |
return redis.client.Redis(connection_pool=cls.conn_pool[db]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment