funkycache 1.0.0__tar.gz → 1.0.1__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: funkycache
3
- Version: 1.0.0
3
+ Version: 1.0.1
4
4
  Summary: A SQLite-backed function cache that tracks file dependencies
5
5
  Author-email: Alex Chan <alex@alexwlchan.net>
6
6
  Maintainer-email: Alex Chan <alex@alexwlchan.net>
@@ -41,6 +41,7 @@ branch = true
41
41
  source = ["funkycache", "tests"]
42
42
  omit = [
43
43
  "tests/bench_sqlite_storage.py",
44
+ "tests/bench_key_encode_as_json.py",
44
45
  ]
45
46
 
46
47
  [tool.coverage.report]
@@ -6,7 +6,7 @@ a collection of arguments to the return value.
6
6
  """
7
7
 
8
8
  from abc import ABC, abstractmethod
9
- from collections.abc import Mapping
9
+ import dataclasses
10
10
  from datetime import datetime, timezone
11
11
  from functools import cache
12
12
  import json
@@ -15,7 +15,7 @@ import sqlite3
15
15
  from types import TracebackType
16
16
  from typing import Any, cast, Self
17
17
 
18
- from pydantic import TypeAdapter
18
+ from pydantic import BaseModel, TypeAdapter
19
19
  import xxhash
20
20
 
21
21
  from .version import __version__
@@ -34,7 +34,7 @@ class CannotOverwriteValueException(ValueError):
34
34
  cached function, and cannot be fixed by retrying.
35
35
  """
36
36
 
37
- def __init__(self, key: Mapping[str, Any]) -> None:
37
+ def __init__(self, key: dict[str, Any]) -> None:
38
38
  super().__init__(f"Key already exists: {key!r}")
39
39
 
40
40
 
@@ -45,7 +45,7 @@ class CacheStorage[V](ABC):
45
45
  """
46
46
 
47
47
  @abstractmethod
48
- def set(self, key: Mapping[str, Any], value: V) -> None:
48
+ def set(self, key: dict[str, Any], value: V) -> None:
49
49
  """
50
50
  Save a key-value pair in the cache.
51
51
 
@@ -54,7 +54,7 @@ class CacheStorage[V](ABC):
54
54
  """
55
55
 
56
56
  @abstractmethod
57
- def get(self, key: Mapping[str, Any]) -> V:
57
+ def get(self, key: dict[str, Any]) -> V:
58
58
  """
59
59
  Look up the value of a key in the cache.
60
60
 
@@ -73,7 +73,7 @@ class MemoryCacheStorage[V](CacheStorage[V]):
73
73
  def __init__(self):
74
74
  self._underlying: dict[str, V] = {}
75
75
 
76
- def set(self, key: Mapping[str, Any], value: V) -> None:
76
+ def set(self, key: dict[str, Any], value: V) -> None:
77
77
  """
78
78
  Save a key-value pair in the cache.
79
79
  """
@@ -85,7 +85,7 @@ class MemoryCacheStorage[V](CacheStorage[V]):
85
85
  except KeyError:
86
86
  self._underlying[enc_key] = value
87
87
 
88
- def get(self, key: Mapping[str, Any]) -> V:
88
+ def get(self, key: dict[str, Any]) -> V:
89
89
  """
90
90
  Look up the value of a key in the cache.
91
91
  """
@@ -105,7 +105,7 @@ class TypedCacheStorage[V](CacheStorage[V]):
105
105
  self.adapter = TypeAdapter(data_type)
106
106
 
107
107
  @abstractmethod
108
- def _set_str(self, key: Mapping[str, Any], value: str) -> None:
108
+ def _set_str(self, key: dict[str, Any], value: str) -> None:
109
109
  """
110
110
  Save a key-value pair in the cache, where the value is a string.
111
111
 
@@ -113,7 +113,7 @@ class TypedCacheStorage[V](CacheStorage[V]):
113
113
  a different value.
114
114
  """
115
115
 
116
- def set(self, key: Mapping[str, Any], value: V) -> None:
116
+ def set(self, key: dict[str, Any], value: V) -> None:
117
117
  """
118
118
  Save a key-value pair in the cache.
119
119
 
@@ -124,14 +124,14 @@ class TypedCacheStorage[V](CacheStorage[V]):
124
124
  self._set_str(key, json_str)
125
125
 
126
126
  @abstractmethod
127
- def _get_str(self, key: Mapping[str, Any]) -> str:
127
+ def _get_str(self, key: dict[str, Any]) -> str:
128
128
  """
129
129
  Look up the string value of a key in the cache.
130
130
 
131
131
  Throws a KeyError if you look up a key which isn't in the cache.
132
132
  """
133
133
 
134
- def get(self, key: Mapping[str, Any]) -> V:
134
+ def get(self, key: dict[str, Any]) -> V:
135
135
  """
136
136
  Look up the value of a key in the cache.
137
137
 
@@ -245,7 +245,7 @@ class SQLiteCacheStorage[V](TypedCacheStorage[V]):
245
245
  """
246
246
  self.close()
247
247
 
248
- def _encode_key(self, key: Mapping[str, Any]) -> str:
248
+ def _encode_key(self, key: dict[str, Any]) -> str:
249
249
  """
250
250
  Encode a mapping as a string key.
251
251
 
@@ -256,7 +256,7 @@ class SQLiteCacheStorage[V](TypedCacheStorage[V]):
256
256
  json_key = _encode_key_as_json(key)
257
257
  return _hash(json_key)
258
258
 
259
- def _set_str(self, key: Mapping[str, Any], value: str) -> None:
259
+ def _set_str(self, key: dict[str, Any], value: str) -> None:
260
260
  """
261
261
  Save a key-value pair in the cache, where the value is a string.
262
262
 
@@ -280,7 +280,7 @@ class SQLiteCacheStorage[V](TypedCacheStorage[V]):
280
280
  else:
281
281
  raise
282
282
 
283
- def _get_str(self, key: Mapping[str, Any]) -> str:
283
+ def _get_str(self, key: dict[str, Any]) -> str:
284
284
  """
285
285
  Look up the string value of a key in the cache.
286
286
 
@@ -311,7 +311,18 @@ def _make_json_compatible(value: Any) -> Any:
311
311
  return value
312
312
 
313
313
 
314
- def _encode_key_as_json(key: Mapping[str, Any]) -> str:
314
+ @cache
315
+ def _get_type_adapter(target_type: Any) -> TypeAdapter:
316
+ """
317
+ Return a TypeAdapter for the given Pydantic type.
318
+
319
+ The type adapter is cached, so schema generation only runs once
320
+ per unique type.
321
+ """
322
+ return TypeAdapter(target_type)
323
+
324
+
325
+ def _encode_key_as_json(key: dict[str, Any]) -> str:
315
326
  """
316
327
  Encode a set of arguments to a Python function as a JSON string.
317
328
 
@@ -319,8 +330,24 @@ def _encode_key_as_json(key: Mapping[str, Any]) -> str:
319
330
  serialisation as JSON. It is possible for values of different types
320
331
  to collide.
321
332
  """
322
- clean_key = _make_json_compatible(key)
323
- return json.dumps(clean_key, sort_keys=True, default=str)
333
+ for name, val in key.items():
334
+ if isinstance(val, str) and len(val) > 2048:
335
+ key[name] = _hash(val)
336
+
337
+ elif isinstance(val, dict):
338
+ for k, v in val.items():
339
+ if isinstance(v, str) and len(v) > 2048:
340
+ val[k] = _hash(v)
341
+
342
+ elif isinstance(val, BaseModel) or dataclasses.is_dataclass(val):
343
+ adapter = _get_type_adapter(type(val))
344
+ key[name] = adapter.dump_python(val, mode="json")
345
+
346
+ try:
347
+ return json.dumps(key, sort_keys=True, default=str)
348
+ except TypeError:
349
+ clean_key = _make_json_compatible(key)
350
+ return json.dumps(clean_key, sort_keys=True, default=str)
324
351
 
325
352
 
326
353
  @cache
@@ -2,4 +2,4 @@
2
2
  The FunkyCache version.
3
3
  """
4
4
 
5
- __version__ = "1.0.0"
5
+ __version__ = "1.0.1"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: funkycache
3
- Version: 1.0.0
3
+ Version: 1.0.1
4
4
  Summary: A SQLite-backed function cache that tracks file dependencies
5
5
  Author-email: Alex Chan <alex@alexwlchan.net>
6
6
  Maintainer-email: Alex Chan <alex@alexwlchan.net>
@@ -3,20 +3,24 @@ Tests for backend storage implementations.
3
3
  """
4
4
 
5
5
  from abc import ABC, abstractmethod
6
- from collections.abc import Generator, Mapping
6
+ from collections.abc import Generator
7
7
  from contextlib import closing, contextmanager
8
+ from dataclasses import dataclass
8
9
  from pathlib import Path
9
10
  import sqlite3
10
11
  from typing import Any
11
12
  from unittest.mock import MagicMock
12
13
 
14
+ from pydantic import BaseModel
13
15
  import pytest
16
+ from _pytest.mark.structures import ParameterSet
14
17
 
15
18
  from funkycache.storage import (
16
19
  CacheStorage,
17
20
  MemoryCacheStorage,
18
21
  SQLiteCacheStorage,
19
22
  CannotOverwriteValueException,
23
+ _encode_key_as_json,
20
24
  )
21
25
 
22
26
 
@@ -50,11 +54,13 @@ class CacheStorageTestSuite(ABC):
50
54
  # A dict with Path keys isn't serialisable by json.dumps(…),
51
55
  # even if you pass default=str.
52
56
  pytest.param({Path("n"): "numbers"}, id="path_key"),
53
- pytest.param([{Path("n"): "numbers"}], id="path_key_nested_in_list"),
57
+ pytest.param(
58
+ {"args": [{Path("n"): "numbers"}]}, id="path_key_nested_in_list"
59
+ ),
54
60
  pytest.param({"1": {Path("n"): "numbers"}}, id="path_key_nested_in_obj"),
55
61
  ],
56
62
  )
57
- def test_cache(self, key: Mapping[str, Any], tmp_path: Path) -> None:
63
+ def test_cache(self, key: dict[str, Any], tmp_path: Path) -> None:
58
64
  """
59
65
  Test the basic properties of a cache.
60
66
  """
@@ -224,3 +230,58 @@ class TestSQLiteCacheStorage(CacheStorageTestSuite):
224
230
 
225
231
  with pytest.raises(sqlite3.OperationalError, match="no such column: key"):
226
232
  SQLiteCacheStorage[int](db_path, data_type="int")
233
+
234
+
235
+ class Shape(BaseModel):
236
+ """A basic model that uses Pydantic."""
237
+
238
+ sides: int
239
+ colour: str
240
+
241
+
242
+ @dataclass
243
+ class Greeting:
244
+ """A basic model that uses dataclasses."""
245
+
246
+ name: str
247
+ message: str
248
+
249
+
250
+ KEY_ENCODING_TEST_CASES: list[ParameterSet] = [
251
+ pytest.param({"n": "one"}, '{"n": "one"}', id="str"),
252
+ pytest.param({"n": 1}, '{"n": 1}', id="int"),
253
+ pytest.param(
254
+ {"n": Shape(sides=4, colour="red")},
255
+ '{"n": {"colour": "red", "sides": 4}}',
256
+ id="pydantic",
257
+ ),
258
+ pytest.param(
259
+ {"n": Greeting(name="alex", message="hello")},
260
+ '{"n": {"message": "hello", "name": "alex"}}',
261
+ id="dataclass",
262
+ ),
263
+ pytest.param(
264
+ {"long": "a" * 5000, "short": "a"},
265
+ '{"long": "xxh64:b2b67943d4803cd6", "short": "a"}',
266
+ id="long_str",
267
+ ),
268
+ pytest.param(
269
+ {"n": {"long": "a" * 5000, "short": "a"}},
270
+ '{"n": {"long": "xxh64:b2b67943d4803cd6", "short": "a"}}',
271
+ id="long_str",
272
+ ),
273
+ pytest.param(
274
+ {"n": {Path("1"): "one"}}, '{"n": {"1": "one"}}', id="nested_path_key"
275
+ ),
276
+ ]
277
+
278
+
279
+ @pytest.mark.parametrize(
280
+ "key, json_key",
281
+ KEY_ENCODING_TEST_CASES,
282
+ )
283
+ def test_encode_key_as_json(key: dict[str, Any], json_key: str) -> None:
284
+ """
285
+ Tests for _encode_key_as_json.
286
+ """
287
+ assert _encode_key_as_json(key) == json_key
File without changes
File without changes
File without changes