mirror of
https://github.com/django/django.git
synced 2025-07-24 05:36:15 +00:00
Fixed #21012 -- New API to access cache backends.
Thanks Curtis Malony and Florian Apolloner. Squashed commit of the following: commit3380495e93
Author: Aymeric Augustin <aymeric.augustin@m4x.org> Date: Sat Nov 23 14:18:07 2013 +0100 Looked up the template_fragments cache at runtime. commit905a74f52b
Author: Aymeric Augustin <aymeric.augustin@m4x.org> Date: Sat Nov 23 14:19:48 2013 +0100 Removed all uses of create_cache. Refactored the cache tests significantly. Made it safe to override the CACHES setting. commit35e289fe92
Author: Aymeric Augustin <aymeric.augustin@m4x.org> Date: Sat Nov 23 12:23:57 2013 +0100 Removed create_cache function. commit8e274f747a
Author: Aymeric Augustin <aymeric.augustin@m4x.org> Date: Sat Nov 23 12:04:52 2013 +0100 Updated docs to describe a simplified cache backend API. commitee7eb0f73e
Author: Curtis Maloney <curtis@tinbrain.net> Date: Sat Oct 19 09:49:24 2013 +1100 Fixed #21012 -- Thread-local caches, like databases.
This commit is contained in:
parent
3ca0815c0b
commit
ffc37e2343
16 changed files with 729 additions and 606 deletions
|
@ -703,22 +703,50 @@ pickling.)
|
|||
Accessing the cache
|
||||
-------------------
|
||||
|
||||
.. data:: django.core.cache.caches
|
||||
|
||||
.. versionadded:: 1.7
|
||||
|
||||
You can access the caches configured in the :setting:`CACHES` setting
|
||||
through a dict-like object: ``django.core.cache.caches``. Repeated
|
||||
requests for the same alias in the same thread will return the same
|
||||
object.
|
||||
|
||||
>>> from django.core.cache import caches
|
||||
>>> cache1 = caches['myalias']
|
||||
>>> cache2 = caches['myalias']
|
||||
>>> cache1 is cache2
|
||||
True
|
||||
|
||||
If the named key does not exist, ``InvalidCacheBackendError`` will be
|
||||
raised.
|
||||
|
||||
To provide thread-safety, a different instance of the cache backend will
|
||||
be returned for each thread.
|
||||
|
||||
.. data:: django.core.cache.cache
|
||||
|
||||
As a shortcut, the default cache is available as
|
||||
``django.core.cache.cache``::
|
||||
|
||||
>>> from django.core.cache import cache
|
||||
|
||||
This object is equivalent to ``caches['default']``.
|
||||
|
||||
.. function:: django.core.cache.get_cache(backend, **kwargs)
|
||||
|
||||
The cache module, ``django.core.cache``, has a ``cache`` object that's
|
||||
automatically created from the ``'default'`` entry in the :setting:`CACHES`
|
||||
setting::
|
||||
.. deprecated:: 1.7
|
||||
This function has been deprecated in favour of
|
||||
:data:`~django.core.cache.caches`.
|
||||
|
||||
>>> from django.core.cache import cache
|
||||
|
||||
If you have multiple caches defined in :setting:`CACHES`, then you can use
|
||||
:func:`django.core.cache.get_cache` to retrieve a cache object for any key::
|
||||
|
||||
>>> from django.core.cache import get_cache
|
||||
>>> cache = get_cache('alternate')
|
||||
|
||||
If the named key does not exist, ``InvalidCacheBackendError`` will be raised.
|
||||
Before Django 1.7 this function was the canonical way to obtain a cache
|
||||
instance. It could also be used to create a new cache instance with a
|
||||
different configuration.
|
||||
|
||||
>>> from django.core.cache import get_cache
|
||||
>>> get_cache('default')
|
||||
>>> get_cache('django.core.cache.backends.memcached.MemcachedCache', LOCATION='127.0.0.2')
|
||||
>>> get_cache('default', TIMEOUT=300)
|
||||
|
||||
Basic usage
|
||||
-----------
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue