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 MikiSoft)

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)

comment:1 by Tim Graham, 8 years ago

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?

in reply to:  1 comment:2 by MikiSoft, 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?

Last edited 8 years ago by MikiSoft (previous) (diff)

comment:3 by Tim Graham, 8 years ago

Easy pickings: set
Has patch: set
Needs tests: set
Triage Stage: UnreviewedAccepted

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):  
    2020            expire_time = self.expire_time_var.resolve(context)
    2121        except VariableDoesNotExist:
    2222            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)
    2728        if self.cache_name:
    2829            try:
    2930                cache_name = self.cache_name.resolve(context)

A test and perhaps a documentation update is also required.

comment:4 by MikiSoft, 8 years ago

Description: modified (diff)

in reply to:  3 comment:5 by MikiSoft, 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 Bo Marchman, 8 years ago

Owner: changed from nobody to Bo Marchman
Status: newassigned

comment:7 by Bo Marchman, 8 years ago

Needs tests: unset

comment:8 by Bo Marchman, 8 years ago

PR is at https://github.com/django/django/pull/8120, with a test and doc changes.

comment:9 by Tim Graham, 8 years ago

Patch needs improvement: set

I left a few comments for improvement on the PR.

comment:10 by Bo Marchman, 8 years ago

Patch needs improvement: unset

comment:11 by Bo Marchman, 8 years ago

Requested changes made.

comment:12 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: assignedclosed

In 7a7b331c:

Fixed #27882 -- Allowed {% cache %} to cache indefinitely.

Note: See TracTickets for help on using tickets.
Back to Top