Get the Django decorator/middleware cache key for given URL

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import hashlib
from django.utils.encoding import iri_to_uri
from django.conf import settings
from django.utils.translation import get_language

def url_cache_key(url, language=None, key_prefix=None):
    if key_prefix is None:
        key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
    ctx = hashlib.md5()
    path = hashlib.md5(iri_to_uri(url))
    cache_key = 'views.decorators.cache.cache_page.%s.%s.%s.%s' % (
        key_prefix, 'GET', path.hexdigest(), ctx.hexdigest())
    if settings.USE_I18N:
        cache_key += '.%s' % (language or get_language())
    return cache_key

More like this

  1. Scalable and invalidateble cache_page decorator by marinho 4 years, 11 months ago
  2. Extended cacheable callables and properties by gsakkis 3 years, 10 months ago
  3. Caching Decorator by spenczar 1 year, 11 months ago
  4. Language aware cache decorator by bartTC 4 years, 11 months ago
  5. Decorator cache handler per view by guicifuentes 5 years, 7 months ago

Comments

(Forgotten your password?)