#20945 closed New feature (fixed)
Allow cache tag to use specified cache config.
Reported by: | FunkyBob | Owned by: | nobody |
---|---|---|---|
Component: | Template system | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Currently the cache tag will use the default cache - always.
However, the staticfiles app, for instance, can be configured as to which it will use.
I propose the following:
- Add a new setting (called DEFAULT_TEMPLATE_FRAGMENT_CACHE in the PR) to specify which to use by default.
- Extend the cache tag to allow the last argument to be using="cache name", falling back to the default if it doesn't exist.
Current code is:
https://github.com/django/django/pull/1490
I'm just working on the tests and final documentation updates.
Change History (15)
comment:1 by , 11 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 11 years ago
I don't think it makes sense to introduce a new dedicated default-cache setting just for the cache
template tag. The default cache is so named because it is, well, the default.
Being able to specify a cache in the tag syntax makes sense, though.
comment:3 by , 11 years ago
Well, yes, and no... I was basing it off the fact staticfiles, by default, will try to use a different cache backend and fall-back to default. I think this is a good pattern for more of Django to follow, allowing people to differentiate if they want.
In our current project we'd like to move all the template fragment caching to a specific backend.
Perhaps a middle ground solution, and have it look for a fixed name [as staticfiles does -- I got confused and thought it had a setting] such as "fragments", and fall back to default otherwise?
comment:4 by , 11 years ago
Hmm. In this case, given we apply the tag-argument part of this patch, it could be easily handled in your project with a simple custom wrapper to the cache tag that passes along a different cache name by default. But if we're going to add support directly in Django for redirecting all template-tag caching away from the default cache, I definitely prefer the staticfiles approach to a setting.
comment:7 by , 11 years ago
Timo,
I missed your comment there... yes, I have considered if it should be a hard error or not to specify a cache that doesn't exist.
I can see a case either way, to be honest. But I guess since there's now a fallback approach for "template_fragments" -> "default", when you define one explicitly we probably should go splodey if it's not there.
comment:8 by , 11 years ago
From IRC, mjtamlyn and I favor carljm's approach of a custom wrapper to the cache tag rather than always defaulting to a "template_fragments" cache.
comment:9 by , 11 years ago
*bump*
So, the PR is now ready, adding the following:
1) Allow specifying which cache backend a use of {% cache %} will store in by adding a using="cachename" to its args.
2) Having {% cache %} tags use the cache 'template_fragments' if it's configured, otherwise use 'default'
I see comments against (2), but I'd like to understand why people are against it.
comment:10 by , 11 years ago
@timo: can you say why and if that's a 0 or -1? I think that having an implicit default for the cache with a fallback to 'default' isn't that bad…
comment:11 by , 11 years ago
Just to clarify my viewpoint - I'm happy for there to be a new implicit default, I'm not happy for there to be a new setting. So overall I'm happy with both parts of the patch.
comment:12 by , 11 years ago
If other people like the implicit default, it's ok with me. My concern is hardcoding a setting that isn't configurable - seems like a small smell, but not too bad as it seems quite unlikely that someone would be using a cache_name of 'template_fragments' for another purpose.
comment:13 by , 11 years ago
Easy pickings: | unset |
---|
comment:14 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
You should probably raise the idea of adding a new setting on django-developers as we are averse to more settings and I'm not sure using a different cache than the "default" cache would be common enough to warrant it. If you have a use case for it, it would be helpful to describe it so we have some justification.
It may also be better if
using="cache name"
throws an error if the given cache doesn't exist (i.e. "fail loudly") -- what do you think?