PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/cms/cache/__init__.py

http://github.com/divio/django-cms
Python | 87 lines | 32 code | 17 blank | 38 comment | 4 complexity | bbba5fa12c8b3d60621bbb263aee6f15 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. # -*- coding: utf-8 -*-
  2. import re
  3. from cms.utils.conf import get_cms_setting
  4. CMS_PAGE_CACHE_VERSION_KEY = get_cms_setting("CACHE_PREFIX") + '_PAGE_CACHE_VERSION'
  5. def _get_cache_version():
  6. """
  7. Returns the current page cache version, explicitly setting one if not
  8. defined.
  9. """
  10. from django.core.cache import cache
  11. version = cache.get(CMS_PAGE_CACHE_VERSION_KEY)
  12. if version:
  13. return version
  14. else:
  15. _set_cache_version(1)
  16. return 1
  17. def _set_cache_version(version):
  18. """
  19. Set the cache version to the specified value.
  20. """
  21. from django.core.cache import cache
  22. cache.set(
  23. CMS_PAGE_CACHE_VERSION_KEY,
  24. version,
  25. get_cms_setting('CACHE_DURATIONS')['content']
  26. )
  27. def invalidate_cms_page_cache():
  28. """
  29. Invalidates the CMS PAGE CACHE.
  30. """
  31. #
  32. # NOTE: We're using a cache versioning strategy for invalidating the page
  33. # cache when necessary. Instead of wiping all the old entries, we simply
  34. # increment the version number rendering all previous entries
  35. # inaccessible and left to expire naturally.
  36. #
  37. # ALSO NOTE: According to the Django documentation, a timeout value of
  38. # `None' (in version 1.6+) is supposed to mean "cache forever", however,
  39. # this is actually only implemented as only slightly less than 30 days in
  40. # some backends (memcached, in particular). In older Djangos, `None' means
  41. # "use default value". To avoid issues arising from different Django
  42. # versions and cache backend implementations, we will explicitly set the
  43. # lifespan of the CMS_PAGE_CACHE_VERSION entry to whatever is set in
  44. # settings.CACHE_DURATIONS['content']. This allows users to adjust as
  45. # necessary for their backend.
  46. #
  47. # To prevent writing cache entries that will live longer than our version
  48. # key, we will always re-write the current version number into the cache
  49. # just after we write any new cache entries, thus ensuring that the
  50. # version number will always outlive any entries written against that
  51. # version. This is a cheap operation.
  52. #
  53. # If there are no new cache writes before the version key expires, its
  54. # perfectly OK, since any previous entries cached against that version
  55. # will have also expired, so, it'd be pointless to try to access them
  56. # anyway.
  57. #
  58. version = _get_cache_version()
  59. _set_cache_version(version + 1)
  60. CLEAN_KEY_PATTERN = re.compile(r'[^a-zA-Z0-9_-]')
  61. def _clean_key(key):
  62. return CLEAN_KEY_PATTERN.sub('-', key)
  63. def _get_cache_key(name, page_lookup, lang, site_id):
  64. from cms.models import Page
  65. if isinstance(page_lookup, Page):
  66. page_key = str(page_lookup.pk)
  67. else:
  68. page_key = str(page_lookup)
  69. page_key = _clean_key(page_key)
  70. return get_cms_setting('CACHE_PREFIX') + name + '__page_lookup:' + page_key + '_site:' + str(site_id) + '_lang:' + str(lang)