Skip to main content

django-cached-fields

Cached fields for Django

Usage

Defining a basic Cached Field

The first argument of the CachedField constructor can be the name of a method on the class.

This method only works with field triggers as signal triggers will require a CachedFieldSignalHandler.

class Invoice(models.Model):
    name = models.TextField(null=False)
    quantity = models.IntegerField()
    amount = models.IntegerField(null=False)

    def calc_total(self):
        self.total = self.quantity * self.amount

    total = CachedIntegerField(
        'calc_total', 
        field_triggers=[
            'amount',
        ],
        timeout=timedelta(seconds=5),
    )

If you would like your cached field's callback to be triggered by a signal, you will need to create a signal handler.

class InvoiceSignalHandler(CachedFieldSignalHandler):

    def calc_total(instance):
        instance.total = instance.quantity * instance.amount

    @for_class('Customer')
    def cutomer_total(customer):
        return invoice.quantity * invoice.amount

    @for_class('Supplier')
    def supplier_total(supplier):
        invoice = supplier.invoice.last()
        return invoice.quantity * invoice.amount

If you would like to offload task processing to Celery, django-cached-fields can handle that for you automatically.

settings.py

CACHED_FIELDS_CELERY_ENABLED = True
CACHED_FIELDS_CELERY = {
    "queue": "default"
}

If you'd like to offload a recalculation to a specific queue, you can do this.

class InvoiceSignalHandler(CachedFieldSignalHandler):

    def calc_total(instance):
        instance.total = instance.quantity * instance.amount

    @for_class('Customer', queue="low_priority")
    def cutomer_total(customer):
        invoices = customer.invoices.all()
        for i in invoices:
            i.cache_toolkit.update_cache('total', i.quantity * i.amount)
            i.save()

    @for_class('Supplier')
    def supplier_total(supplier):
        invoice = supplier.invoice.last()
        invoice.total = invoice.quantity * invoice.amount

Force Recalculation

ym = YourModel.objects.create(name="fart", amount=6)

ym.cache_toolchain.refresh_field('total')

Get last time the cache was updated


For when you need to run queries ```python In [2]: print(ym.total_last_update) Out[2]: datetime.datetime(2019,10,30,4,30,54)

In [3]: YourModel.objcts.filter(total_last_update__date__gte=date(2019,10,1)).exists() Out[3]: True


Release files for django-cached-fields 0.1.2

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-cached-fields 0.1.2
File Size Uploaded
django-cached-fields-0.1.2.tar.gz 6.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-cached-fields 0.1.2
File Interpreter ABI Platform
django_cached_fields-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 13.9 kB

Release files / django-cached-fields-0.1.2.tar.gz

Download URL django-cached-fields-0.1.2.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
d9a582db63377e5a05d4669631218e1401221255175789c8836d1c07992330b8
BLAKE2b-256 checksum
How to use checksums
313c5667e91ee421f725d89e8d8b668783c1081e820a93bdbf11d9311ce1c4f5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.6.8

Release files / django_cached_fields-0.1.2-py3-none-any.whl

Download URL django_cached_fields-0.1.2-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e370f8e573d32e27f535ec8d996c0845f75914fd6f2f3bc518f4589037add528
BLAKE2b-256 checksum
How to use checksums
bb6a7d2053192d08d4e34aaeb0ad4c9f5d6590cd1b333c805195cec28034ca21
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.6.8
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