cachebox 5.0.4__cp313-cp313-musllinux_1_2_i686.whl → 5.1.0__cp313-cp313-musllinux_1_2_i686.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.
Potentially problematic release.
This version of cachebox might be problematic. Click here for more details.
- cachebox/_core.cpython-313-i386-linux-musl.so +0 -0
- cachebox/utils.py +74 -36
- {cachebox-5.0.4.dist-info → cachebox-5.1.0.dist-info}/METADATA +1 -1
- {cachebox-5.0.4.dist-info → cachebox-5.1.0.dist-info}/RECORD +6 -6
- {cachebox-5.0.4.dist-info → cachebox-5.1.0.dist-info}/WHEEL +0 -0
- {cachebox-5.0.4.dist-info → cachebox-5.1.0.dist-info}/licenses/LICENSE +0 -0
|
Binary file
|
cachebox/utils.py
CHANGED
|
@@ -267,13 +267,14 @@ EVENT_HIT = 2
|
|
|
267
267
|
|
|
268
268
|
def _cached_wrapper(
|
|
269
269
|
func,
|
|
270
|
-
cache: BaseCacheImpl,
|
|
270
|
+
cache: typing.Union[BaseCacheImpl, typing.Callable],
|
|
271
271
|
key_maker: typing.Callable[[tuple, dict], typing.Hashable],
|
|
272
272
|
clear_reuse: bool,
|
|
273
273
|
callback: typing.Optional[typing.Callable[[int, typing.Any, typing.Any], typing.Any]],
|
|
274
274
|
copy_level: int,
|
|
275
275
|
is_method: bool,
|
|
276
276
|
):
|
|
277
|
+
is_method = cache_is_function = inspect.isfunction(cache)
|
|
277
278
|
_key_maker = (lambda args, kwds: key_maker(args[1:], kwds)) if is_method else key_maker
|
|
278
279
|
|
|
279
280
|
hits = 0
|
|
@@ -287,11 +288,12 @@ def _cached_wrapper(
|
|
|
287
288
|
if kwds.pop("cachebox__ignore", False):
|
|
288
289
|
return func(*args, **kwds)
|
|
289
290
|
|
|
291
|
+
_cache = cache(args[0]) if cache_is_function else cache
|
|
290
292
|
key = _key_maker(args, kwds)
|
|
291
293
|
|
|
292
294
|
# try to get result from cache
|
|
293
295
|
try:
|
|
294
|
-
result =
|
|
296
|
+
result = _cache[key]
|
|
295
297
|
except KeyError:
|
|
296
298
|
pass
|
|
297
299
|
else:
|
|
@@ -310,7 +312,7 @@ def _cached_wrapper(
|
|
|
310
312
|
raise cached_error
|
|
311
313
|
|
|
312
314
|
try:
|
|
313
|
-
result =
|
|
315
|
+
result = _cache[key]
|
|
314
316
|
hits += 1
|
|
315
317
|
event = EVENT_HIT
|
|
316
318
|
except KeyError:
|
|
@@ -323,7 +325,7 @@ def _cached_wrapper(
|
|
|
323
325
|
raise e
|
|
324
326
|
|
|
325
327
|
else:
|
|
326
|
-
|
|
328
|
+
_cache[key] = result
|
|
327
329
|
misses += 1
|
|
328
330
|
event = EVENT_MISS
|
|
329
331
|
|
|
@@ -332,34 +334,39 @@ def _cached_wrapper(
|
|
|
332
334
|
|
|
333
335
|
return _copy_if_need(result, level=copy_level)
|
|
334
336
|
|
|
335
|
-
|
|
337
|
+
if not cache_is_function:
|
|
338
|
+
_wrapped.cache = cache
|
|
339
|
+
_wrapped.cache_info = lambda: CacheInfo(
|
|
340
|
+
hits, misses, cache.maxsize, len(cache), cache.capacity()
|
|
341
|
+
)
|
|
342
|
+
|
|
336
343
|
_wrapped.callback = callback
|
|
337
|
-
_wrapped.cache_info = lambda: CacheInfo(
|
|
338
|
-
hits, misses, cache.maxsize, len(cache), cache.capacity()
|
|
339
|
-
)
|
|
340
344
|
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
345
|
+
if not cache_is_function:
|
|
346
|
+
|
|
347
|
+
def cache_clear() -> None:
|
|
348
|
+
nonlocal misses, hits, locks, exceptions
|
|
349
|
+
cache.clear(reuse=clear_reuse)
|
|
350
|
+
misses = 0
|
|
351
|
+
hits = 0
|
|
352
|
+
locks.clear()
|
|
353
|
+
exceptions.clear()
|
|
348
354
|
|
|
349
|
-
|
|
355
|
+
_wrapped.cache_clear = cache_clear
|
|
350
356
|
|
|
351
357
|
return _wrapped
|
|
352
358
|
|
|
353
359
|
|
|
354
360
|
def _async_cached_wrapper(
|
|
355
361
|
func,
|
|
356
|
-
cache: BaseCacheImpl,
|
|
362
|
+
cache: typing.Union[BaseCacheImpl, typing.Callable],
|
|
357
363
|
key_maker: typing.Callable[[tuple, dict], typing.Hashable],
|
|
358
364
|
clear_reuse: bool,
|
|
359
365
|
callback: typing.Optional[typing.Callable[[int, typing.Any, typing.Any], typing.Any]],
|
|
360
366
|
copy_level: int,
|
|
361
367
|
is_method: bool,
|
|
362
368
|
):
|
|
369
|
+
is_method = cache_is_function = inspect.isfunction(cache)
|
|
363
370
|
_key_maker = (lambda args, kwds: key_maker(args[1:], kwds)) if is_method else key_maker
|
|
364
371
|
|
|
365
372
|
hits = 0
|
|
@@ -375,11 +382,12 @@ def _async_cached_wrapper(
|
|
|
375
382
|
if kwds.pop("cachebox__ignore", False):
|
|
376
383
|
return await func(*args, **kwds)
|
|
377
384
|
|
|
385
|
+
_cache = cache(args[0]) if cache_is_function else cache
|
|
378
386
|
key = _key_maker(args, kwds)
|
|
379
387
|
|
|
380
388
|
# try to get result from cache
|
|
381
389
|
try:
|
|
382
|
-
result =
|
|
390
|
+
result = _cache[key]
|
|
383
391
|
except KeyError:
|
|
384
392
|
pass
|
|
385
393
|
else:
|
|
@@ -400,7 +408,7 @@ def _async_cached_wrapper(
|
|
|
400
408
|
raise cached_error
|
|
401
409
|
|
|
402
410
|
try:
|
|
403
|
-
result =
|
|
411
|
+
result = _cache[key]
|
|
404
412
|
hits += 1
|
|
405
413
|
event = EVENT_HIT
|
|
406
414
|
except KeyError:
|
|
@@ -413,7 +421,7 @@ def _async_cached_wrapper(
|
|
|
413
421
|
raise e
|
|
414
422
|
|
|
415
423
|
else:
|
|
416
|
-
|
|
424
|
+
_cache[key] = result
|
|
417
425
|
misses += 1
|
|
418
426
|
event = EVENT_MISS
|
|
419
427
|
|
|
@@ -424,21 +432,25 @@ def _async_cached_wrapper(
|
|
|
424
432
|
|
|
425
433
|
return _copy_if_need(result, level=copy_level)
|
|
426
434
|
|
|
427
|
-
|
|
435
|
+
if not cache_is_function:
|
|
436
|
+
_wrapped.cache = cache
|
|
437
|
+
_wrapped.cache_info = lambda: CacheInfo(
|
|
438
|
+
hits, misses, cache.maxsize, len(cache), cache.capacity()
|
|
439
|
+
)
|
|
440
|
+
|
|
428
441
|
_wrapped.callback = callback
|
|
429
|
-
_wrapped.cache_info = lambda: CacheInfo(
|
|
430
|
-
hits, misses, cache.maxsize, len(cache), cache.capacity()
|
|
431
|
-
)
|
|
432
442
|
|
|
433
|
-
|
|
434
|
-
nonlocal misses, hits, locks, exceptions
|
|
435
|
-
cache.clear(reuse=clear_reuse)
|
|
436
|
-
misses = 0
|
|
437
|
-
hits = 0
|
|
438
|
-
locks.clear()
|
|
439
|
-
exceptions.clear()
|
|
443
|
+
if not cache_is_function:
|
|
440
444
|
|
|
441
|
-
|
|
445
|
+
def cache_clear() -> None:
|
|
446
|
+
nonlocal misses, hits, locks, exceptions
|
|
447
|
+
cache.clear(reuse=clear_reuse)
|
|
448
|
+
misses = 0
|
|
449
|
+
hits = 0
|
|
450
|
+
locks.clear()
|
|
451
|
+
exceptions.clear()
|
|
452
|
+
|
|
453
|
+
_wrapped.cache_clear = cache_clear
|
|
442
454
|
|
|
443
455
|
return _wrapped
|
|
444
456
|
|
|
@@ -456,7 +468,8 @@ def cached(
|
|
|
456
468
|
Wraps a function to automatically cache and retrieve its results based on input parameters.
|
|
457
469
|
|
|
458
470
|
Args:
|
|
459
|
-
cache (BaseCacheImpl, dict,
|
|
471
|
+
cache (BaseCacheImpl, dict, callable): Cache implementation to store results. Defaults to FIFOCache.
|
|
472
|
+
Can be a function that got `self` and should return cache.
|
|
460
473
|
key_maker (Callable, optional): Function to generate cache keys from function arguments. Defaults to make_key.
|
|
461
474
|
clear_reuse (bool, optional): Whether to reuse cache during clearing. Defaults to False.
|
|
462
475
|
callback (Callable, optional): Function called on cache hit/miss events. Defaults to None.
|
|
@@ -465,7 +478,7 @@ def cached(
|
|
|
465
478
|
Returns:
|
|
466
479
|
Callable: Decorated function with caching capabilities.
|
|
467
480
|
|
|
468
|
-
Example::
|
|
481
|
+
Example for functions::
|
|
469
482
|
|
|
470
483
|
@cachebox.cached(cachebox.LRUCache(128))
|
|
471
484
|
def sum_as_string(a, b):
|
|
@@ -476,6 +489,20 @@ def cached(
|
|
|
476
489
|
assert len(sum_as_string.cache) == 1
|
|
477
490
|
sum_as_string.cache_clear()
|
|
478
491
|
assert len(sum_as_string.cache) == 0
|
|
492
|
+
|
|
493
|
+
Example for methods::
|
|
494
|
+
|
|
495
|
+
class A:
|
|
496
|
+
def __init__(self, num):
|
|
497
|
+
self.num = num
|
|
498
|
+
self._cache = cachebox.FIFOCache(0)
|
|
499
|
+
|
|
500
|
+
@cachebox.cached(lambda self: self._cache)
|
|
501
|
+
def method(self, n):
|
|
502
|
+
return self.num * n
|
|
503
|
+
|
|
504
|
+
instance = A(10)
|
|
505
|
+
assert A.method(2) == 20
|
|
479
506
|
"""
|
|
480
507
|
if cache is None:
|
|
481
508
|
cache = FIFOCache(0)
|
|
@@ -483,8 +510,8 @@ def cached(
|
|
|
483
510
|
if type(cache) is dict:
|
|
484
511
|
cache = FIFOCache(0, cache)
|
|
485
512
|
|
|
486
|
-
if not isinstance(cache, BaseCacheImpl):
|
|
487
|
-
raise TypeError("we expected cachebox caches, got %r" % (cache,))
|
|
513
|
+
if not isinstance(cache, BaseCacheImpl) and not inspect.isfunction(cache):
|
|
514
|
+
raise TypeError("we expected cachebox caches or function, got %r" % (cache,))
|
|
488
515
|
|
|
489
516
|
def decorator(func: FT) -> FT:
|
|
490
517
|
if inspect.iscoroutinefunction(func):
|
|
@@ -509,6 +536,9 @@ def cachedmethod(
|
|
|
509
536
|
copy_level: int = 1,
|
|
510
537
|
) -> typing.Callable[[FT], FT]:
|
|
511
538
|
"""
|
|
539
|
+
**This function is deperecated due to issue [#35](https://github.com/awolverp/cachebox/issues/35)**.
|
|
540
|
+
Use `cached` method instead.
|
|
541
|
+
|
|
512
542
|
Decorator to create a method-specific memoized cache for function results.
|
|
513
543
|
|
|
514
544
|
Similar to `cached()`, but ignores `self` parameter when generating cache keys.
|
|
@@ -523,6 +553,14 @@ def cachedmethod(
|
|
|
523
553
|
Returns:
|
|
524
554
|
Callable: Decorated method with method-specific caching capabilities.
|
|
525
555
|
"""
|
|
556
|
+
import warnings
|
|
557
|
+
|
|
558
|
+
warnings.warn(
|
|
559
|
+
"cachedmethod is deprecated, use cached instead. see issue https://github.com/awolverp/cachebox/issues/35",
|
|
560
|
+
DeprecationWarning,
|
|
561
|
+
stacklevel=2,
|
|
562
|
+
)
|
|
563
|
+
|
|
526
564
|
if cache is None:
|
|
527
565
|
cache = FIFOCache(0)
|
|
528
566
|
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
cachebox-5.0.
|
|
2
|
-
cachebox-5.0.
|
|
3
|
-
cachebox-5.0.
|
|
1
|
+
cachebox-5.1.0.dist-info/METADATA,sha256=APyzN3otl7JZGklA6mcBETronY87eKlRyHHRbhWyFsE,27190
|
|
2
|
+
cachebox-5.1.0.dist-info/WHEEL,sha256=hnEjxG8MMZmmlhJat41pJlHVa7PMz9kEhUjTj-jdJr4,105
|
|
3
|
+
cachebox-5.1.0.dist-info/licenses/LICENSE,sha256=4oH8nU-rNZ9h5tbh6MoPVPFgtYDqsPXlAr2QnhnxQ4Q,1064
|
|
4
4
|
cachebox.libs/libgcc_s-27e5a392.so.1,sha256=x5sO63liVwXxrjGGP371wB0RyQe1KEnIynYm82T0G0M,449745
|
|
5
5
|
cachebox/__init__.py,sha256=I93y-hLQKXsEIlmd0KAZ6CVfDqKqcWFvyiJdDsMUcvI,647
|
|
6
6
|
cachebox/_cachebox.py,sha256=hCZLh1DRMHvD5Crqcrsffy0pFRmc9fToTxHmUJDFK5Q,70522
|
|
7
|
-
cachebox/_core.cpython-313-i386-linux-musl.so,sha256=
|
|
7
|
+
cachebox/_core.cpython-313-i386-linux-musl.so,sha256=jWLNWMHU43OeB70BbFN31tDASU_Hytn6zn8AuhaZe2s,793425
|
|
8
8
|
cachebox/_core.pyi,sha256=qVl1rXUfzAzxy5FrH35Bv7RTp4gvXbaoZ-oMTDe4zTY,2842
|
|
9
9
|
cachebox/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
10
|
-
cachebox/utils.py,sha256=
|
|
11
|
-
cachebox-5.0.
|
|
10
|
+
cachebox/utils.py,sha256=Lshu3ZMoEXSB535Ucxmo09EV5eFy24uJMf2cRWhcl-U,18211
|
|
11
|
+
cachebox-5.1.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|