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 = 'function_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.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | dc4ae0888723e0b98048d2faaa641c2be3105386608aaabdecbd91c3b028abc0 |
|
MD5 | e02a094ba91114644c7f9bb2d0469864 |
|
BLAKE2b-256 | f6c66ed4ba5952f57ba03a40b1970f2d2fb61dd527985bfd82b29d460c41565b |
Hashes for django_update_cache-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9c3223f1db925ed09c7f0407dc7342f581a0d9d4966f9d59c9fda0e86149e8c |
|
MD5 | c99e091f35a50db4b622c87d76b37c6e |
|
BLAKE2b-256 | 624377644578c06a3132b6910d73bbe46acba703b521caf1d7b105480cdda15e |