Skip to main content

Django Cache Expression Language (DCEL)

Introduction

DCEL is an extension of Django Framework for caching. It provides a more flexible decorator that use an expression language to define the cache key.

Getting Started

Cache Method

Use @Cached decorator to declare a cached method. This will check the cache before executing the actual method. If the defined key is already cached, it'll immediately return the cached return value. Else, it will execute the actual method, and update the cache using the defined key and return value.

class StudentService:
    def __init__(self):
        self.students = []

    @Cached(key='{id}', duration=timedelta(minutes=5))
    def read(self, id: UUID) -> Optional[dict]:
        return first(
            filter(
                lambda s: s['id'] == id, self.students
            ),
            None
        )

Use @CacheUpdate decorator to declare a cache update method. This will update the cache using the defined key and return value after executing the actual method.

class StudentService:
    def __init__(self):
        self.students = []

    @CacheUpdate(key='{id}', duration=timedelta(minutes=5))
    def update(self, id: UUID, name: str) -> dict:
        student = self.read(id)
        student['name'] = name
        return student

Use @CacheInvalidate decorator to declare a cache invalidate method. This will invalidate cache using the defined key after method executing the actual method.

class StudentService:
    def __init__(self):
        self.students = []

    @CacheInvalidate(key='{id}')
    def delete(self, id: UUID) -> dict:
        student = self.read(id)
        self.students.remove(student)
        return student

Cache Key

Cache key is derived from method argument. The syntax is similar to python f-string, with some limitation. You can define key using primitive, object, dictionary, list, tuple data type variable. Variable should be enclosed in curly braces ({}), and string index should be enclosed in quotation mark ("" or '').

class UtilService:

    @staticmethod
    @Cached(key='sum: {a} {b}')
    def sum(a: int, b: int) -> int:
        return a + b

    @staticmethod
    @Cached(key='join name: {a["name"]} {b["name"]}')
    def join_name(a: dict, b: dict) -> str:
        return ''.join((a['name'], b['name']))

    @staticmethod
    @Cached(key='is lowest: {values[0]} {values[-1]}')
    def is_lowest(values: Union[list, tuple]) -> bool:
        return values[0] <= values[-1]

    @staticmethod
    @Cached(key='duration: {started_at.day} {finished_at.day}')
    def duration(started_at: datetime, finished_at: datetime) -> timedelta:
        return finished_at - started_at

Nested variable, for example join_name: {a[b["name"]]} is not supported.

Release files for django-cache-expression-language 0.0.4

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

Source distribution (sdist)

Source distribution for django-cache-expression-language 0.0.4
File Size Uploaded
django-cache-expression-language-0.0.4.tar.gz 3.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-cache-expression-language 0.0.4
File Interpreter ABI Platform
django_cache_expression_language-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 9.8 kB

Release files / django-cache-expression-language-0.0.4.tar.gz

Download URL django-cache-expression-language-0.0.4.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
83b885102c7735fba453cb4ad3ab17027073f0326886a3f6ced3582a1eded725
BLAKE2b-256 checksum
How to use checksums
9946f6f8aedd6d6ac87152a929b4c5f04b5b5ce6b9f43d99387f4dea57e9310b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.9

Release files / django_cache_expression_language-0.0.4-py3-none-any.whl

Download URL django_cache_expression_language-0.0.4-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4c38ca260fd6512ef9995cedc01c262aa2b0806fdd014c627f330c6db9869a46
BLAKE2b-256 checksum
How to use checksums
cb4f88956076bdd762f4941f01248dcfc83549784c470d21311813ed6f34ea16
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.9

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.2

2 release files

0.0.1

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