Exemplo n.º 1
0
        def cached(*args):
            region_name = region[0] if type(region) is list else region
            # If router exist, use it to determine the cache region
            if router is not None:
                region_name = router(*args)
            if region_name is None:
                return func(*args)

            if cache.get(region_name) is None:
                reg = cache_regions.get(region_name)
                cache[region_name] = Cache._get_cache(namespace, reg)
            # If key generator exists, use it to generate a key
            if key_generator is not None:
                combined_args = decor_args + key_generator(*args)
            else:
                combined_args = decor_args + args[1:] if skip_self else args

            cache_key = get_cache_key(combined_args, func_name)

            def go():
                value = func(*args)
                # jsonify object to avoid pickle to get better performance
                return json.dumps(value)

            value = cache[region_name].get_value(cache_key, createfunc=go)
            return json.loads(value)
Exemplo n.º 2
0
        def cached(*args):
            region_name = region[0] if type(region) is list else region
            # If router exist, use it to determine the cache region
            if router is not None:
                region_name = router(*args)
            if region_name is None:
                return func(*args)

            if cache.get(region_name) is None:
                reg = cache_regions.get(region_name)
                cache[region_name] = Cache._get_cache(namespace, reg)
            # If key generator exists, use it to generate a key
            if key_generator is not None:
                combined_args = decor_args + key_generator(*args)
            else:
                combined_args = decor_args + args[1:] if skip_self else args

            cache_key = get_cache_key(combined_args, func_name)

            def go():
                value = func(*args)
                # jsonify object to avoid pickle to get better performance
                return json.dumps(value)

            value = cache[region_name].get_value(cache_key, createfunc=go)
            return json.loads(value)
Exemplo n.º 3
0
def region_invalidate(func, region, *args, namespace='smarter'):
    '''
    Invalidates a cache region

    :param func:  reference to a func
    :param region:  name of the region
    :param arg:  list of positional arguments
    :param namespace:  the namespace that is prepended in cache key
    '''
    reg = cache_regions.get(region)
    if reg is None:
        raise KeyError
    try:
        if isinstance(func, collections.Callable):
            func_name = func._func_name
    except:
        raise KeyError
    cache = Cache._get_cache(namespace, reg)
    cache_key = get_cache_key(args, func_name)
    cache.remove_value(cache_key)
Exemplo n.º 4
0
def region_invalidate(func, region, *args, namespace='smarter'):
    '''
    Invalidates a cache region

    :param func:  reference to a func
    :param region:  name of the region
    :param arg:  list of positional arguments
    :param namespace:  the namespace that is prepended in cache key
    '''
    reg = cache_regions.get(region)
    if reg is None:
        raise KeyError
    try:
        if isinstance(func, collections.Callable):
            func_name = func._func_name
    except:
        raise KeyError
    cache = Cache._get_cache(namespace, reg)
    cache_key = get_cache_key(args, func_name)
    cache.remove_value(cache_key)