Memoizing SQL queries Memoizing SQL queries pandas pandas

Memoizing SQL queries


Yes, you can do this with joblib (this example basically pastes itself):

>>> from tempfile import mkdtemp>>> cachedir = mkdtemp()>>> from joblib import Memory>>> memory = Memory(cachedir=cachedir, verbose=0)>>> @memory.cache... def run_my_query(my_query)...     ......     return df

You can clear the cache using memory.clear().


Note you could also use lru_cache or even "manually" with a simple dict:

def run_my_query(my_query, cache={})    if my_query in cache:        return cache[my_query]    ...    cache[my_query] = df    return df

You could clear the cache with run_my_query.func_defaults[0].clear() (not sure I'd recommend this though, just thought it was a fun example).