summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Johnson <me@adamj.eu>2023-06-27 08:11:09 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-06-28 07:48:18 +0200
commit1dbcf9a005842c3fdd97537f1bd4fab5b96b972f (patch)
treea851b58ea7e9301f29b6482b44c2e941f743e115
parent6fbe5287ac46f38faa9634e02da7e7b01e37c34d (diff)
Fixed #34681 -- Optimized memcache_key_warnings().
-rw-r--r--django/core/cache/backends/base.py16
1 files changed, 9 insertions, 7 deletions
diff --git a/django/core/cache/backends/base.py b/django/core/cache/backends/base.py
index eb4b3eac6d..09b0a5f9c8 100644
--- a/django/core/cache/backends/base.py
+++ b/django/core/cache/backends/base.py
@@ -6,6 +6,7 @@ from asgiref.sync import sync_to_async
from django.core.exceptions import ImproperlyConfigured
from django.utils.module_loading import import_string
+from django.utils.regex_helper import _lazy_re_compile
class InvalidCacheBackendError(ImproperlyConfigured):
@@ -388,16 +389,17 @@ class BaseCache:
pass
+memcached_error_chars_re = _lazy_re_compile(r"[\x00-\x20\x7f]")
+
+
def memcache_key_warnings(key):
if len(key) > MEMCACHE_MAX_KEY_LENGTH:
yield (
"Cache key will cause errors if used with memcached: %r "
"(longer than %s)" % (key, MEMCACHE_MAX_KEY_LENGTH)
)
- for char in key:
- if ord(char) < 33 or ord(char) == 127:
- yield (
- "Cache key contains characters that will cause errors if "
- "used with memcached: %r" % key
- )
- break
+ if memcached_error_chars_re.search(key):
+ yield (
+ "Cache key contains characters that will cause errors if used with "
+ f"memcached: {key!r}"
+ )