python连接redis单例模式

import redis  

class RedisModel(object): 
    HOST = '127.0.0.1'  
    PORT = 6379  
    DBID = 0   

    def __init__(self):  
        if not hasattr(RedisModel, 'pool'):  
            RedisModel.create_pool()  
        self._connection = redis.Redis(connection_pool =  RedisModel.pool) 
     #python中,所有类的实例中的成员变量,都是公用一个内存地址,因此,及时实例化多个RedisCache类,内存中存在的pool也只有一个
    @staticmethod  
    def create_pool():  
        RedisModel.pool = redis.ConnectionPool(  
                host = RedisDBModel.HOST,  
                port = RedisDBModel.PORT,  
                db   = RedisDBModel.DBID)  

你可能感兴趣的:(数据库,Python)