Skip to main content
Help us improve Python packaging – donate today!

Keep generated fields in cache for your Django models.

Project Description

https://travis-ci.org/ActivKonnect/paper.svg?branch=develop

Keep generated fields in cache for your Django models.

Usage

You have two options: @paper will help you to create a calculated field that is stored in Django’s cache, and @cardboard will create a regular field in your model and update it in database automatically.

@paper

Just like the @property decorator, you just need to use the @paper decorator, that will transform a method of your class into a cached property.

class ModelA(models.Model):
    name = models.TextField()

    @paper(1, [
        ('testpaper.ModelB', lambda i: ModelA.objects.filter(children=i)),
    ])
    def count(self):
        return ModelA.objects\
            .filter(pk=self.pk)\
            .annotate(count=Coalesce(Sum('children__count'), 0))\
            .values_list('count', flat=True)[0]


class ModelB(models.Model):
    parent = models.ForeignKey('ModelA', related_name='children')
    count = models.IntegerField()

@paper takes 2 arguments:

  • The method’s version number, in case you want to change the format of what is returned. By example, imagine you’re returning a complex denormalized data structure. If at some point you want to add some data in it, you can simply bump the version number and it will invalidate the caches automatically.
  • A list of model/lister couples. - The model is either a model class, either an ‘app.Model’ string - The lister takes a single argument which is an instance of the updated model (here a ModelB objet) and returns a list of affected objects to invalidate (here a queryset of ModelA objects).

The value will be cached using Django’s default cache. Please note that this requires the cache to be shared across all your web worker instances, otherwise invalidation won’t have any the intended effect.

Invalidation is based on Django signals, so it requires save() or delete() to be called in order to work correctly. Bulk/SQL operations won’t be detected automatically.

@cardboard

Use is very similar to @paper, the main differences being the absence of versioning and the fact that result is going to be stored in the model itself instead of caching it.

class ModelA(models.Model):
    name = models.TextField()

    @cardboard(models.IntegerField(default=0), [
        ('testpaper.ModelB', lambda i: ModelA.objects.filter(children=i)),
    ])
    def count(self):
        return ModelA.objects\
            .filter(pk=self.pk)\
            .annotate(count=Coalesce(Sum('children__count'), 0))\
            .values_list('count', flat=True)[0]


class ModelB(models.Model):
    parent = models.ForeignKey('ModelA', related_name='children')
    count = models.IntegerField()

@cardboard takes two arguments:

  • The field that will be used to store results. You have to give it a sensible default value, because no calculation will be made upon object creation. Calculation are only triggered by the dependencies functions.
  • A list of dependencies, just like in @paper.

The same remark goes for this technique being save()/delete() driven: bulk or SQL operations won’t trigger recalculation.

Licence

This software is licenced by ActivKonnect under the terms of the WTFPL.

Release history Release notifications

This version
History Node

0.2.0

History Node

0.1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
paper-0.2.0.tar.gz (3.9 kB) Copy SHA256 hash SHA256 Source None Sep 13, 2015

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page