1
In my project I own the following model:
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 the report_view
which returns the sum of kg
by Month and by tag in accordance with os filtros
provided in the query of URL
.
That one view
returns something like this:
--------------------------------
|Tag |jan |fev |mar |
--------------------------------
|Tag 1 |1000 |1500 |2000 |
--------------------------------
|Tag 2 |1235 |4652 |0 |
--------------------------------
Like my table Item
already has more than 4 million records and is always growing, my report_view
always stays in the cache.
So far so good.
The problem is: users of the site can change the tags
of Items
and whenever this occurs it is necessary to invalidate the caches, but I’d like to do it in a more granulated.
For example, if a user changes the tag
in a Item
of janeiro
this should only invalidate the caches of janeiro
(I prefer that the caches are by month than by tag
for sometimes a tag
interferes with the other). But I don’t know what all the views
who went to cache
because there are thousands of possibilities of different filters that change the URL
.
What I’ve managed to do so far:
- Create a
signal
which invalidates all caches whenever atag
mute
@receiver(m2m_changed, sender=Item.tags.through) def tags_changed(sender, **kwargs): cache.clear()
But it clears all which is not good..
There is a way to mark caches so something like this can be done:
cache.filter('jan').clear()