|
1 | 1 | import builtins
|
2 | 2 | from collections.abc import Iterable
|
3 | 3 | import random
|
4 |
| -import re |
5 | 4 | import socket
|
6 |
| -from contextlib import suppress |
7 | 5 | from typing import (
|
8 | 6 | Any,
|
9 | 7 | TYPE_CHECKING,
|
|
22 | 20 |
|
23 | 21 | from django_valkey import pool
|
24 | 22 | from django_valkey.compressors.identity import IdentityCompressor
|
25 |
| -from django_valkey.exceptions import CompressorError |
26 | 23 | from django_valkey.serializers.pickle import PickleSerializer
|
27 |
| -from django_valkey.util import CacheKey |
| 24 | +from django_valkey.util import make_key, make_pattern, encode, decode |
28 | 25 | from django_valkey.typings import KeyT
|
29 | 26 |
|
30 | 27 | if TYPE_CHECKING:
|
|
33 | 30 |
|
34 | 31 | _main_exceptions = (TimeoutError, ResponseError, ConnectionError, socket.timeout)
|
35 | 32 |
|
36 |
| -special_re = re.compile("([*?[])") |
37 |
| - |
38 |
| - |
39 |
| -def glob_escape(s: str) -> str: |
40 |
| - return special_re.sub(r"[\1]", s) |
41 |
| - |
42 | 33 |
|
43 | 34 | class BaseClient:
|
44 | 35 | def __init__(
|
@@ -127,59 +118,34 @@ def decode(self, value: bytes) -> Any:
|
127 | 118 | """
|
128 | 119 | Decode the given value.
|
129 | 120 | """
|
130 |
| - try: |
131 |
| - value = int(value) if value.isdigit() else float(value) |
132 |
| - except (ValueError, TypeError): |
133 |
| - # Handle little values, chosen to be not compressed |
134 |
| - with suppress(CompressorError): |
135 |
| - value = self._compressor.decompress(value) |
136 |
| - value = self._serializer.loads(value) |
137 |
| - except AttributeError: |
138 |
| - # if value is None: |
139 |
| - return value |
140 |
| - return value |
| 121 | + return decode(value, serializer=self._serializer, compressor=self._compressor) |
141 | 122 |
|
142 | 123 | def encode(self, value: EncodableT) -> bytes | int | float:
|
143 | 124 | """
|
144 | 125 | Encode the given value.
|
145 | 126 | """
|
146 |
| - |
147 |
| - if type(value) is not int and type(value) is not float: |
148 |
| - value = self._serializer.dumps(value) |
149 |
| - return self._compressor.compress(value) |
150 |
| - |
151 |
| - return value |
| 127 | + return encode(value, serializer=self._serializer, compressor=self._compressor) |
152 | 128 |
|
153 | 129 | def make_key(
|
154 | 130 | self, key: KeyT, version: int | None = None, prefix: str | None = None
|
155 | 131 | ) -> KeyT:
|
156 | 132 | """Return key as a CacheKey instance so it has additional methods"""
|
157 |
| - if isinstance(key, CacheKey): |
158 |
| - return key |
159 |
| - |
160 |
| - if prefix is None: |
161 |
| - prefix = self._backend.key_prefix |
162 |
| - |
163 |
| - if version is None: |
164 |
| - version = self._backend.version |
165 |
| - |
166 |
| - return CacheKey(self._backend.key_func(key, prefix, version)) |
| 133 | + return make_key( |
| 134 | + key, |
| 135 | + version=version or self._backend.version, |
| 136 | + prefix=prefix or self._backend.key_prefix, |
| 137 | + key_func=self._backend.key_func, |
| 138 | + ) |
167 | 139 |
|
168 | 140 | def make_pattern(
|
169 | 141 | self, pattern: str, version: int | None = None, prefix: str | None = None
|
170 | 142 | ) -> KeyT:
|
171 |
| - if isinstance(pattern, CacheKey): |
172 |
| - return pattern |
173 |
| - |
174 |
| - if prefix is None: |
175 |
| - prefix = self._backend.key_prefix |
176 |
| - prefix = glob_escape(prefix) |
177 |
| - |
178 |
| - if version is None: |
179 |
| - version = self._backend.version |
180 |
| - version_str = glob_escape(str(version)) |
181 |
| - |
182 |
| - return CacheKey(self._backend.key_func(pattern, prefix, version_str)) |
| 143 | + return make_pattern( |
| 144 | + pattern, |
| 145 | + version=version or self._backend.version, |
| 146 | + prefix=prefix or self._backend.key_prefix, |
| 147 | + key_func=self._backend.key_func, |
| 148 | + ) |
183 | 149 |
|
184 | 150 | def _decode_iterable_result(
|
185 | 151 | self, result: Any, convert_to_set: bool = True
|
|
0 commit comments