Exemplo n.º 1
0
def redis_cached(cache_miss_func):
    """Decorator that caches a function + parameters to a Redis instance.
    """
    config_settings = config_layer.get_config()
    expiration = config_settings['REDIS_EXPIRATION']

    def inner(*args, **kwargs):
        key = get_redis_key(cache_miss_func, args)

        redis_conn = get_redis_connection()
        prior = redis_conn.get(key)

        if not prior:
            ret_val = cache_miss_func(*args, **kwargs)
            redis_conn.setex(key, json.dumps(ret_val), expiration)
            print 'set ' + key
        else:
            ret_val = json.loads(prior)

        return ret_val

    def inner_guarded(*args, **kwargs):
        try:
            # Try to connect to the redis cache and get cached value
            return inner(*args, **kwargs)
        except Exception as e:
            # If failed to connect to redis cache
            print 'cache fail -', e
            return cache_miss_func(*args, **kwargs)

    return inner_guarded
Exemplo n.º 2
0
def redis_cached(cache_miss_func):
    """Decorator that caches a function + parameters to a Redis instance.
    """
    config_settings = config_layer.get_config()
    expiration = config_settings['REDIS_EXPIRATION']

    def inner(*args, **kwargs):
        key = get_redis_key(cache_miss_func, args)

        redis_conn = get_redis_connection()
        prior = redis_conn.get(key)

        if not prior:
            ret_val = cache_miss_func(*args, **kwargs)
            redis_conn.setex(key, json.dumps(ret_val), expiration)
            print 'set ' + key
        else:
            ret_val = json.loads(prior)

        return ret_val


    def inner_guarded(*args, **kwargs):
        try:
            # Try to connect to the redis cache and get cached value
            return inner(*args, **kwargs)
        except Exception as e:
            # If failed to connect to redis cache
            print 'cache fail -', e
            return cache_miss_func(*args, **kwargs)

    return inner_guarded
Exemplo n.º 3
0
    def __init__(self):
        config_settings = config_layer.get_config()

        host = config_settings['REDIS_HOST']
        port = config_settings['REDIS_PORT']
        db = config_settings['REDIS_DB']
        password = config_settings['REDIS_PASSWORD']
        expiration = config_settings['REDIS_EXPIRATION']

        self.redis_conn = redis.Redis(host=host,
                                      port=port,
                                      db=db,
                                      password=password)
Exemplo n.º 4
0
    def __init__(self):
        config_settings = config_layer.get_config()

        host = config_settings['REDIS_HOST']
        port = config_settings['REDIS_PORT']
        db = config_settings['REDIS_DB']
        password = config_settings['REDIS_PASSWORD']
        expiration = config_settings['REDIS_EXPIRATION']

        self.redis_conn = redis.Redis(
            host=host,
            port=port,
            db=db,
            password=password
        )