示例#1
0
def init_redis(registry: Registry,
               connection_url=None,
               redis_client=StrictRedis,
               **redis_options):
    """Sets up Redis connection pool once at the start of a process.

    Connection pool life cycle is the same as Pyramid registry which is the life cycle of a process (all threads).
    """

    # if no url passed, try to get it from pyramid settings
    url = registry.settings.get(
        'redis.sessions.url') if connection_url is None else connection_url
    # otherwise create a new connection
    if url is not None:
        # remove defaults to avoid duplicating settings in the `url`
        redis_options.pop('password', None)
        redis_options.pop('host', None)
        redis_options.pop('port', None)
        redis_options.pop('db', None)
        # the StrictRedis.from_url option no longer takes a socket
        # argument. instead, sockets should be encoded in the URL if
        # used. example:
        #     unix://[:password]@/path/to/socket.sock?db=0
        redis_options.pop('unix_socket_path', None)
        # connection pools are also no longer a valid option for
        # loading via URL
        redis_options.pop('connection_pool', None)
        redis = redis_client.from_url(url, **redis_options)
    else:
        raise RuntimeError(
            "Redis connection options missing. Please configure redis.sessions.url"
        )

    registry.redis = redis
示例#2
0
def init_redis(registry: Registry, connection_url=None, redis_client=StrictRedis, **redis_options):
    """Sets up Redis connection pool once at the start of a process.

    Connection pool life cycle is the same as Pyramid registry which is the life cycle of a process (all threads).
    """

    # if no url passed, try to get it from pyramid settings
    url = registry.settings.get('redis.sessions.url') if connection_url is None else connection_url
    # otherwise create a new connection
    if url is not None:
        # remove defaults to avoid duplicating settings in the `url`
        redis_options.pop('password', None)
        redis_options.pop('host', None)
        redis_options.pop('port', None)
        redis_options.pop('db', None)
        # the StrictRedis.from_url option no longer takes a socket
        # argument. instead, sockets should be encoded in the URL if
        # used. example:
        #     unix://[:password]@/path/to/socket.sock?db=0
        redis_options.pop('unix_socket_path', None)
        # connection pools are also no longer a valid option for
        # loading via URL
        redis_options.pop('connection_pool', None)
        redis = redis_client.from_url(url, **redis_options)
    else:
        raise RuntimeError("Redis connection options missing. Please configure redis.sessions.url")

    registry.redis = redis