[Django] #27882: Allow template fragment caching for unlimited time

34 views
Skip to first unread message

Django

unread,
Feb 25, 2017, 10:05:38 PM2/25/17
to django-...@googlegroups.com
#27882: Allow template fragment caching for unlimited time
------------------------------------------------+------------------------
Reporter: MikiSoft | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Template system | Version: 1.10
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------------+------------------------
Apparently, I've discovered that `{% cache 0 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 not caching it at all,
and I don't see any other solution to this. So can someone make it to
accept zero as a parameter for unlimited time, like it is in low-level
API?

--
Ticket URL: <https://code.djangoproject.com/ticket/27882>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 27, 2017, 7:04:02 AM2/27/17
to django-...@googlegroups.com
#27882: Allow template fragment caching for unlimited time
-------------------------------------+-------------------------------------
Reporter: MikiSoft | Owner: nobody
Type: | Status: new
Cleanup/optimization |

Component: Template system | Version: 1.10
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham):

I didn't test it but it looks like it should work -- the template tag is
calling
[https://github.com/django/django/blob/fba4f831bc75369c975a95c9b7774e9e89f8a2f9/django/templatetags/cache.py#L47
cache.set(key, value, timeout)]. Can you check to be sure that the problem
isn't in your project?

--
Ticket URL: <https://code.djangoproject.com/ticket/27882#comment:1>

Django

unread,
Feb 27, 2017, 7:40:09 AM2/27/17
to django-...@googlegroups.com
#27882: Allow template fragment caching for unlimited time
-------------------------------------+-------------------------------------
Reporter: MikiSoft | Owner: nobody
Type: | Status: new
Cleanup/optimization |

Component: Template system | Version: 1.10
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by MikiSoft):

Replying to [comment:1 Tim Graham]:
Yes, I'm sure that it's not a problem in my project because I've tested my
project on this this way:
Fist, 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 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!

--
Ticket URL: <https://code.djangoproject.com/ticket/27882#comment:2>

Django

unread,
Feb 27, 2017, 8:37:07 AM2/27/17
to django-...@googlegroups.com
#27882: Allow template fragment caching for unlimited time
--------------------------------------+------------------------------------

Reporter: MikiSoft | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Template system | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Tim Graham):

* has_patch: 0 => 1
* needs_tests: 0 => 1
* easy: 0 => 1
* stage: Unreviewed => Accepted


Comment:

[https://docs.djangoproject.com/en/stable/topics/cache/#basic-usage 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:
{{{ #!diff
diff --git a/django/templatetags/cache.py b/django/templatetags/cache.py
index 3af6dc4..9e402a1 100644
--- a/django/templatetags/cache.py
+++ b/django/templatetags/cache.py
@@ -20,10 +20,11 @@ class CacheNode(Node):
expire_time = self.expire_time_var.resolve(context)
except VariableDoesNotExist:
raise TemplateSyntaxError('"cache" tag got an unknown
variable: %r' % self.expire_time_var.var)
- try:
- expire_time = int(expire_time)
- except (ValueError, TypeError):
- raise TemplateSyntaxError('"cache" tag got a non-integer
timeout value: %r' % expire_time)
+ if expire_time is not None:
+ try:
+ expire_time = int(expire_time)
+ except (ValueError, TypeError):
+ raise TemplateSyntaxError('"cache" tag got a non-integer
timeout value: %r' % expire_time)
if self.cache_name:
try:
cache_name = self.cache_name.resolve(context)
}}}
A test and perhaps a documentation update is also required.

--
Ticket URL: <https://code.djangoproject.com/ticket/27882#comment:3>

Django

unread,
Feb 27, 2017, 10:31:50 AM2/27/17
to django-...@googlegroups.com
#27882: Allow template fragment caching for unlimited time
--------------------------------------+------------------------------------

Reporter: MikiSoft | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Template system | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Description changed by MikiSoft:

Old description:

> Apparently, I've discovered that `{% cache 0 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 not caching it at
> all, and I don't see any other solution to this. So can someone make it
> to accept zero as a parameter for unlimited time, like it is in low-level
> API?

New description:

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?

--

--
Ticket URL: <https://code.djangoproject.com/ticket/27882#comment:4>

Django

unread,
Feb 27, 2017, 10:35:43 AM2/27/17
to django-...@googlegroups.com
#27882: Allow template fragment caching for unlimited time
--------------------------------------+------------------------------------

Reporter: MikiSoft | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Template system | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by MikiSoft):

Replying to [comment:3 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?

--
Ticket URL: <https://code.djangoproject.com/ticket/27882#comment:5>

Django

unread,
Feb 27, 2017, 10:43:01 AM2/27/17
to django-...@googlegroups.com
#27882: Allow template fragment caching for unlimited time
-------------------------------------+-------------------------------------
Reporter: MikiSoft | Owner: Bo
Type: | Marchman
Cleanup/optimization | Status: assigned

Component: Template system | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Bo Marchman):

* owner: nobody => Bo Marchman
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/27882#comment:6>

Django

unread,
Feb 27, 2017, 12:25:16 PM2/27/17
to django-...@googlegroups.com
#27882: Allow template fragment caching for unlimited time
-------------------------------------+-------------------------------------
Reporter: MikiSoft | Owner: Bo
Type: | Marchman
Cleanup/optimization | Status: assigned
Component: Template system | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Bo Marchman):

* needs_tests: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/27882#comment:7>

Django

unread,
Feb 27, 2017, 12:27:05 PM2/27/17
to django-...@googlegroups.com
#27882: Allow template fragment caching for unlimited time
-------------------------------------+-------------------------------------
Reporter: MikiSoft | Owner: Bo
Type: | Marchman
Cleanup/optimization | Status: assigned
Component: Template system | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Bo Marchman):

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

--
Ticket URL: <https://code.djangoproject.com/ticket/27882#comment:8>

Django

unread,
Mar 6, 2017, 9:26:03 AM3/6/17
to django-...@googlegroups.com
#27882: Allow template fragment caching for unlimited time
-------------------------------------+-------------------------------------
Reporter: MikiSoft | Owner: Bo
Type: | Marchman
Cleanup/optimization | Status: assigned
Component: Template system | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* needs_better_patch: 0 => 1


Comment:

I left a few comments for improvement on the PR.

--
Ticket URL: <https://code.djangoproject.com/ticket/27882#comment:9>

Django

unread,
Mar 13, 2017, 11:02:08 AM3/13/17
to django-...@googlegroups.com
#27882: Allow template fragment caching for unlimited time
-------------------------------------+-------------------------------------
Reporter: MikiSoft | Owner: Bo
Type: | Marchman
Cleanup/optimization | Status: assigned
Component: Template system | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Bo Marchman):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/27882#comment:10>

Django

unread,
Mar 13, 2017, 11:02:48 AM3/13/17
to django-...@googlegroups.com
#27882: Allow template fragment caching for unlimited time
-------------------------------------+-------------------------------------
Reporter: MikiSoft | Owner: Bo
Type: | Marchman
Cleanup/optimization | Status: assigned
Component: Template system | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Bo Marchman):

Requested changes made.

--
Ticket URL: <https://code.djangoproject.com/ticket/27882#comment:11>

Django

unread,
Mar 15, 2017, 1:01:31 PM3/15/17
to django-...@googlegroups.com
#27882: Allow template fragment caching for unlimited time
-------------------------------------+-------------------------------------
Reporter: MikiSoft | Owner: Bo
Type: | Marchman
Cleanup/optimization | Status: closed

Component: Template system | Version: 1.10
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"7a7b331cd5975477597dac4dec7ee0ddb67f59e0" 7a7b331c]:
{{{
#!CommitTicketReference repository=""
revision="7a7b331cd5975477597dac4dec7ee0ddb67f59e0"
Fixed #27882 -- Allowed {% cache %} to cache indefinitely.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27882#comment:12>

Reply all
Reply to author
Forward
0 new messages