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 __del__(self):
...         print('delete!')
...     def method(self):
...         ...
...     @cache
...     def method_cache(self):
...         ...
...
>>> Test().method()
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(cache_name='method_cached')
...     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
>>> # foo.method_cached.clear()  # 清空实例的结果缓存
>>> 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.1.1.tar.gz (4.8 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.1.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: instance_cache-0.1.1.tar.gz
  • Upload date:
  • Size: 4.8 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.1.1.tar.gz
Algorithm Hash digest
SHA256 f0afa1fab0f824ad8915f9f9288ea7bc53e4fcbed1d2322e19be7283715b77c5
MD5 7f1d147ae54b241676a384928bc7b7f3
BLAKE2b-256 899d12b00aef14867c345e5c99bd007aa0aa299e4295a930d8dbd5225322d059

See more details on using hashes here.

File details

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

File metadata

  • Download URL: instance_cache-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.3 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.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 57efcf4b6b14e952a0abe88d92b2053459df6d1a68967aab846186e8870a1ddb
MD5 cf6127c3b051eb6bdd5ee928e86aaeed
BLAKE2b-256 be0e03666c5262907108495550b42f714f6119aa85dfb16f1aaa78a16c924347

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