Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def _cache_wrapper(func, timeout, unique_arg):
- """
- :param func: the function that is being decorated
- :param timeout: when should the cache time out
- :param unique_arg: in case there are too many parameter,
- the user can choose to create the hash using one
- unique argument instead of many
- """
- def wrapper(*args, **kwargs):
- """
- Wrapper that checks the cache if it has anything then
- """
- # Generate Key given parameters and function name
- key = generate_key(args, kwargs, unique_arg, func.__name__)
- # Get the result from the cache
- result = cache.get(key)
- # Call the function and update the cache if the result is not found
- if result is None:
- result = func(*args, **kwargs)
- cache.set(key, result, timeout)
- # Open a new thread and check if the cache needs updating
- if result is not None:
- t = Thread(target=_check_and_update_cache, args=(func, args, kwargs, key,))
- t.start()
- return result
- return wrapper
- def cache_output(timeout=60 * 60, unique_arg=None):
- """
- Decorator function that calls the cache wrapper which handles the generation
- of the cache keys, the storage of the function output and the update
- mechanism.
- """
- def decorator_function(func):
- return _cache_wrapper(func, timeout, unique_arg)
- return decorator_function
Add Comment
Please, Sign In to add comment