﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35524	Add possibility to renew {% cache %} templatetag	Petr Dlouhý	nobody	"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)."	New feature	closed	Template system	5.0	Normal	wontfix	cache templatetag		Unreviewed	0	0	0	0	0	0
