A Django app that offers a db-backed eventually consistent cache.
Project description
dj-cache
A database-backed eventually consistent Last-Write-Wins cache for Python functions inside a Django application.
Installation
pip install \
git+https://github.com/aalekhpatel07/dj-cache.git#subdirectory=dj-cache
Usage
Example: As a plain decorator on arbitrary functions.
from dj_cache.decorators import cached
@cached(ttl_seconds=10)
def my_expensive_function():
...
# The expensive function is only called and waited on once.
# Subsequent calls will be cached and in case the cached value
# is expired/stale, an update will be triggered in the background
# without blocking the function call, which will return the last stored value.
# This ensures a Last-Write-Wins cache that is eventually consistent.
# Note: Concurrent calls to update the cache while an update is underway are
# ignored.
Example: Caching an expensive function used inside a Django view.
# urls.py
import time
from django.urls import path
from django.http import HttpResponse
from dj_cache.decorators import cached
@cached(ttl_seconds=60)
def expensive_function():
time.sleep(5)
return 42
def home(request):
value = expensive_function()
return HttpResponse(f"Expensive function returned: {value}")
urlpatterns = [
path("/", home),
]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
dj_cache-0.1.tar.gz
(4.2 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dj_cache-0.1.tar.gz.
File metadata
- Download URL: dj_cache-0.1.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ea2029f763ac900c215a8e745c62b76abcbdf29941d78939b4f51b6ac2cfbc89
|
|
| MD5 |
0f3e7ad09d6b2532767c33f0f2d510ad
|
|
| BLAKE2b-256 |
c2a9bdf48a94a6064414d591a6d8a7392b2ca3d7a46c3d8df0b3dd2a28b59bd8
|
File details
Details for the file dj_cache-0.1-py3-none-any.whl.
File metadata
- Download URL: dj_cache-0.1-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9b0562b477430af011e66927013d29d549b52bc3351e738fb0fb106d3b8076a6
|
|
| MD5 |
5811cddbaacd2e40efb2e730faf8d57b
|
|
| BLAKE2b-256 |
d6b751ddd8e4533b0caddad3337aa872915fbdc0e1d8598dd2a61293ee08cc23
|