Calmcode - diskcache: usage

Memoize via diskcache

1 2 3 4 5

If you haven't already, you'll need to install it.

python -m pip install diskcache

Assuming it's installed: let's rewrite our function to use diskcache.

# Before

import requests as rq
from functools import lru_cache 

@lru_cache 
def fetch_starwars_person(i):
    return rq.get(f"https://swapi.dev/api/people/{i}/").json()

First, we'll install diskcache via pip.

%pip install diskcache

Next, we'll create a new cache object and use it to replace the lru_cache decorator.

from diskcache import Cache

cache = Cache("local")

@cache.memoize() 
def fetch_starwars_person(i):
    return rq.get(f"https://swapi.dev/api/people/{i}/").json()

It's very similar to what we had before! Again if you run this you'll notice that the first request takes a while but the second request comes back much quicker.

Compare

It's good to point out that using a diskcache is slower that using an in-memory approach. Before we saw it being returned in ~ 10µs while it takes about ~1 ms now. It's good to notice that this is a 100x slowdown, but that can be attributed to reading from disk as opposed to memory. The benefit is that we don't loose data when we process restarts, but it is a bit slower.

In plenty of cases 1ms will still be plenty fast, but it's good to remember that there can still be tradeoffs here.