Ejemplo n.º 1
0
    def __new__(cls, db, *args, **kwargs):
        redis_conf = current_app.config["REDIS_CONF"]
        if not cls.redis[db]:
            # 支持多线程
            with cls._instance_lock:
                if not cls.redis[db]:
                    o = Redis.__new__(cls, *args)
                    # Redis.__init__(o, db=db, **cls.redis_conf)
                    Redis.__init__(o, db=db, **redis_conf)
                    cls.redis.insert(db, o)

        return cls.redis[db]
Ejemplo n.º 2
0
from redis import Redis

api_blueprint = Blueprint("api", __name__, url_prefix="/api")
rest_api = Api(api_blueprint)
jwt = JWTManager()
# Restplus implements its own error handlers while JWT's error handlers are implemented with flask's native error
# handlers. Restplus catches the error before the native Flask implementation does and therefore, the error never makes
# it to Flask JWT. This means that the error messages JWT wants to throw never make it to the client. Here, we use a
# protected JWT method to add the error handlers manually to the restplus api instance.
# https://github.com/vimalloc/flask-jwt-extended/issues/83 and https://github.com/vimalloc/flask-jwt-extended/issues/86
# noinspection PyProtectedMember
jwt._set_error_handler_callbacks(rest_api)

# Construct now, defer initialization to runtime
redis_db = Redis.__new__(Redis)


def init_redis_db(config):
    """
    Defers Redis database initialization to runtime so that different configurations can be used for dev, test, & prod.
    """

    redis_db.__init__(host=config.REDIS_HOST,
                      port=config.REDIS_PORT,
                      db=config.REDIS_DB,
                      password=config.REDIS_DB_PASS,
                      decode_responses=True)
    return redis_db