An Interest In:
Web News this Week
- March 20, 2024
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
- March 15, 2024
- March 14, 2024
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
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)
Adding to the cache
Adding to the cache only needs a key and value.
cache.add('me', 'waylonwalker' )
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)
tagging
Diskcache supports tagging entries added to the cache.
# add an item to the cache with a tagcache.add('me', 'waylonwalker', expire=60, tag='people')
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 cachecache.evict(tag='people')
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'
Cache Misses
Cache misses will return a None
just like any dictionary .get
miss.
missed = cache.get('missing')# missed == None
Give Grant some love and give grantjenks/python-diskcache a
.
Original Link: https://dev.to/waylonwalker/how-i-setup-a-sqlite-cache-in-python-17ni
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To