/examples/itemisApp.gae/src/django/core/cache/__init__.py

https://github.com/RefuX/applause · Python · 86 lines · 56 code · 10 blank · 20 comment · 12 complexity · 1e666cf197c5ac1088592842ec875f17 MD5 · raw file

  1. """
  2. Caching framework.
  3. This package defines set of cache backends that all conform to a simple API.
  4. In a nutshell, a cache is a set of values -- which can be any object that
  5. may be pickled -- identified by string keys. For the complete API, see
  6. the abstract BaseCache class in django.core.cache.backends.base.
  7. Client code should not access a cache backend directly; instead it should
  8. either use the "cache" variable made available here, or it should use the
  9. get_cache() function made available here. get_cache() takes a backend URI
  10. (e.g. "memcached://127.0.0.1:11211/") and returns an instance of a backend
  11. cache class.
  12. See docs/cache.txt for information on the public API.
  13. """
  14. try:
  15. # The mod_python version is more efficient, so try importing it first.
  16. from mod_python.util import parse_qsl
  17. except ImportError:
  18. try:
  19. # Python 2.6 and greater
  20. from urlparse import parse_qsl
  21. except ImportError:
  22. # Python 2.5, 2.4. Works on Python 2.6 but raises
  23. # PendingDeprecationWarning
  24. from cgi import parse_qsl
  25. from django.conf import settings
  26. from django.core import signals
  27. from django.core.cache.backends.base import InvalidCacheBackendError, CacheKeyWarning
  28. from django.utils import importlib
  29. # Name for use in settings file --> name of module in "backends" directory.
  30. # Any backend scheme that is not in this dictionary is treated as a Python
  31. # import path to a custom backend.
  32. BACKENDS = {
  33. 'memcached': 'memcached',
  34. 'locmem': 'locmem',
  35. 'file': 'filebased',
  36. 'db': 'db',
  37. 'dummy': 'dummy',
  38. }
  39. def parse_backend_uri(backend_uri):
  40. """
  41. Converts the "backend_uri" into a cache scheme ('db', 'memcached', etc), a
  42. host and any extra params that are required for the backend. Returns a
  43. (scheme, host, params) tuple.
  44. """
  45. if backend_uri.find(':') == -1:
  46. raise InvalidCacheBackendError("Backend URI must start with scheme://")
  47. scheme, rest = backend_uri.split(':', 1)
  48. if not rest.startswith('//'):
  49. raise InvalidCacheBackendError("Backend URI must start with scheme://")
  50. host = rest[2:]
  51. qpos = rest.find('?')
  52. if qpos != -1:
  53. params = dict(parse_qsl(rest[qpos+1:]))
  54. host = rest[2:qpos]
  55. else:
  56. params = {}
  57. if host.endswith('/'):
  58. host = host[:-1]
  59. return scheme, host, params
  60. def get_cache(backend_uri):
  61. scheme, host, params = parse_backend_uri(backend_uri)
  62. if scheme in BACKENDS:
  63. name = 'django.core.cache.backends.%s' % BACKENDS[scheme]
  64. else:
  65. name = scheme
  66. module = importlib.import_module(name)
  67. return module.CacheClass(host, params)
  68. cache = get_cache(settings.CACHE_BACKEND)
  69. # Some caches -- pythont-memcached in particular -- need to do a cleanup at the
  70. # end of a request cycle. If the cache provides a close() method, wire it up
  71. # here.
  72. if hasattr(cache, 'close'):
  73. signals.request_finished.connect(cache.close)