def set_page_url_cache(page_lookup, lang, site_id, url): from django.core.cache import cache cache.set(_page_url_key(page_lookup, lang, site_id), url, get_cms_setting('CACHE_DURATIONS')['content'], version=_get_cache_version()) _set_cache_version(_get_cache_version())
def set_placeholder_page_cache(page_lookup, lang, site_id, placeholder_name, content): from django.core.cache import cache cache.set(_placeholder_page_cache_key(page_lookup, lang, site_id, placeholder_name), content, get_cms_setting('CACHE_DURATIONS')['content'], version=_get_cache_version()) _set_cache_version(_get_cache_version())
def set_placeholder_page_cache(page_lookup, lang, site_id, placeholder_name, content): from django.core.cache import cache cache.set( _placeholder_page_cache_key(page_lookup, lang, site_id, placeholder_name), content, get_cms_setting("CACHE_DURATIONS")["content"], version=_get_cache_version(), ) _set_cache_version(_get_cache_version())
def set_placeholder_cache(placeholder, lang, content): """ 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'], version=_get_cache_version()) _set_cache_version(_get_cache_version())
def set_placeholder_cache(placeholder, lang, content): """ 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"], version=_get_cache_version(), ) _set_cache_version(_get_cache_version())
def get_placeholder_cache(placeholder, lang): """ Retrieves the cached content of a placeholder """ from django.core.cache import cache return cache.get(_placeholder_cache_key(placeholder, lang), version=_get_cache_version())
def save(self, *args, **kwargs): super(Redirection, self).save(*args, **kwargs) invalidate_cms_page_cache() cache.set('redir:%s:%s:%s' % (self.site_id, self.source, self.enabled), self, get_cms_setting('CACHE_DURATIONS')['menus'], version=_get_cache_version())
def set_blog_page_cache(response): from django.core.cache import cache if not get_cms_setting('PAGE_CACHE'): return response request = response._request save_cache = True for placeholder in getattr(request, 'placeholders', []): if not placeholder.cache_placeholder: save_cache = False break if hasattr(request, 'toolbar'): if request.toolbar.edit_mode or request.toolbar.show_toolbar: save_cache = False if request.user.is_authenticated(): save_cache = False if not save_cache: add_never_cache_headers(response) return response else: version = _get_cache_version() ttl = get_cms_setting('CACHE_DURATIONS')['content'] cache.set( _blog_page_cache_key(request), (response.content, response._headers), ttl, version=version ) # See note in invalidate_cms_page_cache() _set_cache_version(version)
def set_page_cache(response): from django.core.cache import cache if not get_cms_setting('PAGE_CACHE'): return response request = response._request save_cache = True for placeholder in getattr(request, 'placeholders', []): if not placeholder.cache_placeholder: save_cache = False break if hasattr(request, 'toolbar'): if request.toolbar.edit_mode or request.toolbar.show_toolbar: save_cache = False if request.user.is_authenticated(): save_cache = False if not save_cache: add_never_cache_headers(response) return response else: version = _get_cache_version() ttl = get_cms_setting('CACHE_DURATIONS')['content'] cache.set( _page_cache_key(request), (response.content, response._headers), ttl, version=version ) # See note in invalidate_cms_page_cache() _set_cache_version(version)
def set_page_cache(response): from django.core.cache import cache request = response._request toolbar = get_toolbar_from_request(request) is_authenticated = request.user.is_authenticated() if is_authenticated or toolbar._cache_disabled or not get_cms_setting( "PAGE_CACHE"): add_never_cache_headers(response) return response # This *must* be TZ-aware timestamp = now() placeholders = toolbar.content_renderer.get_rendered_placeholders() # Checks if there's a plugin using the legacy "cache = False" placeholder_ttl_list = [] vary_cache_on_set = set() for ph in placeholders: # get_cache_expiration() always returns: # EXPIRE_NOW <= int <= MAX_EXPIRATION_IN_SECONDS ttl = ph.get_cache_expiration(request, timestamp) vary_cache_on = ph.get_vary_cache_on(request) placeholder_ttl_list.append(ttl) if ttl and vary_cache_on: # We're only interested in vary headers if they come from # a cache-able placeholder. vary_cache_on_set |= set(vary_cache_on) if EXPIRE_NOW not in placeholder_ttl_list: if placeholder_ttl_list: min_placeholder_ttl = min(x for x in placeholder_ttl_list) else: # Should only happen when there are no placeholders at all min_placeholder_ttl = MAX_EXPIRATION_TTL ttl = min( get_cms_setting('CACHE_DURATIONS')['content'], min_placeholder_ttl) if ttl > 0: # Adds expiration, etc. to headers patch_response_headers(response, cache_timeout=ttl) patch_vary_headers(response, sorted(vary_cache_on_set)) version = _get_cache_version() # We also store the absolute expiration timestamp to avoid # recomputing it on cache-reads. expires_datetime = timestamp + timedelta(seconds=ttl) cache.set(_page_cache_key(request), ( response.content, response._headers, expires_datetime, ), ttl, version=version) # See note in invalidate_cms_page_cache() _set_cache_version(version) return response
def cache_key(self, template_name, template_dirs, skip=None): key = super(Loader, self).cache_key(template_name, template_dirs, skip) if template_name.startswith(settings.PAGE_TEMP_MEMCACHED): new_key = key + '-' + str(_get_cache_version()) if key in self.cached_templates: old_key = self.cached_templates[key] if old_key != new_key: del self.get_template_cache[old_key] self.cached_templates[key] = new_key key = new_key return key
def get_cms_templates_list(): from django.conf import settings import copy templates = copy.deepcopy(settings.BASE_CMS_TEMPLATES) # Fetch the templates list from the cache first from django.contrib.sites.models import Site from django.core.cache import cache from cms.cache import _get_cache_version from cms.utils import get_cms_setting site_id = Site.objects.get_current().pk version = _get_cache_version() custom_templates = cache.get('templ:%s' % (site_id), version=version) if custom_templates is None: custom_templates = dict() try: from .models import PageTemplate from django.contrib.sites.models import Site for template in PageTemplate.objects.only( 'id', 'name', 'site_id').filter(site_id=site_id, enabled=True): custom_templates[ template.get_template_html_name()] = template.name.title() except: from .constants import TEMPLATE_DIR import os, warnings warnings.warn( 'Getting templates by looking in the template directory') for template in os.listdir(TEMPLATE_DIR): template_name = get_name_from_filename(template) if template_name: custom_templates[template] = template_name.title() cache.set('templ:%s' % (site_id), custom_templates, get_cms_setting('CACHE_DURATIONS')['menus'], version=version) if not isinstance(custom_templates, long): templates.update(custom_templates) return templates
def get_page_url_cache(page_lookup, lang, site_id): from django.core.cache import cache return cache.get(_page_url_key(page_lookup, lang, site_id), version=_get_cache_version())
def set_xframe_cache(page, xframe_options): from django.core.cache import cache cache.set('cms:xframe_options:%s' % page.pk, xframe_options, version=_get_cache_version()) _set_cache_version(_get_cache_version())
def get_placeholder_page_cache(page_lookup, lang, site_id, placeholder_name): from django.core.cache import cache return cache.get(_placeholder_page_cache_key(page_lookup, lang, site_id, placeholder_name), version=_get_cache_version())
def clear_placeholder_cache(placeholder, lang): from django.core.cache import cache cache.delete(_placeholder_cache_key(placeholder, lang), version=_get_cache_version())
def set_page_cache(response): from django.core.cache import cache request = response._request toolbar = get_toolbar_from_request(request) is_authenticated = request.user.is_authenticated() if is_authenticated or toolbar._cache_disabled or not get_cms_setting("PAGE_CACHE"): add_never_cache_headers(response) return response # This *must* be TZ-aware timestamp = now() placeholders = toolbar.content_renderer.get_rendered_placeholders() # Checks if there's a plugin using the legacy "cache = False" placeholder_ttl_list = [] vary_cache_on_set = set() for ph in placeholders: # get_cache_expiration() always returns: # EXPIRE_NOW <= int <= MAX_EXPIRATION_IN_SECONDS ttl = ph.get_cache_expiration(request, timestamp) vary_cache_on = ph.get_vary_cache_on(request) placeholder_ttl_list.append(ttl) if ttl and vary_cache_on: # We're only interested in vary headers if they come from # a cache-able placeholder. vary_cache_on_set |= set(vary_cache_on) if EXPIRE_NOW not in placeholder_ttl_list: if placeholder_ttl_list: min_placeholder_ttl = min(x for x in placeholder_ttl_list) else: # Should only happen when there are no placeholders at all min_placeholder_ttl = MAX_EXPIRATION_TTL ttl = min( get_cms_setting('CACHE_DURATIONS')['content'], min_placeholder_ttl ) if ttl > 0: # Adds expiration, etc. to headers patch_response_headers(response, cache_timeout=ttl) patch_vary_headers(response, sorted(vary_cache_on_set)) version = _get_cache_version() # We also store the absolute expiration timestamp to avoid # recomputing it on cache-reads. expires_datetime = timestamp + timedelta(seconds=ttl) cache.set( _page_cache_key(request), ( response.content, response._headers, expires_datetime, ), ttl, version=version ) # See note in invalidate_cms_page_cache() _set_cache_version(version) return response
def get_blog_page_cache(request): from django.core.cache import cache return cache.get(_blog_page_cache_key(request), version=_get_cache_version())
def get_page_cache(request): from django.core.cache import cache return cache.get(_page_cache_key(request), version=_get_cache_version())
def get_placeholder_page_cache(page_lookup, lang, site_id, placeholder_name): from django.core.cache import cache return cache.get( _placeholder_page_cache_key(page_lookup, lang, site_id, placeholder_name), version=_get_cache_version() )
def test_cache_page(self): # Ensure that we're testing in an environment WITHOUT the MW cache... exclude = [ 'django.middleware.cache.UpdateCacheMiddleware', 'django.middleware.cache.FetchFromCacheMiddleware' ] overrides = dict() if getattr(settings, 'MIDDLEWARE', None): overrides['MIDDLEWARE'] = [mw for mw in settings.MIDDLEWARE if mw not in exclude] else: overrides['MIDDLEWARE_CLASSES'] = [mw for mw in settings.MIDDLEWARE_CLASSES if mw not in exclude] with self.settings(**overrides): # Silly to do these tests if this setting isn't True page_cache_setting = get_cms_setting('PAGE_CACHE') self.assertTrue(page_cache_setting) # Create a test page page1 = create_page('test page 1', 'nav_playground.html', 'en', published=True) # Add some content placeholder = page1.placeholders.filter(slot="body")[0] add_plugin(placeholder, "TextPlugin", 'en', body="English") add_plugin(placeholder, "TextPlugin", 'de', body="Deutsch") # Create a request object request = self.get_request(page1.get_path(), 'en') # Ensure that user is NOT authenticated self.assertFalse(request.user.is_authenticated()) # Test that the page is initially uncached with self.assertNumQueries(FuzzyInt(1, 24)): response = self.client.get('/en/') self.assertEqual(response.status_code, 200) # # Test that subsequent requests of the same page are cached by # asserting that they require fewer queries. # with self.assertNumQueries(0): response = self.client.get('/en/') self.assertEqual(response.status_code, 200) # # Test that the cache is invalidated on unpublishing the page # old_version = _get_cache_version() page1.unpublish('en') self.assertGreater(_get_cache_version(), old_version) # # Test that this means the page is actually not cached. # page1.publish('en') with self.assertNumQueries(FuzzyInt(1, 24)): response = self.client.get('/en/') self.assertEqual(response.status_code, 200) # # Test that the above behavior is different when CMS_PAGE_CACHE is # set to False (disabled) # with self.settings(CMS_PAGE_CACHE=False): # Test that the page is initially un-cached with self.assertNumQueries(FuzzyInt(1, 20)): response = self.client.get('/en/') self.assertEqual(response.status_code, 200) # # Test that subsequent requests of the same page are still requires DB # access. # with self.assertNumQueries(FuzzyInt(1, 20)): response = self.client.get('/en/') self.assertEqual(response.status_code, 200)