Skip to content

Commit

Permalink
Tweaks, properly set placeholder cache and honor settings...
Browse files Browse the repository at this point in the history
CMS_PAGE_CACHE, CMS_PLACEHOLDER_CACHE, CMS_PLUGIN_CACHE now all supported.
  • Loading branch information
mkoistinen committed Mar 23, 2016
1 parent dca712c commit dd4e320
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 19 deletions.
10 changes: 7 additions & 3 deletions cms/cache/placeholder.py
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
from django.conf import settings
from django.utils.encoding import force_text
from django.utils.timezone import get_current_timezone_name
from django.utils.timezone import get_current_timezone_name, now
from cms.cache import _get_cache_version, _set_cache_version, _clean_key, _get_cache_key
from cms.utils import get_cms_setting

Expand All @@ -14,14 +14,18 @@ def _placeholder_cache_key(placeholder, lang):
return cache_key


def set_placeholder_cache(placeholder, lang, content):
def set_placeholder_cache(placeholder, lang, content, request):
"""
Caches the rendering of a placeholder
"""
from django.core.cache import cache

cache.set(_placeholder_cache_key(placeholder, lang),
content,
get_cms_setting('CACHE_DURATIONS')['content'],
min(
get_cms_setting('CACHE_DURATIONS')['content'],
placeholder.get_cache_expiration(request, now())
),
version=_get_cache_version())
_set_cache_version(_get_cache_version())

Expand Down
8 changes: 5 additions & 3 deletions cms/models/placeholdermodel.py
Expand Up @@ -18,6 +18,7 @@
from cms.utils.i18n import get_language_object
from cms.utils.urlutils import admin_reverse
from cms.constants import EXPIRE_NOW, MAX_EXPIRATION_TTL
from cms.utils.conf import get_cms_setting


@python_2_unicode_compatible
Expand Down Expand Up @@ -322,15 +323,16 @@ def get_cache_expiration(self, request, response_timestamp):
for plugin_item in self.get_plugins(lang):
instance, plugin = plugin_item.get_plugin_instance()

if not self.cache_placeholder:
if not self.cache_placeholder or not get_cms_setting('PLUGIN_CACHE'): # noqa
# This placeholder has a plugin with an effective
# `cache = False` setting, so, no point in continuing.
# `cache = False` setting or the developer has explicitly
# disabled the PLUGIN_CACHE, so, no point in continuing.
return EXPIRE_NOW
plugin_expiration = plugin.get_cache_expiration(
request, instance, self)

# The plugin_expiration should only ever be either: None, a TZ-aware
# datetime, or an integer.
# datetime, a timedelta, or an integer.
if plugin_expiration is None:
# Do not consider plugins that return None
continue
Expand Down
2 changes: 1 addition & 1 deletion cms/plugin_rendering.py
Expand Up @@ -183,7 +183,7 @@ def render_placeholder(placeholder, context_to_copy, name_fallback="Placeholder"
result = render_to_string("cms/toolbar/content.html", flatten_context(context))
changes = watcher.get_changes()
if placeholder and not edit and placeholder.cache_placeholder and get_cms_setting('PLACEHOLDER_CACHE') and use_cache:
set_placeholder_cache(placeholder, lang, content={'content': result, 'sekizai': changes})
set_placeholder_cache(placeholder, lang, content={'content': result, 'sekizai': changes}, request=request)
context.pop()
return result

Expand Down
8 changes: 4 additions & 4 deletions cms/tests/test_cache.py
Expand Up @@ -48,7 +48,7 @@ def test_cache_placeholder(self):
request = self.get_request('/en/')
request.current_page = Page.objects.get(pk=page1.pk)
request.toolbar = CMSToolbar(request)
with self.assertNumQueries(5):
with self.assertNumQueries(9):
self.render_template_obj(template, {}, request)
request = self.get_request('/en/')
request.current_page = Page.objects.get(pk=page1.pk)
Expand Down Expand Up @@ -135,7 +135,7 @@ def test_no_cache_plugin(self):
request = self.get_request('/en/')
request.current_page = Page.objects.get(pk=page1.pk)
request.toolbar = CMSToolbar(request)
with self.assertNumQueries(4):
with self.assertNumQueries(6):
output = self.render_template_obj(template, {}, request)
with self.assertNumQueries(FuzzyInt(14, 24)): # was 19
response = self.client.get('/en/')
Expand Down Expand Up @@ -293,7 +293,7 @@ def test_expiration_cache_plugins(self):
request = self.get_request('/en/')
request.current_page = Page.objects.get(pk=page1.pk)
request.toolbar = CMSToolbar(request)
with self.assertNumQueries(FuzzyInt(14, 25)): # was 14, 24
with self.assertNumQueries(FuzzyInt(14, 26)):
response = self.client.get('/en/')
resp1 = response.content.decode('utf8').split("$$$")[1]
self.assertTrue('max-age=40' in response['Cache-Control'], response['Cache-Control'])
Expand Down Expand Up @@ -411,7 +411,7 @@ def test_cache_page(self):
# Test that this means the page is actually not cached.
#
page1.publish('en')
with self.assertNumQueries(FuzzyInt(1, 21)):
with self.assertNumQueries(FuzzyInt(1, 24)):
response = self.client.get('/en/')
self.assertEqual(response.status_code, 200)

Expand Down
2 changes: 1 addition & 1 deletion cms/tests/test_page.py
Expand Up @@ -1218,7 +1218,7 @@ def test_plugin_loading_queries(self):
# trigger the get_languages query so it doesn't get in our way
context = self.get_context(page=page)
context['request'].current_page.get_languages()
with self.assertNumQueries(4):
with self.assertNumQueries(37):
for i, placeholder in enumerate(placeholders):
content = get_placeholder_content(context, context['request'], page, placeholder.slot, False, None)
for j in range(5):
Expand Down
4 changes: 2 additions & 2 deletions cms/tests/test_templatetags.py
Expand Up @@ -305,7 +305,7 @@ def test_show_placeholder_for_page_marks_output_safe(self):
request.user = AnonymousUser()
request.current_page = page
template = "{% load cms_tags sekizai_tags %}{% show_placeholder slot page 'en' 1 %}{% render_block 'js' %}"
with self.assertNumQueries(4):
with self.assertNumQueries(6):
output = self.render_template_obj(template, {'page': page, 'slot': placeholder.slot}, request)
self.assertIn('<b>Test</b>', output)

Expand All @@ -324,7 +324,7 @@ def test_cached_show_placeholder_preview(self):
request.current_page = page.publisher_public
request.user = user
template = "{% load cms_tags %}{% show_placeholder slot page 'en' 1 %}"
with self.assertNumQueries(4):
with self.assertNumQueries(6):
output = self.render_template_obj(template, {'page': page, 'slot': placeholder.slot}, request)
self.assertIn('<b>Test</b>', output)
add_plugin(placeholder, TextPlugin, 'en', body='<b>Test2</b>')
Expand Down
2 changes: 1 addition & 1 deletion cms/tests/test_views.py
Expand Up @@ -258,7 +258,7 @@ def test_context_current_page(self):
# Number of queries when context processors is not enabled
with self.settings(**override):
# Baseline number of queries
with self.assertNumQueries(FuzzyInt(13, 23)) as context:
with self.assertNumQueries(FuzzyInt(13, 25)) as context:
response = self.client.get("/en/page-2/")
num_queries_page = len(context.captured_queries)
cache.clear()
Expand Down
5 changes: 3 additions & 2 deletions cms/utils/plugins.py
Expand Up @@ -12,7 +12,7 @@
from cms.exceptions import PluginLimitReached
from cms.models import Page, CMSPlugin
from cms.plugin_pool import plugin_pool
from cms.utils import get_cms_setting, get_language_from_request
from cms.utils import get_language_from_request
from cms.utils.i18n import get_fallback_languages
from cms.utils.moderator import get_cmsplugin_queryset
from cms.utils.permissions import has_plugin_permission
Expand Down Expand Up @@ -168,7 +168,8 @@ def downcast_plugins(queryset,
for pl in placeholders:
if instance.placeholder_id == pl.pk:
instance.placeholder = pl
if not cls().get_cache_expiration(request, instance, pl) and not cls.cache: # noqa
if not cls().get_cache_expiration(
request, instance, pl) and not cls.cache:
pl.cache_placeholder = False
# make the equivalent list of qs, but with downcasted instances
return [plugin_lookup.get(plugin.pk, plugin) for plugin in queryset]
Expand Down
4 changes: 3 additions & 1 deletion cms/views.py
Expand Up @@ -25,6 +25,7 @@ def details(request, slug):
The main view of the Django-CMS! Takes a request and a slug, renders the
page.
"""
response_timestamp = now()
if get_cms_setting("PAGE_CACHE") and (
not hasattr(request, 'toolbar') or (
not request.toolbar.edit_mode and
Expand All @@ -38,7 +39,8 @@ def details(request, slug):
response = HttpResponse(content)
response._headers = headers
# Recalculate the max-age header for this cached response
max_age = int((expires_datetime - now()).total_seconds() + 0.5)
max_age = int(
(expires_datetime - response_timestamp).total_seconds() + 0.5)
patch_cache_control(response, max_age=max_age)
return response

Expand Down
2 changes: 1 addition & 1 deletion docs/how_to/custom_plugins.rst
Expand Up @@ -200,7 +200,7 @@ objects to your context, but plugins can override this to include any context
that is required.

A number of other methods are available for overriding on your CMSPluginBase
sub-classes. See: :module:`cms.plugin_base` for further details.
sub-classes. See: :mod:`cms.plugin_base` for further details.


***************
Expand Down

0 comments on commit dd4e320

Please sign in to comment.