Skip to content

Commit c26f8a0

Browse files
committed
Fix matthewwithanm#317 RemovedInDjango19Warning: 'get_cache'
Make a `get_cache` function in `utils` which will call `django.core.cache.get_cache` if we are running Django < 1.7 or will mimic it's behaviour for newer versions of Django
1 parent 6bb45bc commit c26f8a0

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

imagekit/cachefiles/backends.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
from ..utils import get_singleton, sanitize_cache_key
1+
from ..utils import get_singleton, get_cache, sanitize_cache_key
22
import warnings
33
from copy import copy
4-
from django.core.cache import get_cache
54
from django.core.exceptions import ImproperlyConfigured
65

76

imagekit/utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from hashlib import md5
66

77
from django.conf import settings
8+
from django.core import signals
89
from django.core.exceptions import ImproperlyConfigured
910
from django.core.files import File
1011
try:
@@ -151,6 +152,25 @@ def call_strategy_method(file, method_name):
151152
fn(file)
152153

153154

155+
def get_cache(backend, **kwargs):
156+
"""
157+
Compatibilty wrapper for getting Django's cache backend instance
158+
"""
159+
try:
160+
from django.core.cache import _create_cache
161+
except ImportError:
162+
# Django < 1.7
163+
from django.core.cache import get_cache as _get_cache
164+
return _get_cache(backend, **kwargs)
165+
166+
cache = _create_cache(backend, **kwargs)
167+
# Some caches -- python-memcached in particular -- need to do a cleanup at the
168+
# end of a request cycle. If not implemented in a particular backend
169+
# cache.close is a no-op
170+
signals.request_finished.connect(cache.close)
171+
return cache
172+
173+
154174
def sanitize_cache_key(key):
155175
if settings.IMAGEKIT_USE_MEMCACHED_SAFE_CACHE_KEY:
156176
# Memcached keys can't contain whitespace or control characters.

0 commit comments

Comments
 (0)