Lazy cache updates for Django
Project description
django-update-cache
Lazy cache updates for Django.
Rationale
Cache function results with optional asynchronous updates.
Support
Supports: Python 3.10.
Supports Django Versions: 4.2.7
Installation
$ pip install django-update-cache
Usage
Add update_cache.apps.UpdateCacheConfig
to INSTALLED_APPS
.
Run migrations:
python manage.py migrate
Add a module cached_functions
to one or more of your apps. Decorate any function for which you want the results to be
cached:
# cached_functions.py
from update_cache.decorators import cache_function
@cache_function()
def my_expensive_function():
...
Specify a specific timeout:
# cached_functions.py
from update_cache.decorators import cache_function
@cache_function(timeout=60)
def my_expensive_function():
...
Use asynchronous updating (this requires running a django_rq worker):
# cached_functions.py
from update_cache.brokers import async_broker
from update_cache.decorators import cache_function
@cache_function(broker=async_broker)
def my_expensive_function():
...
Use a custom cache backend:
# cached_functions.py
from update_cache.decorators import cache_function
@cache_function(backend='my_cache_alias')
def my_expensive_function():
...
You can set a global broker to delegate the cache updates in settings.py
.
DUC_DEFAULT_BROKER = 'update_cache.brokers.AsyncBroker'
Invalidate the cache:
# cached_functions.py
from update_cache.decorators import cache_function
@cache_function()
def my_expensive_function():
...
my_expensive_function.cache.invalidate()
Cache a view (only synchronous updates).
# cached_functions.py
from update_cache.decorators import cache_view
@cache_view()
def my_expensive_view():
...
View all cached entries in Django Admin:
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-update-cache-1.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b5bb4c0c75ea784c9a5025a67d5a02eaad1cba9817bb80ea32464510949be231 |
|
MD5 | e08eb86aa022d0d4dbd9b12b3435240f |
|
BLAKE2b-256 | 05c60ce7376ad60c26ac97f020855c6dc691c88aa6fd67e04d1fb4c9ce4e2e25 |
Hashes for django_update_cache-1.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 82ea3b90422a724c4e13f0a8cf52d69b07afbf131e0825528a750d8fc07870c7 |
|
MD5 | e79307db9b73e04eb80ff4b2e1b4ada3 |
|
BLAKE2b-256 | 25de5c0a66719e85defb531d0d01d2387bec82af034c90c9c8b158300189ded3 |