Skip to main content

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

This version

0.1

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)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dj_cache-0.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

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

Hashes for dj_cache-0.1.tar.gz
Algorithm Hash digest
SHA256 ea2029f763ac900c215a8e745c62b76abcbdf29941d78939b4f51b6ac2cfbc89
MD5 0f3e7ad09d6b2532767c33f0f2d510ad
BLAKE2b-256 c2a9bdf48a94a6064414d591a6d8a7392b2ca3d7a46c3d8df0b3dd2a28b59bd8

See more details on using hashes here.

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

Hashes for dj_cache-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9b0562b477430af011e66927013d29d549b52bc3351e738fb0fb106d3b8076a6
MD5 5811cddbaacd2e40efb2e730faf8d57b
BLAKE2b-256 d6b751ddd8e4533b0caddad3337aa872915fbdc0e1d8598dd2a61293ee08cc23

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page