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
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