huskar_api/models/utils.py
def make_cache_decorator(redis_client):
    """Creates a decorator to apply cache on arguments."""

    def cache_on_arguments(expiration_time):
        def decorator(fn):
            fn_generate_key = function_key_generator(
                'cache_on_arguments:v1', fn, to_str=unicode)
            fn_args = inspect.getargspec(fn)
            fn_has_self = fn_args[0] and fn_args[0][0] in ('self', 'cls')

            @functools.wraps(fn)
            def wrapper(*args, **kwargs):
                key = fn_generate_key(*args, **kwargs)
                val = redis_client.get(key)
                if val is None:
                    val = fn(*args, **kwargs)
                    redis_client.set(key, zdumps(val), expiration_time)
                    return val
                else:
                    return zloads(val)

            def generate_key(*args, **kwargs):
                args = ((None,) + args) if fn_has_self else args
                return fn_generate_key(*args, **kwargs)

            def flush(*args, **kwargs):
                return redis_client.delete(generate_key(*args, **kwargs))

            wrapper.generate_key = generate_key
            wrapper.flush = flush
            wrapper.original = fn

            return wrapper
        return decorator
    return cache_on_arguments
Creates a decorator to apply cache on arguments.
Creates a decorator to apply cache on arguments.
Creates a decorator to apply cache on arguments .
def make_cache_decorator(redis_client):
    def cache_on_arguments(expiration_time):
        def decorator(fn):
            fn_generate_key = function_key_generator(
                'cache_on_arguments:v1', fn, to_str=unicode)
            fn_args = inspect.getargspec(fn)
            fn_has_self = fn_args[0] and fn_args[0][0] in ('self', 'cls')
            @functools.wraps(fn)
            def wrapper(*args, **kwargs):
                key = fn_generate_key(*args, **kwargs)
                val = redis_client.get(key)
                if val is None:
                    val = fn(*args, **kwargs)
                    redis_client.set(key, zdumps(val), expiration_time)
                    return val
                else:
                    return zloads(val)
            def generate_key(*args, **kwargs):
                args = ((None,) + args) if fn_has_self else args
                return fn_generate_key(*args, **kwargs)
            def flush(*args, **kwargs):
                return redis_client.delete(generate_key(*args, **kwargs))
            wrapper.generate_key = generate_key
            wrapper.flush = flush
            wrapper.original = fn
            return wrapper
        return decorator
    return cache_on_arguments
def make_cache_decorator ( redis_client ) : def cache_on_arguments ( expiration_time ) : def decorator ( fn ) : fn_generate_key = function_key_generator ( 'cache_on_arguments:v1' , fn , to_str = unicode ) fn_args = inspect . getargspec ( fn ) fn_has_self = fn_args [ 0 ] and fn_args [ 0 ] [ 0 ] in ( 'self' , 'cls' ) @ functools . wraps ( fn ) def wrapper ( * args , ** kwargs ) : key = fn_generate_key ( * args , ** kwargs ) val = redis_client . get ( key ) if val is None : val = fn ( * args , ** kwargs ) redis_client . set ( key , zdumps ( val ) , expiration_time ) return val else : return zloads ( val ) def generate_key ( * args , ** kwargs ) : args = ( ( None , ) + args ) if fn_has_self else args return fn_generate_key ( * args , ** kwargs ) def flush ( * args , ** kwargs ) : return redis_client . delete ( generate_key ( * args , ** kwargs ) ) wrapper . generate_key = generate_key wrapper . flush = flush wrapper . original = fn return wrapper return decorator return cache_on_arguments
Creates a decorator to apply cache on arguments.
Creates a decorator to apply cache on arguments .
"""Creates a decorator to apply cache on arguments."""
redis_client