DEV Community

Cover image for How I setup a sqlite cache in python
Waylon Walker
Waylon Walker

Posted on • Originally published at waylonwalker.com

How I setup a sqlite cache in python

When I need to cache some data between runs or share a cache accross multiple processes my go to library in python is diskcache. It's built on sqlite with just enough cacheing niceties that make it very worth it.

install diskcache

Install diskcache into your virtual environement of choice using pip from your command line.

python -m pip install diskcache
Enter fullscreen mode Exit fullscreen mode

setup the cache

There are a couple of different types of cache, Cache, FanoutCache, and DjangoCache, you can read more about those in the docs

from diskcache import Cache cache = FanoutCache('.mycache', statistics=True)
Enter fullscreen mode Exit fullscreen mode

Adding to the cache

Adding to the cache only needs a key and value.

cache.add('me', 'waylonwalker' )
Enter fullscreen mode Exit fullscreen mode

Set the expire time

Optionally you can set the seconds before it expires. The cache invalidation tools like this is what really makes diskcache shine over using raw sqlite or any sort of static file.

cache.add('me', 'waylonwalker', expire=60)
Enter fullscreen mode Exit fullscreen mode

tagging

Diskcache supports tagging entries added to the cache.

# add an item to the cache with a tag
cache.add('me', 'waylonwalker', expire=60, tag='people')
Enter fullscreen mode Exit fullscreen mode

This seems to let you do a few new things like getting items from the cache by both key and tag, or evict all tags from the cache.

# evict all items tagged as 'people' from the cache
cache.evict(tag='people')
Enter fullscreen mode Exit fullscreen mode

Reading from the cache

You can read from the cache by using the .get method and giving it the key you want to retrieve.

who = cache.get('me')
# who == 'waylonwalker'
Enter fullscreen mode Exit fullscreen mode

Cache Misses

Cache misses will return a None just like any dictionary .get miss.

missed = cache.get('missing')
# missed == None
Enter fullscreen mode Exit fullscreen mode

Give Grant some love and give grantjenks/python-diskcache a
⭐.

Top comments (0)