Cacheback - asynchronous cache
The fastest code is, of course, code that never runs. — Jacob Kaplan-Moss
django-cacheback
For a long time, I want to write something about cache until I find django-cacheback. The main difference between cacheback and other design is asynchronously
. Other synchronous design of cache already talked a lot on the internet. Why I choose cacheback? because it can fix the cash crash
issue without sharding.
It’s a smart caching and the key idea being that it’s better to serve a stale item (and populate the cache asynchronously) than block the response process in order to populate the cache synchronously.
demo
import requests
from cacheback.decorators import cacheback
@cacheback(fetch_on_miss=False)
def fetch_tweets(username):
url = "https://twitter.com/statuses/user_timeline.json?screen_name=%s"
return requests.get(url % username).json
- Cache items for 10 minutes.
- For a cache miss, None will be returned and the cache refreshed asynchronously.
Read article Cacheback - asynchronous cache refreshing for Django first, you will find the difference between them. BTW, I find this lib as the author write a famous e-commerce framework django-oscar
what are we caring about when designing cache?
- Cache name: feature related
- Key structure: generated automatically with unique name, meaningful and easy to do batch operation
- Value structure: performance related (json, pickle, etc)
- When this cache has been generated?
- When this cache will be flushed?
- When this cache will be fetched?
- What if cache crash?
A usable design
https://github.com/iamsk/cacher
Refs:
Django doesn’t scale! (And what you can do about it.) django-cacheback docs https://www.fullstackpython.com/caching.html