Invalidation of several caches efficiently in Django

1

In my project I have the following template:

class Item(models.Model):
    month = models.DateField('month', null=False, blank=False, db_index=True)
    kg = models.BigIntegerField('kg')
    tags = models.ManyToManyField('Tag', related_name='items')
    // vários outros campos utilizados para filtrar

And I have report_view that returns the sum of kg per month and per tag according to os filtros given in query URL .

This view returns something like this:

--------------------------------
|Tag    |jan    |fev    |mar   |
--------------------------------
|Tag 1  |1000   |1500   |2000  |
--------------------------------
|Tag 2  |1235   |4652   |0     |
--------------------------------

Since my table Item already has more than 4 million records and is always growing, my report_view always stays in cache .

So far so good.

The problem is : Site users can change tags of Items and whenever this occurs it is necessary to invalidate caches , but I would like to do this in a more granular way.

For example, if a user changes the tag to a Item of janeiro this should only invalidate the caches of janeiro (I prefer that the caches are per month than tag because sometimes a tag interferes with the other). But I do not know which are all views that were cache because there are thousands of possibilities of different filters that change URL .

What I've been able to do so far:

  • Create a signal that invalidates all caches whenever a tag changes
@receiver(m2m_changed, sender=Item.tags.through)
def tags_changed(sender, **kwargs):
    cache.clear()

But that cleans everything which is not good ..

There is a way to mark the caches so that something like this is done:

cache.filter('jan').clear()
    
asked by anonymous 27.07.2017 / 02:11

0 answers