Esempio n. 1
0
    def test_get_placeholder_cache_key(self):
        version, vary_on_list = _get_placeholder_cache_version(self.placeholder, 'en', 1)
        desired_key = '{prefix}|render_placeholder|id:{id}|lang:{lang}|site:{site}|tz:{tz}|v:{version}|country-code:{cc}'.format(  # noqa
            prefix=get_cms_setting('CACHE_PREFIX'),
            id=self.placeholder.pk,
            lang='en',
            site=1,
            tz=get_timezone_name(),
            version=version,
            cc='_',
        )
        _set_placeholder_cache_version(self.placeholder, 'en', 1, version, vary_on_list=vary_on_list, duration=1)
        actual_key = _get_placeholder_cache_key(self.placeholder, 'en', 1, self.en_request)
        self.assertEqual(actual_key, desired_key)

        en_key = _get_placeholder_cache_key(self.placeholder, 'en', 1, self.en_request)
        de_key = _get_placeholder_cache_key(self.placeholder, 'de', 1, self.de_request)
        self.assertNotEqual(en_key, de_key)

        en_us_key = _get_placeholder_cache_key(self.placeholder, 'en', 1, self.en_us_request)
        self.assertNotEqual(en_key, en_us_key)

        desired_key = '{prefix}|render_placeholder|id:{id}|lang:{lang}|site:{site}|tz:{tz}|v:{version}|country-code:{cc}'.format(  # noqa
            prefix=get_cms_setting('CACHE_PREFIX'),
            id=self.placeholder.pk,
            lang='en',
            site=1,
            tz=get_timezone_name(),
            version=version,
            cc='US',
        )
        self.assertEqual(en_us_key, desired_key)
Esempio n. 2
0
def _page_cache_key(request):
    #md5 key of current path
    cache_key = "%s:%d:%s" % (
        get_cms_setting("CACHE_PREFIX"), settings.SITE_ID,
        hashlib.md5(iri_to_uri(
            request.get_full_path()).encode('utf-8')).hexdigest())
    if settings.USE_TZ:
        cache_key += '.%s' % get_timezone_name()
    return cache_key
Esempio n. 3
0
def _page_cache_key(request):
    #md5 key of current path
    cache_key = "%s:%d:%s" % (
        get_cms_setting("CACHE_PREFIX"),
        settings.SITE_ID,
        hashlib.md5(iri_to_uri(request.get_full_path()).encode('utf-8')).hexdigest()
    )
    if settings.USE_TZ:
        cache_key += '.%s' % get_timezone_name()
    return cache_key
Esempio n. 4
0
def _get_placeholder_cache_key(placeholder,
                               lang,
                               site_id,
                               request,
                               soft=False):
    """
    Returns the fully-addressed cache key for the given placeholder and
    the request.

    The kwarg «soft» should be set to True if getting the cache key to then
    read from the cache. If instead the key retrieval is to support a cache
    write, let «soft» be False.
    """
    prefix = get_cms_setting('CACHE_PREFIX')
    version, vary_on_list = _get_placeholder_cache_version(
        placeholder, lang, site_id)
    main_key = '{prefix}|render_placeholder|id:{id}|lang:{lang}|site:{site}|tz:{tz}|v:{version}'.format(
        prefix=prefix,
        id=placeholder.pk,
        lang=lang,
        site=site_id,
        tz=get_timezone_name(),
        version=version,
    )

    if not soft:
        # We are about to write to the cache, so we want to get the latest
        # vary_cache_on headers and the correct cache expiration, ignoring any
        # we already have. If the placeholder has already been rendered, this
        # will be very efficient (zero-additional queries) due to the caching
        # of all its plugins during the rendering process anyway.
        vary_on_list = placeholder.get_vary_cache_on(request)
        duration = placeholder.get_cache_expiration(request, now())
        # Update the main placeholder cache version
        _set_placeholder_cache_version(placeholder, lang, site_id, version,
                                       vary_on_list, duration)

    sub_key_list = []
    for key in vary_on_list:
        value = request.META.get(get_header_name(key)) or '_'
        sub_key_list.append(key + ':' + value)

    cache_key = main_key
    if sub_key_list:
        cache_key += '|' + '|'.join(sub_key_list)

    if len(cache_key) > 250:
        cache_key = '{prefix}|{hash}'.format(
            prefix=prefix,
            hash=hashlib.sha1(cache_key.encode('utf-8')).hexdigest(),
        )

    return cache_key
Esempio n. 5
0
def _get_placeholder_cache_key(placeholder, lang, site_id, request, soft=False):
    """
    Returns the fully-addressed cache key for the given placeholder and
    the request.

    The kwarg «soft» should be set to True if getting the cache key to then
    read from the cache. If instead the key retrieval is to support a cache
    write, let «soft» be False.
    """
    prefix = get_cms_setting('CACHE_PREFIX')
    version, vary_on_list = _get_placeholder_cache_version(placeholder, lang, site_id)
    main_key = '{prefix}|render_placeholder|id:{id}|lang:{lang}|site:{site}|tz:{tz}|v:{version}'.format(
        prefix=prefix,
        id=placeholder.pk,
        lang=lang,
        site=site_id,
        tz=get_timezone_name(),
        version=version,
    )

    if not soft:
        # We are about to write to the cache, so we want to get the latest
        # vary_cache_on headers and the correct cache expiration, ignoring any
        # we already have. If the placeholder has already been rendered, this
        # will be very efficient (zero-additional queries) due to the caching
        # of all its plugins during the rendering process anyway.
        vary_on_list = placeholder.get_vary_cache_on(request)
        duration = placeholder.get_cache_expiration(request, now())
        # Update the main placeholder cache version
        _set_placeholder_cache_version(
            placeholder, lang, site_id, version, vary_on_list, duration)

    sub_key_list = []
    for key in vary_on_list:
        value = request.META.get(get_header_name(key)) or '_'
        sub_key_list.append(key + ':' + value)

    cache_key = main_key
    if sub_key_list:
        cache_key += '|' + '|'.join(sub_key_list)

    if len(cache_key) > 250:
        cache_key = '{prefix}|{hash}'.format(
            prefix=prefix,
            hash=hashlib.sha1(cache_key.encode('utf-8')).hexdigest(),
        )

    return cache_key