<aside> 💡 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.

</aside>

memoize to disk - python - persistent memoization

joblib.Memory - joblib 1.2.0.dev0 documentation

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)

<aside> ☝ The memory variable will me used as a decorator, and in principle, you can have several different caches for different functions if you need.

</aside>

Step 2

@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