mirror of
https://github.com/django/django.git
synced 2024-12-23 01:25:58 +00:00
Removed django.core.cache.get_cache() per deprecation timeline; refs #21012.
This commit is contained in:
parent
aff0e54d51
commit
d038c547b5
31
django/core/cache/__init__.py
vendored
31
django/core/cache/__init__.py
vendored
@ -13,19 +13,17 @@ object.
|
|||||||
See docs/topics/cache.txt for information on the public API.
|
See docs/topics/cache.txt for information on the public API.
|
||||||
"""
|
"""
|
||||||
from threading import local
|
from threading import local
|
||||||
import warnings
|
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core import signals
|
from django.core import signals
|
||||||
from django.core.cache.backends.base import (
|
from django.core.cache.backends.base import (
|
||||||
InvalidCacheBackendError, CacheKeyWarning, BaseCache)
|
InvalidCacheBackendError, CacheKeyWarning, BaseCache)
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
from django.utils.deprecation import RemovedInDjango19Warning
|
|
||||||
from django.utils.module_loading import import_string
|
from django.utils.module_loading import import_string
|
||||||
|
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'get_cache', 'cache', 'DEFAULT_CACHE_ALIAS', 'InvalidCacheBackendError',
|
'cache', 'DEFAULT_CACHE_ALIAS', 'InvalidCacheBackendError',
|
||||||
'CacheKeyWarning', 'BaseCache',
|
'CacheKeyWarning', 'BaseCache',
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -35,33 +33,6 @@ if DEFAULT_CACHE_ALIAS not in settings.CACHES:
|
|||||||
raise ImproperlyConfigured("You must define a '%s' cache" % DEFAULT_CACHE_ALIAS)
|
raise ImproperlyConfigured("You must define a '%s' cache" % DEFAULT_CACHE_ALIAS)
|
||||||
|
|
||||||
|
|
||||||
def get_cache(backend, **kwargs):
|
|
||||||
"""
|
|
||||||
Function to create a cache backend dynamically. This is flexible by design
|
|
||||||
to allow different use cases:
|
|
||||||
|
|
||||||
To load a backend that is pre-defined in the settings::
|
|
||||||
|
|
||||||
cache = get_cache('default')
|
|
||||||
|
|
||||||
To create a backend with its dotted import path,
|
|
||||||
including arbitrary options::
|
|
||||||
|
|
||||||
cache = get_cache('django.core.cache.backends.memcached.MemcachedCache', **{
|
|
||||||
'LOCATION': '127.0.0.1:11211', 'TIMEOUT': 30,
|
|
||||||
})
|
|
||||||
|
|
||||||
"""
|
|
||||||
warnings.warn("'get_cache' is deprecated in favor of 'caches'.",
|
|
||||||
RemovedInDjango19Warning, stacklevel=2)
|
|
||||||
cache = _create_cache(backend, **kwargs)
|
|
||||||
# Some caches -- python-memcached in particular -- need to do a cleanup at the
|
|
||||||
# end of a request cycle. If not implemented in a particular backend
|
|
||||||
# cache.close is a no-op
|
|
||||||
signals.request_finished.connect(cache.close)
|
|
||||||
return cache
|
|
||||||
|
|
||||||
|
|
||||||
def _create_cache(backend, **kwargs):
|
def _create_cache(backend, **kwargs):
|
||||||
try:
|
try:
|
||||||
# Try to get the CACHES entry for the given backend name first
|
# Try to get the CACHES entry for the given backend name first
|
||||||
|
@ -544,7 +544,7 @@ Cache
|
|||||||
|
|
||||||
* Access to caches configured in :setting:`CACHES` is now available via
|
* Access to caches configured in :setting:`CACHES` is now available via
|
||||||
:data:`django.core.cache.caches`. This dict-like object provides a different
|
:data:`django.core.cache.caches`. This dict-like object provides a different
|
||||||
instance per thread. It supersedes :func:`django.core.cache.get_cache` which
|
instance per thread. It supersedes ``django.core.cache.get_cache()`` which
|
||||||
is now deprecated.
|
is now deprecated.
|
||||||
|
|
||||||
* If you instantiate cache backends directly, be aware that they aren't
|
* If you instantiate cache backends directly, be aware that they aren't
|
||||||
@ -1468,7 +1468,7 @@ Features deprecated in 1.7
|
|||||||
``django.core.cache.get_cache``
|
``django.core.cache.get_cache``
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
:func:`django.core.cache.get_cache` has been supplanted by
|
``django.core.cache.get_cache`` has been supplanted by
|
||||||
:data:`django.core.cache.caches`.
|
:data:`django.core.cache.caches`.
|
||||||
|
|
||||||
``django.utils.dictconfig``/``django.utils.importlib``
|
``django.utils.dictconfig``/``django.utils.importlib``
|
||||||
|
@ -747,21 +747,6 @@ Accessing the cache
|
|||||||
|
|
||||||
This object is equivalent to ``caches['default']``.
|
This object is equivalent to ``caches['default']``.
|
||||||
|
|
||||||
.. function:: django.core.cache.get_cache(backend, **kwargs)
|
|
||||||
|
|
||||||
.. deprecated:: 1.7
|
|
||||||
This function has been deprecated in favor of
|
|
||||||
:data:`~django.core.cache.caches`.
|
|
||||||
|
|
||||||
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
|
Basic usage
|
||||||
-----------
|
-----------
|
||||||
|
|
||||||
|
36
tests/cache/tests.py
vendored
36
tests/cache/tests.py
vendored
@ -17,9 +17,7 @@ import warnings
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core import management
|
from django.core import management
|
||||||
from django.core import signals
|
from django.core import signals
|
||||||
from django.core.cache import (cache, caches, CacheKeyWarning,
|
from django.core.cache import cache, caches, CacheKeyWarning, DEFAULT_CACHE_ALIAS
|
||||||
InvalidCacheBackendError, DEFAULT_CACHE_ALIAS, get_cache,
|
|
||||||
close_caches)
|
|
||||||
from django.db import connection, connections, transaction
|
from django.db import connection, connections, transaction
|
||||||
from django.core.cache.utils import make_template_fragment_key
|
from django.core.cache.utils import make_template_fragment_key
|
||||||
from django.http import HttpRequest, HttpResponse, StreamingHttpResponse
|
from django.http import HttpRequest, HttpResponse, StreamingHttpResponse
|
||||||
@ -30,14 +28,13 @@ from django.template import engines
|
|||||||
from django.template.context_processors import csrf
|
from django.template.context_processors import csrf
|
||||||
from django.template.response import TemplateResponse
|
from django.template.response import TemplateResponse
|
||||||
from django.test import (TestCase, TransactionTestCase, RequestFactory,
|
from django.test import (TestCase, TransactionTestCase, RequestFactory,
|
||||||
ignore_warnings, override_settings)
|
override_settings)
|
||||||
from django.test.signals import setting_changed
|
from django.test.signals import setting_changed
|
||||||
from django.utils import six
|
from django.utils import six
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils import translation
|
from django.utils import translation
|
||||||
from django.utils.cache import (patch_vary_headers, get_cache_key,
|
from django.utils.cache import (patch_vary_headers, get_cache_key,
|
||||||
learn_cache_key, patch_cache_control, patch_response_headers)
|
learn_cache_key, patch_cache_control, patch_response_headers)
|
||||||
from django.utils.deprecation import RemovedInDjango19Warning
|
|
||||||
from django.utils.encoding import force_text
|
from django.utils.encoding import force_text
|
||||||
from django.views.decorators.cache import cache_page
|
from django.views.decorators.cache import cache_page
|
||||||
|
|
||||||
@ -1221,40 +1218,13 @@ class CustomCacheKeyValidationTests(TestCase):
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
class GetCacheTests(TestCase):
|
class CacheClosingTests(TestCase):
|
||||||
|
|
||||||
@ignore_warnings(category=RemovedInDjango19Warning)
|
|
||||||
def test_simple(self):
|
|
||||||
self.assertIsInstance(
|
|
||||||
caches[DEFAULT_CACHE_ALIAS],
|
|
||||||
get_cache('default').__class__
|
|
||||||
)
|
|
||||||
|
|
||||||
cache = get_cache(
|
|
||||||
'django.core.cache.backends.dummy.DummyCache',
|
|
||||||
**{'TIMEOUT': 120}
|
|
||||||
)
|
|
||||||
self.assertEqual(cache.default_timeout, 120)
|
|
||||||
|
|
||||||
self.assertRaises(InvalidCacheBackendError, get_cache, 'does_not_exist')
|
|
||||||
|
|
||||||
def test_close(self):
|
def test_close(self):
|
||||||
self.assertFalse(cache.closed)
|
self.assertFalse(cache.closed)
|
||||||
signals.request_finished.send(self.__class__)
|
signals.request_finished.send(self.__class__)
|
||||||
self.assertTrue(cache.closed)
|
self.assertTrue(cache.closed)
|
||||||
|
|
||||||
@ignore_warnings(category=RemovedInDjango19Warning)
|
|
||||||
def test_close_deprecated(self):
|
|
||||||
cache = get_cache('cache.closeable_cache.CacheClass')
|
|
||||||
self.assertFalse(cache.closed)
|
|
||||||
# Ensure that we don't close the global cache instances.
|
|
||||||
signals.request_finished.disconnect(close_caches)
|
|
||||||
try:
|
|
||||||
signals.request_finished.send(self.__class__)
|
|
||||||
self.assertTrue(cache.closed)
|
|
||||||
finally:
|
|
||||||
signals.request_finished.connect(close_caches)
|
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_MEMORY_CACHES_SETTINGS = {
|
DEFAULT_MEMORY_CACHES_SETTINGS = {
|
||||||
'default': {
|
'default': {
|
||||||
|
Loading…
Reference in New Issue
Block a user