🧮

Caching in IPython (notebooks)

💡
The goal is to learn how to memoize a function in IPython with no additional effort in a way that the cache is saved after we close and reopen the notebook.
 
 

Step 1

Assign a specific directory that will keep the cache for your functions.
 
from joblib import Memory

cachedir = "my-cache"
memory = Memory(cachedir, verbose = 0)
 
The memory variable will me used as a decorator, and in principle, you can have several different caches for different functions if you need.

Step 2

  • A classical way to check the caching is to have a print statement inside.
  • The function will print its output only on the first run.
  • Starting from the second run, its value will be taken from a dictionary.
 
@memory.cache
def f(x):
    print('Running f(%s)...' % x)
    return x
 
>>> first = f(1) # First run
Running f(1)...

>>> second = f(1) # Second run

>>> print(first)
1

>>> print(second)
1

Step 3

🤔
You can clean after yourself if necessary.
 
## We can clear the cache of a specific function
>>> f.clear()

## And now, the values need to be recomputed again
>>> f(1)
Running f(1)...
1

Step 4

When the function is changed during code development, the cache is cleared.
@memory.cache
def f(x):
    print('The function has changed f(%s)' % x)
    return x

>>> f(1)
The function has changed f(1)
1

Step 5

Finally, you can clear the entire cache associated with variable memory
>>> memory.clear()