diff --git a/django/core/cache/backends/base.py b/django/core/cache/backends/base.py
index f7573b2..1d767cc 100644
a
|
b
|
class InvalidCacheBackendError(ImproperlyConfigured):
|
12 | 12 | class CacheKeyWarning(DjangoRuntimeWarning): |
13 | 13 | pass |
14 | 14 | |
| 15 | class CacheKeyError(ValueError): |
| 16 | pass |
| 17 | |
| 18 | |
15 | 19 | # Memcached does not accept keys longer than this. |
16 | 20 | MEMCACHE_MAX_KEY_LENGTH = 250 |
17 | 21 | |
… |
… |
class BaseCache(object):
|
80 | 84 | new_key = self.key_func(key, self.key_prefix, version) |
81 | 85 | return new_key |
82 | 86 | |
| 87 | def _check_key(self, key): |
| 88 | """ |
| 89 | Checks if key can be used by any cache backend. Raises CacheKeyError |
| 90 | if not. |
| 91 | |
| 92 | This is inner check method, it checks given key as is. |
| 93 | """ |
| 94 | if len(key) > MEMCACHE_MAX_KEY_LENGTH: |
| 95 | raise CacheKeyError('Cache key will cause errors if used with ' |
| 96 | 'memcached: %s (longer than %s)' % (key, |
| 97 | MEMCACHE_MAX_KEY_LENGTH)) |
| 98 | for char in key: |
| 99 | if ord(char) < 33 or ord(char) == 127: |
| 100 | raise CacheKeyError('Cache key contains characters that will ' |
| 101 | 'cause errors if used with memcached: %r' % |
| 102 | key) |
| 103 | |
| 104 | def check_key(self, key): |
| 105 | """ |
| 106 | Returns whether key can be used by any cache backend. |
| 107 | """ |
| 108 | try: |
| 109 | self._check_key(self.make_key(key)) |
| 110 | except CacheKeyError: |
| 111 | return False |
| 112 | return True |
| 113 | |
83 | 114 | def add(self, key, value, timeout=None, version=None): |
84 | 115 | """ |
85 | 116 | Set a value in the cache if the key does not already exist. If |
… |
… |
class BaseCache(object):
|
191 | 222 | cache code. |
192 | 223 | |
193 | 224 | """ |
194 | | if len(key) > MEMCACHE_MAX_KEY_LENGTH: |
195 | | warnings.warn('Cache key will cause errors if used with memcached: ' |
196 | | '%s (longer than %s)' % (key, MEMCACHE_MAX_KEY_LENGTH), |
197 | | CacheKeyWarning) |
198 | | for char in key: |
199 | | if ord(char) < 33 or ord(char) == 127: |
200 | | warnings.warn('Cache key contains characters that will cause ' |
201 | | 'errors if used with memcached: %r' % key, |
202 | | CacheKeyWarning) |
| 225 | try: |
| 226 | self._check_key(key) |
| 227 | except CacheKeyError, error: |
| 228 | warnings.warn(str(error), CacheKeyWarning) |
203 | 229 | |
204 | 230 | def incr_version(self, key, delta=1, version=None): |
205 | 231 | """Adds delta to the cache version for the supplied key. Returns the |