Skip to main content

Expression Language Cache Decorator for Django

Project description

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.

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

django-cache-expression-language-0.0.4.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file django-cache-expression-language-0.0.4.tar.gz.

File metadata

  • Download URL: django-cache-expression-language-0.0.4.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for django-cache-expression-language-0.0.4.tar.gz
Algorithm Hash digest
SHA256 83b885102c7735fba453cb4ad3ab17027073f0326886a3f6ced3582a1eded725
MD5 3de0ad852baef4c792140b0786afa3b2
BLAKE2b-256 9946f6f8aedd6d6ac87152a929b4c5f04b5b5ce6b9f43d99387f4dea57e9310b

See more details on using hashes here.

File details

Details for the file django_cache_expression_language-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: django_cache_expression_language-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for django_cache_expression_language-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 4c38ca260fd6512ef9995cedc01c262aa2b0806fdd014c627f330c6db9869a46
MD5 6610bb608721146c1232e8afa878d0cd
BLAKE2b-256 cb4f88956076bdd762f4941f01248dcfc83549784c470d21311813ed6f34ea16

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