1
0
mirror of https://github.com/django/django.git synced 2025-10-26 07:06:08 +00:00

Fixed #21012 -- Thread-local caches, like databases.

This commit is contained in:
Curtis Maloney
2013-10-19 09:49:24 +11:00
committed by Aymeric Augustin
parent 3ca0815c0b
commit ee7eb0f73e
14 changed files with 303 additions and 143 deletions

View File

@@ -215,6 +215,9 @@ these changes.
* The internal ``django.utils.functional.memoize`` will be removed.
* ``get_cache`` from django.core.cache will be removed. Instead, use
``create_cache`` or ``caches``, depending on your need.
2.0
---

View File

@@ -269,6 +269,26 @@ Minor features
allowing the ``published`` element to be included in the feed (which
relies on ``pubdate``).
Cache
^^^^^
* Access to caches configured in ``settings.CACHES`` is now available via
``django.core.cache.caches``. This will now return a different instance per
thread.
* A new function ``django.core.cache.create_cache`` has been added to make it
clearer what's happening. ``django.core.cache.get_cache`` will call this
if it's passed anything other than just a cache config alias.
* ``django.core.cache.get_cache`` has been deprecated. Use
``django.core.cache.caches`` to access caches configurd in
``settings.CACHES``, or ``django.core.cache.create_cache`` to create ad-hoc
instances.
* All thread safety in cache backends has been removed, as
``django.core.cache.caches`` now yields differend backend instances per
thread.
Email
^^^^^
@@ -643,6 +663,12 @@ Miscellaneous
Features deprecated in 1.7
==========================
``django.core.cache.get_cache``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
``django.core.cache.get_cache`` has been supplanted by
``django.core.cache.caches`` and ``django.core.cache.create_cache``.
``django.utils.dictconfig``/``django.utils.importlib``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@@ -703,7 +703,22 @@ pickling.)
Accessing the cache
-------------------
.. function:: django.core.cache.get_cache(backend, **kwargs)
.. versionadded:: 1.7
You can access the caches configured in ``settings.CACHES`` through the
dict-like ``django.core.cache.caches`` object. Repeated requests for the same
alias 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.
The ``caches`` dict is thread aware, so a different instance of each alias will
be returned for each thread.
The cache module, ``django.core.cache``, has a ``cache`` object that's
automatically created from the ``'default'`` entry in the :setting:`CACHES`
@@ -711,13 +726,43 @@ setting::
>>> 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::
This is a proxy object to caches['default']. It is provided for backward
compatiblity.
.. function:: django.core.cache.create_cache(backend, **kwargs)
You can create caches from ad-hoc configurations using ``create_cache``.
>>> from django.core.cache import create_cache
# Create an instance of a specific backend
>>> cache = create_cache(
'django.core.cache.backends.memcached.MemcachedCache',
LOCATION='/tmp/memcached.sock'
)
# Create a separate copy of the 'default' cache:
>>> new_default = create_cache('default')
# Create a cache with the same config as 'default', but a different timeout
>>> cache2 = create_cache('default', TIMEOUT=1)
This is guaranteed to always create a new instance.
.. function:: django.core.cache.get_cache(backend, **kwargs)
.. deprecated:: 1.7
This function has been deprecated in favour of ``caches`` and
``create_cache``.
Before Django 1.7 this was the only way to get a cache instance. Now it acts
as a wrapper to ``create_cache``, except in the case where it is passed only a
configured alias, where it will return the cache from ``caches``::
>>> from django.core.cache import get_cache
>>> cache = get_cache('alternate')
If the named key does not exist, ``InvalidCacheBackendError`` will be raised.
# Passes call to create_cache
>>> cache = get_cache('django.core.cache.backends.memcached.MemcachedCache', LOCATION='127.0.0.2')
# Creates a new cache based on the config in settings.CACHES['default']
>>> cache = get_cache('default', TIMEOUT=300)
# Returns instance from caches object
>>> cache = get_cache('default')
Basic usage