Created
July 29, 2019 07:30
Star
You must be signed in to star a gist
how to cache with non immutable function inputs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def cache_load_utturances(ttl=360000): | |
""" | |
Decorator for wrapping simple cache around load_utterances. | |
Since some arguments are unhashable (tokenizer) or immutable (list) we need to make the key manually | |
""" | |
def decorate(func): | |
@simple_cache.wraps(func) | |
def wrapper(**kwargs): | |
# key = (args, tuple_kwargs(kwargs)) | |
filename = f"data/.simple.cache" | |
tokenizer = kwargs["tokenizer"] | |
# We must use immutaable, hashable args as keys, so no lists, sets, or tokenizer | |
key = simple_cache.tuple_kwargs( | |
dict( | |
data_dir=kwargs["data_dir"], | |
resnet_name=type(resnet).__name__, | |
tokenizer_name=type(tokenizer).__name__, | |
vocab_size=len(tokenizer.encoder), | |
special_tokens=tuple(sorted(tokenizer.special_tokens)), | |
) | |
) | |
value = simple_cache.load_key(filename, key) | |
if value is None: | |
value = func(**kwargs) | |
simple_cache.save_key(filename, key, value, ttl) | |
else: | |
logger.info(f'Loaded utturances from cache for {kwargs["personality"]}') | |
return value | |
return wrapper | |
return decorate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment