Opened 8 years ago
Closed 8 years ago
#27882 closed Cleanup/optimization (fixed)
Allow template fragment caching for unlimited time
Reported by: | MikiSoft | Owned by: | Bo Marchman |
---|---|---|---|
Component: | Template system | Version: | 1.10 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description (last modified by )
Apparently, I've discovered that {% cache None name %}
doesn't work - it should cache the content wrapped with it forever i.e. until server restarts, but when I specify it like that then it's giving an error for invalid input, and I don't see any other solution to this. So can someone make it to accept None
as a parameter for unlimited time, like it is in low-level API?
Change History (12)
follow-up: 2 comment:1 by , 8 years ago
comment:2 by , 8 years ago
Replying to Tim Graham:
Yes, I'm sure that it's not a problem in my project because I've tested my project on this way:
First, I put in a view print(cache.get(make_template_fragment_key('test')))
and in corresponding template {% cache 0 test %}
, then I started the server and I opened to that page. It printed me None
, even after refreshing the page.
So, I changed the tag to be {% cache 300 test %}
and I've opened that page again. And suddenly it started printing content which I put under that cache tag!
Maybe the problem is with make_template_fragment_key
method or it has to do with the cache framework itself?
follow-up: 5 comment:3 by , 8 years ago
Easy pickings: | set |
---|---|
Has patch: | set |
Needs tests: | set |
Triage Stage: | Unreviewed → Accepted |
The documentation says, "Passing in None
for timeout
will cache the value forever. A timeout of 0 won’t cache the value." So {% cache %}
needs to allow None
. It looks like this patch does the trick:
-
django/templatetags/cache.py
diff --git a/django/templatetags/cache.py b/django/templatetags/cache.py index 3af6dc4..9e402a1 100644
a b class CacheNode(Node): 20 20 expire_time = self.expire_time_var.resolve(context) 21 21 except VariableDoesNotExist: 22 22 raise TemplateSyntaxError('"cache" tag got an unknown variable: %r' % self.expire_time_var.var) 23 try: 24 expire_time = int(expire_time) 25 except (ValueError, TypeError): 26 raise TemplateSyntaxError('"cache" tag got a non-integer timeout value: %r' % expire_time) 23 if expire_time is not None: 24 try: 25 expire_time = int(expire_time) 26 except (ValueError, TypeError): 27 raise TemplateSyntaxError('"cache" tag got a non-integer timeout value: %r' % expire_time) 27 28 if self.cache_name: 28 29 try: 29 30 cache_name = self.cache_name.resolve(context)
A test and perhaps a documentation update is also required.
comment:4 by , 8 years ago
Description: | modified (diff) |
---|
comment:5 by , 8 years ago
Replying to Tim Graham:
Yes you're right about that, it should be None
instead of zero (I've corrected the main post). Thanks for solving it!
I have one more question if you can answer me here (sorry for being a bit off topic) - when can I expect a new Django release with such patch?
comment:6 by , 8 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:7 by , 8 years ago
Needs tests: | unset |
---|
comment:8 by , 8 years ago
PR is at https://github.com/django/django/pull/8120, with a test and doc changes.
comment:9 by , 8 years ago
Patch needs improvement: | set |
---|
I left a few comments for improvement on the PR.
comment:10 by , 8 years ago
Patch needs improvement: | unset |
---|
I didn't test it but it looks like it should work -- the template tag is calling cache.set(key, value, timeout). Can you check to be sure that the problem isn't in your project?