funkycache 1.0.3__tar.gz → 1.0.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {funkycache-1.0.3/src/funkycache.egg-info → funkycache-1.0.4}/PKG-INFO +1 -1
- {funkycache-1.0.3 → funkycache-1.0.4}/src/funkycache/storage.py +82 -52
- {funkycache-1.0.3 → funkycache-1.0.4}/src/funkycache/version.py +1 -1
- {funkycache-1.0.3 → funkycache-1.0.4/src/funkycache.egg-info}/PKG-INFO +1 -1
- {funkycache-1.0.3 → funkycache-1.0.4}/tests/test_storage.py +26 -9
- {funkycache-1.0.3 → funkycache-1.0.4}/LICENSE +0 -0
- {funkycache-1.0.3 → funkycache-1.0.4}/README.md +0 -0
- {funkycache-1.0.3 → funkycache-1.0.4}/pyproject.toml +0 -0
- {funkycache-1.0.3 → funkycache-1.0.4}/setup.cfg +0 -0
- {funkycache-1.0.3 → funkycache-1.0.4}/src/funkycache/__init__.py +0 -0
- {funkycache-1.0.3 → funkycache-1.0.4}/src/funkycache/manager.py +0 -0
- {funkycache-1.0.3 → funkycache-1.0.4}/src/funkycache/py.typed +0 -0
- {funkycache-1.0.3 → funkycache-1.0.4}/src/funkycache.egg-info/SOURCES.txt +0 -0
- {funkycache-1.0.3 → funkycache-1.0.4}/src/funkycache.egg-info/dependency_links.txt +0 -0
- {funkycache-1.0.3 → funkycache-1.0.4}/src/funkycache.egg-info/requires.txt +0 -0
- {funkycache-1.0.3 → funkycache-1.0.4}/src/funkycache.egg-info/top_level.txt +0 -0
- {funkycache-1.0.3 → funkycache-1.0.4}/tests/test_integration.py +0 -0
- {funkycache-1.0.3 → funkycache-1.0.4}/tests/test_manager.py +0 -0
|
@@ -8,7 +8,7 @@ a collection of arguments to the return value.
|
|
|
8
8
|
from abc import ABC, abstractmethod
|
|
9
9
|
import dataclasses
|
|
10
10
|
from datetime import datetime, timezone
|
|
11
|
-
|
|
11
|
+
import functools
|
|
12
12
|
import json
|
|
13
13
|
from pathlib import Path
|
|
14
14
|
import sqlite3
|
|
@@ -299,19 +299,83 @@ class SQLiteCacheStorage[V](TypedCacheStorage[V]):
|
|
|
299
299
|
raise KeyError(key)
|
|
300
300
|
|
|
301
301
|
|
|
302
|
-
def
|
|
302
|
+
def _encode_key_as_json(key: dict[str, Any]) -> str:
|
|
303
303
|
"""
|
|
304
|
-
|
|
304
|
+
Encode a set of arguments to a Python function as a JSON string.
|
|
305
|
+
|
|
306
|
+
This is a lossy transformation that is designed to get a reasonable
|
|
307
|
+
serialisation as JSON. It is possible for values of different types
|
|
308
|
+
to collide.
|
|
309
|
+
|
|
310
|
+
JSON encoding is a significant bottleneck, so we are trying to reduce
|
|
311
|
+
the JSON object to be as small as possible before encoding it.
|
|
312
|
+
|
|
313
|
+
This function must not mutate its input.
|
|
305
314
|
"""
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
return
|
|
315
|
+
reduced_key = {k: _process_value(v) for k, v in key.items()}
|
|
316
|
+
|
|
317
|
+
try:
|
|
318
|
+
return json.dumps(
|
|
319
|
+
reduced_key, sort_keys=True, default=str, separators=(",", ":")
|
|
320
|
+
)
|
|
321
|
+
except TypeError:
|
|
322
|
+
clean_key = _make_json_compatible(reduced_key)
|
|
323
|
+
return json.dumps(clean_key, sort_keys=True, default=str, separators=(",", ":"))
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
def _process_value(val: Any) -> Any:
|
|
327
|
+
"""
|
|
328
|
+
Prepare a single value for JSON encoding.
|
|
329
|
+
"""
|
|
330
|
+
# Primitive types: return directly and skip the isinstance() checks.
|
|
331
|
+
# Large strings should be hashed.
|
|
332
|
+
if val is None or type(val) in (str, int, float, bool):
|
|
333
|
+
if isinstance(val, str) and len(val) > 256:
|
|
334
|
+
return _hash(val)
|
|
335
|
+
return val
|
|
336
|
+
|
|
337
|
+
# Dictionaries: walk recursively, and omit keys for empty values.
|
|
338
|
+
elif isinstance(val, dict):
|
|
339
|
+
return {k: _process_value(v) for k, v in val.items() if v}
|
|
340
|
+
|
|
341
|
+
# Lists and tuples: walk recursively. If a list/tuple contains a list
|
|
342
|
+
# of dictionaries, extract the keys/values into separate lists to
|
|
343
|
+
# avoid repeating the keys in the JSON.
|
|
344
|
+
elif isinstance(val, (list, tuple)):
|
|
345
|
+
processed_items = [_process_value(v) for v in val]
|
|
346
|
+
|
|
347
|
+
if len(processed_items) <= 1:
|
|
348
|
+
return processed_items
|
|
349
|
+
|
|
350
|
+
if all(isinstance(item, dict) for item in processed_items):
|
|
351
|
+
all_keys = set().union(*(item.keys() for item in processed_items))
|
|
352
|
+
sorted_keys = sorted(all_keys)
|
|
353
|
+
|
|
354
|
+
values_matrix = [
|
|
355
|
+
[item.get(k) for k in sorted_keys] for item in processed_items
|
|
356
|
+
]
|
|
357
|
+
|
|
358
|
+
return {"_keys": sorted_keys, "_values": values_matrix}
|
|
359
|
+
|
|
360
|
+
return processed_items
|
|
361
|
+
|
|
362
|
+
# Pydantic models and dataclasses: convert to dicts.
|
|
363
|
+
#
|
|
364
|
+
# This comes after the isinstance(…, dict) check because every Pydantic
|
|
365
|
+
# value will hit this branch and the dict() branch, but some values can
|
|
366
|
+
# go directly into the dict() branch. This reduces isinstance and
|
|
367
|
+
# is_dataclass calls.
|
|
368
|
+
elif isinstance(val, BaseModel) or dataclasses.is_dataclass(val):
|
|
369
|
+
# print(type(val))
|
|
370
|
+
adapter = _get_type_adapter(type(val))
|
|
371
|
+
dumped = adapter.dump_python(val, mode="json")
|
|
372
|
+
return _process_value(dumped)
|
|
373
|
+
|
|
310
374
|
else:
|
|
311
|
-
return
|
|
375
|
+
return val
|
|
312
376
|
|
|
313
377
|
|
|
314
|
-
@cache
|
|
378
|
+
@functools.cache
|
|
315
379
|
def _get_type_adapter(target_type: Any) -> TypeAdapter:
|
|
316
380
|
"""
|
|
317
381
|
Return a TypeAdapter for the given Pydantic type.
|
|
@@ -322,53 +386,19 @@ def _get_type_adapter(target_type: Any) -> TypeAdapter:
|
|
|
322
386
|
return TypeAdapter(target_type)
|
|
323
387
|
|
|
324
388
|
|
|
325
|
-
def
|
|
326
|
-
"""
|
|
327
|
-
Return whether the value is a long string that should be hashed
|
|
328
|
-
before JSON-encoding the key.
|
|
329
|
-
"""
|
|
330
|
-
return isinstance(v, str) and len(v) > 512
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
def _encode_key_as_json(key: dict[str, Any]) -> str:
|
|
389
|
+
def _make_json_compatible(value: Any) -> Any:
|
|
334
390
|
"""
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
This is a lossy transformation that is designed to get a reasonable
|
|
338
|
-
serialisation as JSON. It is possible for values of different types
|
|
339
|
-
to collide.
|
|
340
|
-
|
|
341
|
-
This function must not mutate its input.
|
|
391
|
+
Recursively walk an arbitrary structure and prepare it for JSON encoding.
|
|
342
392
|
"""
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
elif isinstance(val, dict):
|
|
350
|
-
if any(_should_hash(v) for v in val.values()):
|
|
351
|
-
overrides[name] = {
|
|
352
|
-
k: _hash(v) if _should_hash(v) else v for k, v in val.items()
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
elif isinstance(val, BaseModel) or dataclasses.is_dataclass(val):
|
|
356
|
-
adapter = _get_type_adapter(type(val))
|
|
357
|
-
overrides[name] = {
|
|
358
|
-
k: _hash(v) if _should_hash(v) else v
|
|
359
|
-
for k, v in adapter.dump_python(val, mode="json").items()
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
reduced_key = {**key, **overrides}
|
|
363
|
-
|
|
364
|
-
try:
|
|
365
|
-
return json.dumps(reduced_key, sort_keys=True, default=str)
|
|
366
|
-
except TypeError:
|
|
367
|
-
clean_key = _make_json_compatible(reduced_key)
|
|
368
|
-
return json.dumps(clean_key, sort_keys=True, default=str)
|
|
393
|
+
if isinstance(value, dict):
|
|
394
|
+
return {str(k): _make_json_compatible(v) for k, v in value.items()}
|
|
395
|
+
elif isinstance(value, (list, tuple, set)):
|
|
396
|
+
return [_make_json_compatible(item) for item in value]
|
|
397
|
+
else:
|
|
398
|
+
return value
|
|
369
399
|
|
|
370
400
|
|
|
371
|
-
@cache
|
|
401
|
+
@functools.cache
|
|
372
402
|
def _hash(s: str) -> str:
|
|
373
403
|
"""
|
|
374
404
|
Hash a string to reduce it to a fixed size.
|
|
@@ -264,31 +264,48 @@ class Greeting:
|
|
|
264
264
|
|
|
265
265
|
|
|
266
266
|
KEY_ENCODING_TEST_CASES: list[ParameterSet] = [
|
|
267
|
-
pytest.param({"n": "one"}, '{"n":
|
|
268
|
-
pytest.param({"n": 1}, '{"n":
|
|
267
|
+
pytest.param({"n": "one"}, '{"n":"one"}', id="str"),
|
|
268
|
+
pytest.param({"n": 1}, '{"n":1}', id="int"),
|
|
269
|
+
pytest.param(
|
|
270
|
+
{"n": ["one", "uno", "eins"]}, '{"n":["one","uno","eins"]}', id="list"
|
|
271
|
+
),
|
|
272
|
+
pytest.param(
|
|
273
|
+
{"n": ("one", "uno", "eins")}, '{"n":["one","uno","eins"]}', id="tuple"
|
|
274
|
+
),
|
|
275
|
+
pytest.param({"n": []}, '{"n":[]}', id="empty_list"),
|
|
276
|
+
pytest.param({"n": [{"en": "one"}]}, '{"n":[{"en":"one"}]}', id="list_1"),
|
|
277
|
+
pytest.param(
|
|
278
|
+
{
|
|
279
|
+
"n": [
|
|
280
|
+
{"lang": "en", "val": "one"},
|
|
281
|
+
{"lang": "fr", "val": "un"},
|
|
282
|
+
{"lang": "de", "val": "eins"},
|
|
283
|
+
]
|
|
284
|
+
},
|
|
285
|
+
'{"n":{"_keys":["lang","val"],"_values":[["en","one"],["fr","un"],["de","eins"]]}}',
|
|
286
|
+
id="list_many",
|
|
287
|
+
),
|
|
269
288
|
pytest.param(
|
|
270
289
|
{"n": Shape(sides=4, colour="red")},
|
|
271
|
-
'{"n":
|
|
290
|
+
'{"n":{"colour":"red","sides":4}}',
|
|
272
291
|
id="pydantic",
|
|
273
292
|
),
|
|
274
293
|
pytest.param(
|
|
275
294
|
{"n": Greeting(name="alex", message="hello")},
|
|
276
|
-
'{"n":
|
|
295
|
+
'{"n":{"message":"hello","name":"alex"}}',
|
|
277
296
|
id="dataclass",
|
|
278
297
|
),
|
|
279
298
|
pytest.param(
|
|
280
299
|
{"long": "a" * 5000, "short": "a"},
|
|
281
|
-
'{"long":
|
|
300
|
+
'{"long":"xxh64:b2b67943d4803cd6","short":"a"}',
|
|
282
301
|
id="long_str",
|
|
283
302
|
),
|
|
284
303
|
pytest.param(
|
|
285
304
|
{"n": {"long": "a" * 5000, "short": "a"}},
|
|
286
|
-
'{"n":
|
|
305
|
+
'{"n":{"long":"xxh64:b2b67943d4803cd6","short":"a"}}',
|
|
287
306
|
id="long_str",
|
|
288
307
|
),
|
|
289
|
-
pytest.param(
|
|
290
|
-
{"n": {Path("1"): "one"}}, '{"n": {"1": "one"}}', id="nested_path_key"
|
|
291
|
-
),
|
|
308
|
+
pytest.param({"n": {Path("1"): "one"}}, '{"n":{"1":"one"}}', id="nested_path_key"),
|
|
292
309
|
]
|
|
293
310
|
|
|
294
311
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|