Skip to main content

Django DBCache Fields

Project description

Version:
0.9.2
Docs:

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

Download:

https://pypi-hypernode.com/pypi/django_dbcache_fields

Source:

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

Keywords:

django, database, cache, methods, decorator

Build status coverage Lintly 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.

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_dbcache_fields-0.9.2.tar.gz (709.5 kB view details)

Uploaded Source

Built Distribution

django_dbcache_fields-0.9.2-py2.py3-none-any.whl (11.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django_dbcache_fields-0.9.2.tar.gz.

File metadata

File hashes

Hashes for django_dbcache_fields-0.9.2.tar.gz
Algorithm Hash digest
SHA256 f92c2b1e9125e498c62ddf53685cb55a28582317252425c78cb460ae00dc12b5
MD5 cdb2e8f575fa33918a1b2e2d79f1bc42
BLAKE2b-256 fb65dbb4bbdc21c4bc28716066ab50b8e7859943e66f19f38a3d9ffc7a5c20e2

See more details on using hashes here.

File details

Details for the file django_dbcache_fields-0.9.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_dbcache_fields-0.9.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 8d09a6d75e0c8607d3344ae97979a76a2ffad2bfd1c3384f448f9fe0f7c8bb32
MD5 190a3c5696e97661492379217245ea39
BLAKE2b-256 8fa26647a3f0b8af14d58802224d605bd672c89066030b02a138871c23385556

See more details on using hashes here.

Supported by

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