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
File details
Details for the file django-update-cache-1.0.4.tar.gz
.
File metadata
- Download URL: django-update-cache-1.0.4.tar.gz
- Upload date:
- Size: 116.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e3fc4f3aa2dd4190ddd6d49c3741334e56225723a0313a8a9d386ba36ec647e6 |
|
MD5 | 4e1867355d8cba8d275a60d93b13496b |
|
BLAKE2b-256 | b5c428addf1aeea7866d0eb0be5f953798c8d35d07948230a8348a2d7fe80ed5 |
File details
Details for the file django_update_cache-1.0.4-py3-none-any.whl
.
File metadata
- Download URL: django_update_cache-1.0.4-py3-none-any.whl
- Upload date:
- Size: 11.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5614e29d10cf7c61206be33dde6779d3861bb89be7e73e0087705df3342e31a3 |
|
MD5 | 68df3ee6ac804cb0fa6bc1c298097ae1 |
|
BLAKE2b-256 | 5cfbfdffe34a6704718784e92a93e6b70bb46366507c377cb3e29bdfd88d5ed6 |