Skip to main content

为类方法提供实例级的结果缓存, 不影响类实例正常垃圾回收的装饰器

Project description

instance_cache

为类方法提供实例级的结果缓存, 不影响类实例正常垃圾回收的装饰器


0. 背景

functools.lru_cache 和 cache 函数会保留对调用参数的强引用, 会影响这些参数正常的垃圾回收,
需要等待缓存超过 max_size 后弹出或手动调用 cache_clear, 比较麻烦.
最常见的场景是作用在一般的类方法上, 保留参数 self 的引用后会影响整个类实例的垃圾回收.

>>> from functools import cache
>>>
>>> class Test:
...     def method(self):
...         ...
...
...     @cache
...     def method_cache(self):
...         ...
...
...     def __del__(self):
...         print('delete!')
...
>>> Test().method()  # 正常垃圾回收
delete!
>>> Test().method_cache()  # 无法进行垃圾回收
>>> Test().method_cache()
>>> Test().method_cache()
>>> Test.method_cache.cache_clear()  # 手动清理缓存才会回收
delete!
delete!
delete!

此处提供一个一般类方法的结果缓存装饰器, 提供实例级别的缓存 (为每个实例单独创建缓存空间).
通过将缓存内容作为每个类实例的属性进行存储 (类似于 functools.cached_property), 避免影响类实例 self 的正常垃圾回收,
其他调用参数在类实例被回收后也会正常回收.


1. 安装

使用以下命令安装该库

pip install instance_cache

2. 使用

使用方法非常简单, 与 functools.lru_cache 基本一致

>>> from instance_cache import instance_cache
>>>
>>> class Test:
...     @instance_cache()
...     def method(self, x=1, y=2):
...         print('run')
...         ...  # 耗时操作
...         return 1
...
...     def __del__(self):
...         print('delete!')
...
>>> foo = Test()
>>> foo.method(1, 2)
run
1
>>> foo.method(1, 2)  # 命中缓存, 不运行方法直接返回结果
1
>>> Test.method.cache_info(foo)  # 查看实例的缓存信息
CacheInfo(hits=1, misses=1, maxsize=128, currsize=1)
>>> # Test.method.cache_clear(foo)  # 清空实例的缓存并重置缓存信息
>>> del foo  # 立刻进行垃圾回收
delete!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

instance_cache-0.2.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

instance_cache-0.2.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file instance_cache-0.2.0.tar.gz.

File metadata

  • Download URL: instance_cache-0.2.0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for instance_cache-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3b8f4022055908a6427f9e058f5ac48c142e993016944f761d1a0674db333dc4
MD5 0132d963f1148a337b81ff0a04f9b443
BLAKE2b-256 10a2efa3c992294f1a88525e0715ff3d46a2a94e4c99dc963e1edf95a7703ed6

See more details on using hashes here.

File details

Details for the file instance_cache-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: instance_cache-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for instance_cache-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 18bf69847a99e33fbe27c17f2e53ea3e1e74874cb185005fe2730674031578f2
MD5 a723c01f13e63e285f5c01ce887cd7d3
BLAKE2b-256 fad79a04a141dbe74cf1d9dc1fe29ff8c328a9a7c15422ebbd597daa4ad2d327

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page