Skip to main content

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),
]

Metadata

Release files for dj-cache 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dj-cache 0.1
File Size Uploaded
dj_cache-0.1.tar.gz 4.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dj-cache 0.1
File Interpreter ABI Platform
dj_cache-0.1-py3-none-any.whl Python 3 none any Details

Total release size: 9.2 kB

Release files / dj_cache-0.1.tar.gz

Download URL dj_cache-0.1.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
ea2029f763ac900c215a8e745c62b76abcbdf29941d78939b4f51b6ac2cfbc89
BLAKE2b-256 checksum
How to use checksums
c2a9bdf48a94a6064414d591a6d8a7392b2ca3d7a46c3d8df0b3dd2a28b59bd8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.10

Release files / dj_cache-0.1-py3-none-any.whl

Download URL dj_cache-0.1-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9b0562b477430af011e66927013d29d549b52bc3351e738fb0fb106d3b8076a6
BLAKE2b-256 checksum
How to use checksums
d6b751ddd8e4533b0caddad3337aa872915fbdc0e1d8598dd2a61293ee08cc23
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.10

Release history Release notifications | RSS feed

This release

0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page