nlbone 0.7.10__py3-none-any.whl → 0.7.11__py3-none-any.whl

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.
nlbone/utils/cache.py CHANGED
@@ -1,10 +1,12 @@
1
1
  import asyncio
2
2
  import inspect
3
3
  import json
4
- from typing import Any, Callable, Iterable, Optional
4
+ from typing import Any, Callable, Iterable, Optional, Mapping
5
5
 
6
6
  from makefun import wraps as mf_wraps
7
7
 
8
+ from nlbone.interfaces.api.additional_filed import AdditionalFieldsRequest
9
+ from nlbone.interfaces.api.pagination import PaginateRequest
8
10
  from nlbone.utils.cache_registry import get_cache
9
11
 
10
12
  try:
@@ -25,11 +27,42 @@ def _bind(func: Callable, args, kwargs):
25
27
  return bound
26
28
 
27
29
 
30
+ PRIMITIVES = (str, int, float, bool, type(None))
31
+
32
+
33
+ def to_jsonable(obj):
34
+ if isinstance(obj, PRIMITIVES):
35
+ return obj
36
+
37
+ if isinstance(obj, dict):
38
+ return {str(k): to_jsonable(v) for k, v in obj.items()}
39
+
40
+ if isinstance(obj, (list, tuple, set)):
41
+ return [to_jsonable(v) for v in obj]
42
+
43
+ if isinstance(obj, PaginateRequest):
44
+ return {
45
+ "limit": obj.limit,
46
+ "offset": obj.offset,
47
+ "sort": obj.sort,
48
+ "filters": obj.filters,
49
+ "include_ids": obj.include_ids,
50
+ }
51
+
52
+ if isinstance(obj, AdditionalFieldsRequest):
53
+ return {
54
+ "fields": obj.fields,
55
+ "bundles": obj.bundles,
56
+ }
57
+
58
+ return f"<{obj.__class__.__name__}>"
59
+
60
+
28
61
  def _key_from_template(
29
- tpl: Optional[str],
30
- func: Callable,
31
- args,
32
- kwargs,
62
+ tpl: Optional[str],
63
+ func: Callable,
64
+ args,
65
+ kwargs,
33
66
  ) -> str:
34
67
  """Format key template with bound arguments or build a stable default."""
35
68
  bound = _bind(func, args, kwargs)
@@ -37,15 +70,22 @@ def _key_from_template(
37
70
  return tpl.format(**bound.arguments)
38
71
 
39
72
  # Default stable key: module:qualname:sha of args
40
- payload = json.dumps(bound.arguments, sort_keys=True, default=str)
73
+
74
+ clean_args = to_jsonable(bound.arguments)
75
+ payload = json.dumps(clean_args, sort_keys=True)
76
+
77
+ payload = json.dumps(
78
+ payload,
79
+ sort_keys=True,
80
+ )
41
81
  return f"{func.__module__}:{func.__qualname__}:{hash(payload)}"
42
82
 
43
83
 
44
84
  def _format_tags(
45
- tag_tpls: Optional[Iterable[str]],
46
- func: Callable,
47
- args,
48
- kwargs,
85
+ tag_tpls: Optional[Iterable[str]],
86
+ func: Callable,
87
+ args,
88
+ kwargs,
49
89
  ) -> list[str] | None:
50
90
  if not tag_tpls:
51
91
  return None
@@ -84,13 +124,13 @@ def _run_maybe_async(func: Callable, *args, **kwargs):
84
124
 
85
125
 
86
126
  def cached(
87
- *,
88
- ttl: int,
89
- key: str | None = None,
90
- tags: Iterable[str] | None = None,
91
- serializer: Callable[[Any], bytes] = default_serialize,
92
- deserializer: Callable[[bytes], Any] = default_deserialize,
93
- cache_resolver: Optional[Callable[[], Any]] = None,
127
+ *,
128
+ ttl: int,
129
+ key: str | None = None,
130
+ tags: Iterable[str] | None = None,
131
+ serializer: Callable[[Any], bytes] = default_serialize,
132
+ deserializer: Callable[[bytes], Any] = default_deserialize,
133
+ cache_resolver: Optional[Callable[[], Any]] = None,
94
134
  ):
95
135
  """
96
136
  Framework-agnostic caching for SYNC or ASYNC callables.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nlbone
3
- Version: 0.7.10
3
+ Version: 0.7.11
4
4
  Summary: Backbone package for interfaces and infrastructure in Python projects
5
5
  Author-email: Amir Hosein Kahkbazzadeh <a.khakbazzadeh@gmail.com>
6
6
  License: MIT
@@ -98,7 +98,7 @@ nlbone/interfaces/jobs/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3
98
98
  nlbone/interfaces/jobs/dispatch_outbox.py,sha256=yLZSC3nvkgxT2LL4Pq_DYzCyf_tZB-FknrjjgN89GFg,809
99
99
  nlbone/interfaces/jobs/sync_tokens.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
100
100
  nlbone/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
101
- nlbone/utils/cache.py,sha256=hVfkR62o5vllDrE_nY4At10wK0It6qpZ45K1xoj10cQ,5931
101
+ nlbone/utils/cache.py,sha256=HyoVIJ9WDSQeSROgCoxY96-NUTajl3A1L2fhxRa1DN0,6988
102
102
  nlbone/utils/cache_keys.py,sha256=Y2YSellHTbUOcoaNbl1jaD4r485VU_e4KXsfBWhYTBo,1075
103
103
  nlbone/utils/cache_registry.py,sha256=w28sEfUQZAhzCCqVH5TflWQY3nyDXyEcFWt8hkuHRHw,823
104
104
  nlbone/utils/context.py,sha256=MmclJ24BG2uvSTg1IK7J-Da9BhVFDQ5ag4Ggs2FF1_w,1600
@@ -107,8 +107,8 @@ nlbone/utils/http.py,sha256=MPDEyaC16AKsL0YH6sWCPp8NC2TgzEHpWERYK5HcaYQ,1001
107
107
  nlbone/utils/normalize_mobile.py,sha256=sGH4tV9gX-6eVKozviNWJhm1DN1J28Nj-ERldCYkS_E,732
108
108
  nlbone/utils/redactor.py,sha256=-V4HrHmHwPi3Kez587Ek1uJlgK35qGSrwBOvcbw8Jas,1279
109
109
  nlbone/utils/time.py,sha256=DjjyQ9GLsfXoT6NK8RDW2rOlJg3e6sF04Jw6PBUrSvg,1268
110
- nlbone-0.7.10.dist-info/METADATA,sha256=PKHtP5UntSl8ErQ4WCmFNLVwwpHJxdJHvklfg0IZork,2295
111
- nlbone-0.7.10.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
112
- nlbone-0.7.10.dist-info/entry_points.txt,sha256=CpIL45t5nbhl1dGQPhfIIDfqqak3teK0SxPGBBr7YCk,59
113
- nlbone-0.7.10.dist-info/licenses/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
114
- nlbone-0.7.10.dist-info/RECORD,,
110
+ nlbone-0.7.11.dist-info/METADATA,sha256=xaLQVPgxrifFVdB1r-VlgQicLswzLwPBiCubMxTZimo,2295
111
+ nlbone-0.7.11.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
112
+ nlbone-0.7.11.dist-info/entry_points.txt,sha256=CpIL45t5nbhl1dGQPhfIIDfqqak3teK0SxPGBBr7YCk,59
113
+ nlbone-0.7.11.dist-info/licenses/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
114
+ nlbone-0.7.11.dist-info/RECORD,,