#35524: Add possibility to renew {% cache %} templatetag
-------------------------------------+-------------------------------------
Reporter: Petr | Owner: nobody
Dlouhý |
Type: New | Status: new
feature |
Component: Template | Version: 5.0
system |
Severity: Normal | Keywords: cache templatetag
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
The wider context of my use-case is described at:
https://code.djangoproject.com/ticket/5815#comment:17
I have a view which uses templates with `{% cache %}` templatetag.
When the cache timeouts it results results into database hammering -
multiple requests lacking the cache are hitting DB at the same time.
Erasing the cache with `make_template_fragment_key` is not an option
either, it results in the same situation.
I would like to be able to renew the state of cached parts of my template
without erasing the cache key, but rather just setting the new values.
There are two scenarios in which I could use this functionality to solve
my problem:
1) I would create a task/job that will periodically renew the cache on
that page.
2) The cache would use staggering - I randomize the cache timeout to
reduce number of requests without cache. This unfortunately doesn't work,
because the cache key will expire at one time.
I am trying to use the 1) solution - the very easy solution is to modify
the cache templatetag code with something like:
{{{
class CacheNode(Node):
def __init__(self, nodelist, expire_time_var, fragment_name, vary_on,
cache_name, renew_cache=False):
self.renew_cache = renew_cache
...
def render(self, context):
...
value = fragment_cache.get(cache_key)
if value is None or self.renew_cache
value = self.nodelist.render(context)
fragment_cache.set(cache_key, value, expire_time)
return value
...
}}}
Right now I have this custom templatetag, but I am not very happy that I
have to copy and maintain a big portion of code from Django.
I think Django should provide some mechanisms to deal with situations like
this (and ticket #5815 is supporting this claim).
--
Ticket URL: <
https://code.djangoproject.com/ticket/35524>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.