[Answered ]-Automatically cache view after cache expires in django

2👍

Nothing out of the box afaik. Your best bet would be probably running a background task (django manage command from crontab, or celery) every 5 min and manually caching that value under some key (with the expiration set to never expire), then reading it in the view by the key (no more whole page caching). I think this is the only way to keep 100% of requests cached and not return any stale data (older than 5 min).

If you don’t mind showing the stale data to the first user after the 5 min have passed, then you can store a timestamp along with the value inside the cache to mark when this cache was last refreshed. This timestamp then can be used to manually check if 5 min have passed since the last refresh (this is to battle memcached standard behavior of returning nothing for expired values). If 5 min have passed, return the stale cached value to a user immediately and spawn a background thread to update the cached value.

👤serg

Leave a comment