Skip to main content

Django DBCache Fields

Version:

0.9.3

Docs:

https://django-dbcache-fields.readthedocs.io/

Download:

https://pypi.python.org/pypi/django_dbcache_fields

Source:

https://github.com/maykinmedia/django-dbcache-fields

Keywords:

django, database, cache, methods, decorator

Build status coverage BSD License Supported Python versions Supported Django versions

About

This library provides a decorator dbcache that caches the result of your Django Model methods in your database.

It adds a regular Field on your Model for each method that you decorate. This means you can use all ORM-functions like aggregation and migrations. You can use existing fields or let dbcache create the field for you.

You can also invalidate the cached value by creating a _dirty_ function or by indicating which other models affect the this cached value. By default, the cached value is only updated when the model is saved.

Installation

You can install django_dbcache_fields either via the Python Package Index (PyPI) or from source.

To install using pip:

$ pip install -U django_dbcache_fields

Usage

To use this with your project you need to follow these steps:

  1. Install the django_dbcache_fields library:

    $ pip install django_dbcache_fields
  2. Add django_dbcache_fields to INSTALLED_APPS in your Django project’s settings.py:

    INSTALLED_APPS = (
        # ...,
        'django_dbcache_fields',
    )

    Note that there is no dash in the module name, only underscores.

  3. All done. You can now decorate methods in your Model with @dbcache.

Example

Simple example to show what dbcache does:

from django.db import models
from django_dbcache_fields.decorators import dbcache

class Ingredient(models.Model):
    name = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=4, decimal_places=2)

class Pizza(models.Model):
    name = models.CharField(max_length=100)
    ingredients = models.ManyToManyField(Ingredient)

    @dbcache(models.DecimalField(max_digits=6, decimal_places=2,
             blank=True, null=True), invalidated_by=['myapp.Ingredient'])
    def get_price(self):
        return self.ingredients.aggregate(total=Sum('price'))['total'] or Decimal()

Every call to get_price would normally perform a database query to calculate the total price of all ingredients. However, the dbcache decorator caused a new field to be added to the model: A DecimalField that can store the resulting value of the get_price function, so it doesn’t need to perform the same query over and over again.

Metadata

Release files for django-dbcache-fields 0.9.3

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-dbcache-fields 0.9.3
File Size Uploaded Provenance Actions
django_dbcache_fields-0.9.3.tar.gz 711.6 kB Without provenance Details for django_dbcache_fields-0.9.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-dbcache-fields 0.9.3
File Interpreter ABI Platform Actions
django_dbcache_fields-0.9.3-py2.py3-none-any.whl Python 2, Python 3 none any Details for django_dbcache_fields-0.9.3-py2.py3-none-any.whl

Total release size: 721.6 kB

Release files / django_dbcache_fields-0.9.3.tar.gz

Download URL django_dbcache_fields-0.9.3.tar.gz
Size 711.6 kB
Tags Source
SHA-256 checksum
How to use checksums
1d3f8246e4730975a26b86186ea4bbcf5a9a24b49719909f71a873b0a5a0983e
BLAKE2b-256 checksum
How to use checksums
b27dfa010754960ca7a1ab317ad0e34847f95ed96b1bbf8a9b4a96e3a1d36f58
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.41.1 CPython/3.6.6

Release files / django_dbcache_fields-0.9.3-py2.py3-none-any.whl

Download URL django_dbcache_fields-0.9.3-py2.py3-none-any.whl
Size 10.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
6c5efbdf5e2c11c36a6dfb5d68530d4ce0cf412e1dce4c0659fe9222b065ad76
BLAKE2b-256 checksum
How to use checksums
56144dbde0b1c7184666b29fb6df590f753790b72ca82ac8244c256f24256ff1
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.41.1 CPython/3.6.6

Security for django-dbcache-fields 0.9.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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