cachebox 5.0.4__cp312-cp312-win32.whl → 5.1.0__cp312-cp312-win32.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.

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 = cache[key]
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 = cache[key]
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
- cache[key] = result
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
- _wrapped.cache = cache
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
- def cache_clear() -> None:
342
- nonlocal misses, hits, locks, exceptions
343
- cache.clear(reuse=clear_reuse)
344
- misses = 0
345
- hits = 0
346
- locks.clear()
347
- exceptions.clear()
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
- _wrapped.cache_clear = cache_clear
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 = cache[key]
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 = cache[key]
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
- cache[key] = result
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
- _wrapped.cache = cache
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
- def cache_clear() -> None:
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
- _wrapped.cache_clear = cache_clear
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, optional): Cache implementation to store results. Defaults to FIFOCache.
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,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cachebox
3
- Version: 5.0.4
3
+ Version: 5.1.0
4
4
  Classifier: Programming Language :: Python :: Implementation :: CPython
5
5
  Classifier: Programming Language :: Python :: Implementation :: PyPy
6
6
  Classifier: Programming Language :: Python :: 3
@@ -0,0 +1,10 @@
1
+ cachebox-5.1.0.dist-info/METADATA,sha256=jEwzecb0N34UOEajD0L6i1Rb4th5CS5rI5ZAQ7MCBlA,28039
2
+ cachebox-5.1.0.dist-info/WHEEL,sha256=wyHSmqqJ-qET2p6Rf3R6YLhn3ORV7Mu9gupuCQ3hHYM,92
3
+ cachebox-5.1.0.dist-info/licenses/LICENSE,sha256=PtaZzbHvAYrs6g0EqSW8ZSR3xjsVTZ251eU1lxOzeU8,1084
4
+ cachebox/__init__.py,sha256=L_-CrOIvkwg5QI9qg2Q7EAlrTN8x3y0GtgBmdFpxRWs,673
5
+ cachebox/_cachebox.py,sha256=Y3OjNge4q0ygWufAd_Zs-vUN79FEpfZ7lPoWMCrO4ts,72632
6
+ cachebox/_core.cp312-win32.pyd,sha256=PbjFXvHoJfMa2ZJRxjs-R5YeLMeecDAJsRhxnlHRZPo,513024
7
+ cachebox/_core.pyi,sha256=wo-6ajI9eE6b50Bx9ZhVZ_nFjgIqAPfJyacmjRRBZh0,2917
8
+ cachebox/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
+ cachebox/utils.py,sha256=AAo5MdtA0T_U_qCo6jVmdL07j6B-skUVksFdW5fhxfA,18803
10
+ cachebox-5.1.0.dist-info/RECORD,,
@@ -1,10 +0,0 @@
1
- cachebox-5.0.4.dist-info/METADATA,sha256=U_hmiO4GkbgfVNKGW5hnu_q67Gg4Qi_yvV5P1UfxDDw,28039
2
- cachebox-5.0.4.dist-info/WHEEL,sha256=wyHSmqqJ-qET2p6Rf3R6YLhn3ORV7Mu9gupuCQ3hHYM,92
3
- cachebox-5.0.4.dist-info/licenses/LICENSE,sha256=PtaZzbHvAYrs6g0EqSW8ZSR3xjsVTZ251eU1lxOzeU8,1084
4
- cachebox/__init__.py,sha256=L_-CrOIvkwg5QI9qg2Q7EAlrTN8x3y0GtgBmdFpxRWs,673
5
- cachebox/_cachebox.py,sha256=Y3OjNge4q0ygWufAd_Zs-vUN79FEpfZ7lPoWMCrO4ts,72632
6
- cachebox/_core.cp312-win32.pyd,sha256=zp6ckXRv2SJslB5Upft3oFfAOLwg6M4DxO0didPqhoI,513024
7
- cachebox/_core.pyi,sha256=wo-6ajI9eE6b50Bx9ZhVZ_nFjgIqAPfJyacmjRRBZh0,2917
8
- cachebox/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
- cachebox/utils.py,sha256=11CxHhZO2fYLYlqeZyfGrj8o75KA1L0vlhM2b3zH1wg,17358
10
- cachebox-5.0.4.dist-info/RECORD,,