instance-cache


Nameinstance-cache JSON
Version 0.1.0 PyPI version JSON
download
home_pageNone
Summary为类方法提供实例级的结果缓存, 不影响类实例正常垃圾回收的装饰器
upload_time2025-08-09 18:35:03
maintainerNone
docs_urlNone
authorNone
requires_python>=3.7.0
licenseMIT License Copyright (c) [2025] [Liu Wei] Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords instance_cache cache
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # instance_cache

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


---

## 0. 背景

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

```pycon
>>> 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. 安装

使用以下命令安装该库

```commandline
pip install instance_cache
```

--- 

## 2. 使用

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

```pycon
>>> 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!
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "instance-cache",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7.0",
    "maintainer_email": "Liu Wei <23S112099@stu.hit.edu.cn>",
    "keywords": "instance_cache, cache",
    "author": null,
    "author_email": "Liu Wei <23S112099@stu.hit.edu.cn>",
    "download_url": "https://files.pythonhosted.org/packages/86/10/6f74253c9185543f4aa35e148b678a8bd4742f4fcb83c28a457e3538b057/instance_cache-0.1.0.tar.gz",
    "platform": null,
    "description": "# instance_cache\r\n\r\n\u4e3a\u7c7b\u65b9\u6cd5\u63d0\u4f9b\u5b9e\u4f8b\u7ea7\u7684\u7ed3\u679c\u7f13\u5b58, \u4e0d\u5f71\u54cd\u7c7b\u5b9e\u4f8b\u6b63\u5e38\u5783\u573e\u56de\u6536\u7684\u88c5\u9970\u5668\r\n\r\n\r\n---\r\n\r\n## 0. \u80cc\u666f\r\n\r\nfunctools.lru_cache \u548c cache \u51fd\u6570\u4f1a\u4fdd\u7559\u5bf9\u8c03\u7528\u53c2\u6570\u7684\u5f3a\u5f15\u7528, \u4f1a\u5f71\u54cd\u8fd9\u4e9b\u53c2\u6570\u6b63\u5e38\u7684\u5783\u573e\u56de\u6536, \u9700\u8981\u7b49\u5f85\u7f13\u5b58 \\\r\n\u8d85\u8fc7 max_size \u540e\u5f39\u51fa\u6216\u624b\u52a8\u8c03\u7528 cache_clear, \u6bd4\u8f83\u9ebb\u70e6. \\\r\n\u6700\u5e38\u89c1\u7684\u573a\u666f\u662f\u4f5c\u7528\u5728\u4e00\u822c\u7684\u7c7b\u65b9\u6cd5\u4e0a, \u4fdd\u7559\u53c2\u6570 self \u7684\u5f15\u7528\u540e\u4f1a\u5f71\u54cd\u6574\u4e2a\u7c7b\u5b9e\u4f8b\u7684\u5783\u573e\u56de\u6536\r\n\r\n```pycon\r\n>>> from functools import cache\r\n>>>\r\n>>> class Test:\r\n...     def __del__(self):\r\n...         print('delete!')\r\n...     def method(self):\r\n...         ...\r\n...     @cache\r\n...     def method_cache(self):\r\n...         ...\r\n...\r\n>>> Test().method()\r\ndelete!\r\n>>> Test().method()\r\ndelete!\r\n>>> Test().method_cache()  # \u65e0\u6cd5\u8fdb\u884c\u5783\u573e\u56de\u6536\r\n>>> Test().method_cache()\r\n>>> Test().method_cache()\r\n>>> Test.method_cache.cache_clear()  # \u9700\u624b\u52a8\u8c03\u7528, \u4e00\u6b21\u6027\u5220\u9664\u6240\u6709\u5b9e\u4f8b\u7684\u7f13\u5b58 (\u5373\u4f7f\u8fd8\u6709\u5176\u4ed6\u5b9e\u4f8b\u5904\u4e8e\u6b63\u5e38\u751f\u547d\u5468\u671f\u5185)\r\ndelete!\r\ndelete!\r\ndelete!\r\n```\r\n\r\n\u6b64\u5904\u63d0\u4f9b\u4e00\u4e2a\u4e00\u822c\u7c7b\u65b9\u6cd5\u7684\u7ed3\u679c\u7f13\u5b58\u88c5\u9970\u5668, \u63d0\u4f9b\u5b9e\u4f8b\u7ea7\u522b\u7684\u7f13\u5b58 (\u4e3a\u6bcf\u4e2a\u5b9e\u4f8b\u5355\u72ec\u521b\u5efa\u7f13\u5b58\u7a7a\u95f4).\r\n\u901a\u8fc7\u5c06\u7f13\u5b58\u5185\u5bb9\u4f5c\u4e3a\u6bcf\u4e2a\u7c7b\u5b9e\u4f8b\u7684\u5c5e\u6027\u8fdb\u884c\u5b58\u50a8 (\u7c7b\u4f3c\u4e8e functools.cached_property), \u907f\u514d\u4e86\u5f71\u54cd\u7c7b\u5b9e\u4f8b self \u7684\u6b63\u5e38\u5783\u573e\u56de\u6536.\r\n\u5bf9\u4e8e\u5176\u4ed6\u8c03\u7528\u53c2\u6570, \u5f53\u7c7b\u5b9e\u4f8b\u88ab\u56de\u6536\u540e\u4e5f\u4f1a\u6b63\u5e38\u56de\u6536\r\n\r\n---\r\n\r\n## 1. \u5b89\u88c5\r\n\r\n\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u8be5\u5e93\r\n\r\n```commandline\r\npip install instance_cache\r\n```\r\n\r\n--- \r\n\r\n## 2. \u4f7f\u7528\r\n\r\n\u4f7f\u7528\u65b9\u6cd5\u975e\u5e38\u7b80\u5355, \u4e0e functools.lru_cache \u57fa\u672c\u4e00\u81f4\r\n\r\n```pycon\r\n>>> from instance_cache import instance_cache\r\n>>>\r\n>>> class Test:\r\n...     @instance_cache(cache_name='method_cached')\r\n...     def method(self, x=1, y=2):\r\n...         print('run')\r\n...         ...  # \u8017\u65f6\u64cd\u4f5c\r\n...         return 1\r\n...     def __del__(self):\r\n...         print('delete!')\r\n...\r\n>>> foo = Test()\r\n>>> foo.method(1, 2)\r\nrun\r\n1\r\n>>> foo.method(1, 2)  # \u547d\u4e2d\u7f13\u5b58, \u4e0d\u8fd0\u884c\u65b9\u6cd5\u76f4\u63a5\u8fd4\u56de\u7ed3\u679c\r\n1\r\n>>> foo.method_cached.clear()  # \u6e05\u7a7a\u5b9e\u4f8b\u7684\u7ed3\u679c\u7f13\u5b58\r\n>>> del foo  # \u4f1a\u7acb\u523b\u8fdb\u884c\u5783\u573e\u56de\u6536\r\ndelete!\r\n```\r\n",
    "bugtrack_url": null,
    "license": "MIT License\r\n        \r\n        Copyright (c) [2025] [Liu Wei]\r\n        \r\n        Permission is hereby granted, free of charge, to any person obtaining a copy\r\n        of this software and associated documentation files (the \"Software\"), to deal\r\n        in the Software without restriction, including without limitation the rights\r\n        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r\n        copies of the Software, and to permit persons to whom the Software is\r\n        furnished to do so, subject to the following conditions:\r\n        \r\n        The above copyright notice and this permission notice shall be included in all\r\n        copies or substantial portions of the Software.\r\n        \r\n        THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r\n        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r\n        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r\n        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r\n        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r\n        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r\n        SOFTWARE.\r\n        ",
    "summary": "\u4e3a\u7c7b\u65b9\u6cd5\u63d0\u4f9b\u5b9e\u4f8b\u7ea7\u7684\u7ed3\u679c\u7f13\u5b58, \u4e0d\u5f71\u54cd\u7c7b\u5b9e\u4f8b\u6b63\u5e38\u5783\u573e\u56de\u6536\u7684\u88c5\u9970\u5668",
    "version": "0.1.0",
    "project_urls": {
        "Homepage": "https://github.com/AomandeNiuma/instance_cache"
    },
    "split_keywords": [
        "instance_cache",
        " cache"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "4254b5561569d41149c5f3d75aefe09b80bee80775d64a30a8d2bd553f34b9fb",
                "md5": "8b84546cb28a4d1133c8830c51e80117",
                "sha256": "e1729efd83901527b4a87966351057bd4aed6bc4aa16a29ed9d0efe9e97bd032"
            },
            "downloads": -1,
            "filename": "instance_cache-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8b84546cb28a4d1133c8830c51e80117",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7.0",
            "size": 6317,
            "upload_time": "2025-08-09T18:35:01",
            "upload_time_iso_8601": "2025-08-09T18:35:01.758673Z",
            "url": "https://files.pythonhosted.org/packages/42/54/b5561569d41149c5f3d75aefe09b80bee80775d64a30a8d2bd553f34b9fb/instance_cache-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "86106f74253c9185543f4aa35e148b678a8bd4742f4fcb83c28a457e3538b057",
                "md5": "21533fa6f1e2e7a4146cd42f9e7225b3",
                "sha256": "08bf911ec79fdc70c03b3702e352f8cd3137b52f447ebd6620dd28d279b2323f"
            },
            "downloads": -1,
            "filename": "instance_cache-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "21533fa6f1e2e7a4146cd42f9e7225b3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7.0",
            "size": 4794,
            "upload_time": "2025-08-09T18:35:03",
            "upload_time_iso_8601": "2025-08-09T18:35:03.250562Z",
            "url": "https://files.pythonhosted.org/packages/86/10/6f74253c9185543f4aa35e148b678a8bd4742f4fcb83c28a457e3538b057/instance_cache-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-09 18:35:03",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "AomandeNiuma",
    "github_project": "instance_cache",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "instance-cache"
}
        
Elapsed time: 1.84039s