Name | gqylpy-cache JSON |
Version |
1.3.4
JSON |
| download |
home_page | http://gqylpy.com |
Summary | 如其名,它实现缓存功能,可缓存某个函数或某个类中定义的所有方法的返回值。 |
upload_time | 2022-12-11 11:06:47 |
maintainer | |
docs_url | None |
author | 竹永康 |
requires_python | >=3.6, <4 |
license | Apache 2.0 |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
[<img alt="LOGO" src="http://www.gqylpy.com/static/img/favicon.ico" height="21" width="21"/>](http://www.gqylpy.com)
[![Release](https://img.shields.io/github/release/gqylpy/gqylpy-cache.svg?style=flat-square")](https://github.com/gqylpy/gqylpy-cache/releases/latest)
[![Python Versions](https://img.shields.io/pypi/pyversions/gqylpy_cache)](https://pypi.org/project/gqylpy_cache)
[![License](https://img.shields.io/pypi/l/gqylpy_cache)](https://github.com/gqylpy/gqylpy-cache/blob/master/LICENSE)
[![Downloads](https://pepy.tech/badge/gqylpy_cache/month)](https://pepy.tech/project/gqylpy_cache)
# gqylpy-cache
> 如其名,`gqylpy_cache` 实现缓存功能,由 GQYLPY 团队研发的一个框架,可缓存某个函数或某个类中定义的所有方法的返回值。
>
> > _你的程序中有一个函数会被多次调用,并且返回值不变,你会怎么做?为提高代码效率,你会先调用一次该函数并把返回值存到一个变量,之后就使用这个变量,而不是重复调用函数。是这样吗?你已经很不错了。但现在,我们要传授你一种比之更简明的方案,使用 `gqylpy_cache` 模块直接缓存函数返回值。_
>
> `gqylpy_cache` 有两种使用方式:当做元类使用,将缓存其元类实例中定义的所有方法的返回值;当做装饰器使用,将缓存被装饰函数的返回值。
<kbd>pip3 install gqylpy_cache</kbd>
###### 缓存类中方法的返回值
```python
import gqylpy_cache
class Alpha(metaclass=gqylpy_cache):
...
```
此时,类 `Alpha` 中定义的所有方法以及`property`属性,在被其实例调用一次后,返回值都将被缓存,缓存在 `__cache_pool__` 属性中。此后的每次调用,只要参数不变,都是直接从 `__cache_pool__` 中取值,不会重复执行相关代码,大幅减少程序功耗并提高代码可读性。
上述缓存功能默认只作用于单个实例,每个实例都有自己的 `__cache_pool__` 属性,若希望 `Alpha` 的所有实例共享同一份缓存,可启用 `__shared_instance_cache__` 属性:
```python
class Alpha(metaclass=gqylpy_cache):
__shared_instance_cache__ = True
```
设置类属性 `__shared_instance_cache__ = True` 后,属性 `__cache_pool__` 将被创建在 `Alpha` 类中,而不是 `Alpha` 的每个实例中。
若希望某个方法或`property`不被缓存,可将其加入到 `__not_cache__` 列表中:
```python
class Alpha(metaclass=gqylpy_cache):
__not_cache__ = [method_obj_or_method_name, ...]
```
另外,`Alpha` 的子类也拥有上述缓存功能。
###### 缓存函数返回值
```python
import gqylpy_cache
@gqylpy_cache
def alpha():
...
```
此时,函数 `alpha` 在被调用一次后,其返回值将被缓存。此后的每次调用,只要参数不变,都是直接从缓存中取值,而不会重复执行 `alpha` 函数。
装饰器的用法亦可获得单例类,只要实例化参数一致:
```python
@gqylpy_cache
class Alpha:
...
```
另外一种兼容编辑器语法提示的用法:
```python
from gqylpy_cache import cache
@cache
def alpha():
...
```
Raw data
{
"_id": null,
"home_page": "http://gqylpy.com",
"name": "gqylpy-cache",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6, <4",
"maintainer_email": "",
"keywords": "",
"author": "\u7af9\u6c38\u5eb7",
"author_email": "<gqylpy@outlook.com>",
"download_url": "https://files.pythonhosted.org/packages/19/b4/a4b11c8a9deb8ac32d896be9e626b55dd584c5893e11026a8e338d82500d/gqylpy_cache-1.3.4.tar.gz",
"platform": null,
"description": "[<img alt=\"LOGO\" src=\"http://www.gqylpy.com/static/img/favicon.ico\" height=\"21\" width=\"21\"/>](http://www.gqylpy.com)\n[![Release](https://img.shields.io/github/release/gqylpy/gqylpy-cache.svg?style=flat-square\")](https://github.com/gqylpy/gqylpy-cache/releases/latest)\n[![Python Versions](https://img.shields.io/pypi/pyversions/gqylpy_cache)](https://pypi.org/project/gqylpy_cache)\n[![License](https://img.shields.io/pypi/l/gqylpy_cache)](https://github.com/gqylpy/gqylpy-cache/blob/master/LICENSE)\n[![Downloads](https://pepy.tech/badge/gqylpy_cache/month)](https://pepy.tech/project/gqylpy_cache)\n\n# gqylpy-cache\n\n> \u5982\u5176\u540d\uff0c`gqylpy_cache` \u5b9e\u73b0\u7f13\u5b58\u529f\u80fd\uff0c\u7531 GQYLPY \u56e2\u961f\u7814\u53d1\u7684\u4e00\u4e2a\u6846\u67b6\uff0c\u53ef\u7f13\u5b58\u67d0\u4e2a\u51fd\u6570\u6216\u67d0\u4e2a\u7c7b\u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u3002\n> \n> > _\u4f60\u7684\u7a0b\u5e8f\u4e2d\u6709\u4e00\u4e2a\u51fd\u6570\u4f1a\u88ab\u591a\u6b21\u8c03\u7528\uff0c\u5e76\u4e14\u8fd4\u56de\u503c\u4e0d\u53d8\uff0c\u4f60\u4f1a\u600e\u4e48\u505a\uff1f\u4e3a\u63d0\u9ad8\u4ee3\u7801\u6548\u7387\uff0c\u4f60\u4f1a\u5148\u8c03\u7528\u4e00\u6b21\u8be5\u51fd\u6570\u5e76\u628a\u8fd4\u56de\u503c\u5b58\u5230\u4e00\u4e2a\u53d8\u91cf\uff0c\u4e4b\u540e\u5c31\u4f7f\u7528\u8fd9\u4e2a\u53d8\u91cf\uff0c\u800c\u4e0d\u662f\u91cd\u590d\u8c03\u7528\u51fd\u6570\u3002\u662f\u8fd9\u6837\u5417\uff1f\u4f60\u5df2\u7ecf\u5f88\u4e0d\u9519\u4e86\u3002\u4f46\u73b0\u5728\uff0c\u6211\u4eec\u8981\u4f20\u6388\u4f60\u4e00\u79cd\u6bd4\u4e4b\u66f4\u7b80\u660e\u7684\u65b9\u6848\uff0c\u4f7f\u7528 `gqylpy_cache` \u6a21\u5757\u76f4\u63a5\u7f13\u5b58\u51fd\u6570\u8fd4\u56de\u503c\u3002_\n> \n> `gqylpy_cache` \u6709\u4e24\u79cd\u4f7f\u7528\u65b9\u5f0f\uff1a\u5f53\u505a\u5143\u7c7b\u4f7f\u7528\uff0c\u5c06\u7f13\u5b58\u5176\u5143\u7c7b\u5b9e\u4f8b\u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\uff1b\u5f53\u505a\u88c5\u9970\u5668\u4f7f\u7528\uff0c\u5c06\u7f13\u5b58\u88ab\u88c5\u9970\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002\n\n<kbd>pip3 install gqylpy_cache</kbd>\n\n###### \u7f13\u5b58\u7c7b\u4e2d\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\n\n```python\nimport gqylpy_cache\n\nclass Alpha(metaclass=gqylpy_cache):\n ...\n```\n\u6b64\u65f6\uff0c\u7c7b `Alpha` \u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u65b9\u6cd5\u4ee5\u53ca`property`\u5c5e\u6027\uff0c\u5728\u88ab\u5176\u5b9e\u4f8b\u8c03\u7528\u4e00\u6b21\u540e\uff0c\u8fd4\u56de\u503c\u90fd\u5c06\u88ab\u7f13\u5b58\uff0c\u7f13\u5b58\u5728 `__cache_pool__` \u5c5e\u6027\u4e2d\u3002\u6b64\u540e\u7684\u6bcf\u6b21\u8c03\u7528\uff0c\u53ea\u8981\u53c2\u6570\u4e0d\u53d8\uff0c\u90fd\u662f\u76f4\u63a5\u4ece `__cache_pool__` \u4e2d\u53d6\u503c\uff0c\u4e0d\u4f1a\u91cd\u590d\u6267\u884c\u76f8\u5173\u4ee3\u7801\uff0c\u5927\u5e45\u51cf\u5c11\u7a0b\u5e8f\u529f\u8017\u5e76\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\u3002\n\n\u4e0a\u8ff0\u7f13\u5b58\u529f\u80fd\u9ed8\u8ba4\u53ea\u4f5c\u7528\u4e8e\u5355\u4e2a\u5b9e\u4f8b\uff0c\u6bcf\u4e2a\u5b9e\u4f8b\u90fd\u6709\u81ea\u5df1\u7684 `__cache_pool__` \u5c5e\u6027\uff0c\u82e5\u5e0c\u671b `Alpha` \u7684\u6240\u6709\u5b9e\u4f8b\u5171\u4eab\u540c\u4e00\u4efd\u7f13\u5b58\uff0c\u53ef\u542f\u7528 `__shared_instance_cache__` \u5c5e\u6027\uff1a\n```python\nclass Alpha(metaclass=gqylpy_cache):\n __shared_instance_cache__ = True\n```\n\u8bbe\u7f6e\u7c7b\u5c5e\u6027 `__shared_instance_cache__ = True` \u540e\uff0c\u5c5e\u6027 `__cache_pool__` \u5c06\u88ab\u521b\u5efa\u5728 `Alpha` \u7c7b\u4e2d\uff0c\u800c\u4e0d\u662f `Alpha` \u7684\u6bcf\u4e2a\u5b9e\u4f8b\u4e2d\u3002\n\n\u82e5\u5e0c\u671b\u67d0\u4e2a\u65b9\u6cd5\u6216`property`\u4e0d\u88ab\u7f13\u5b58\uff0c\u53ef\u5c06\u5176\u52a0\u5165\u5230 `__not_cache__` \u5217\u8868\u4e2d\uff1a\n\n```python\nclass Alpha(metaclass=gqylpy_cache):\n __not_cache__ = [method_obj_or_method_name, ...]\n```\n\u53e6\u5916\uff0c`Alpha` \u7684\u5b50\u7c7b\u4e5f\u62e5\u6709\u4e0a\u8ff0\u7f13\u5b58\u529f\u80fd\u3002\n\n###### \u7f13\u5b58\u51fd\u6570\u8fd4\u56de\u503c\n\n```python\nimport gqylpy_cache\n\n@gqylpy_cache\ndef alpha():\n ...\n```\n\u6b64\u65f6\uff0c\u51fd\u6570 `alpha` \u5728\u88ab\u8c03\u7528\u4e00\u6b21\u540e\uff0c\u5176\u8fd4\u56de\u503c\u5c06\u88ab\u7f13\u5b58\u3002\u6b64\u540e\u7684\u6bcf\u6b21\u8c03\u7528\uff0c\u53ea\u8981\u53c2\u6570\u4e0d\u53d8\uff0c\u90fd\u662f\u76f4\u63a5\u4ece\u7f13\u5b58\u4e2d\u53d6\u503c\uff0c\u800c\u4e0d\u4f1a\u91cd\u590d\u6267\u884c `alpha` \u51fd\u6570\u3002\n\n\u88c5\u9970\u5668\u7684\u7528\u6cd5\u4ea6\u53ef\u83b7\u5f97\u5355\u4f8b\u7c7b\uff0c\u53ea\u8981\u5b9e\u4f8b\u5316\u53c2\u6570\u4e00\u81f4\uff1a\n```python\n@gqylpy_cache\nclass Alpha:\n ...\n```\n\n\u53e6\u5916\u4e00\u79cd\u517c\u5bb9\u7f16\u8f91\u5668\u8bed\u6cd5\u63d0\u793a\u7684\u7528\u6cd5\uff1a\n```python\nfrom gqylpy_cache import cache\n\n@cache\ndef alpha():\n ...\n```\n",
"bugtrack_url": null,
"license": "Apache 2.0",
"summary": "\u5982\u5176\u540d\uff0c\u5b83\u5b9e\u73b0\u7f13\u5b58\u529f\u80fd\uff0c\u53ef\u7f13\u5b58\u67d0\u4e2a\u51fd\u6570\u6216\u67d0\u4e2a\u7c7b\u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u3002",
"version": "1.3.4",
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "a9898827170d43913e0987ca4ff8d2a6",
"sha256": "e3a3cf2feffc1818c5121aecd57add1dfd23a512b1fdb70ea77545f71d2fadf1"
},
"downloads": -1,
"filename": "gqylpy_cache-1.3.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "a9898827170d43913e0987ca4ff8d2a6",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6, <4",
"size": 11054,
"upload_time": "2022-12-11T11:06:45",
"upload_time_iso_8601": "2022-12-11T11:06:45.813549Z",
"url": "https://files.pythonhosted.org/packages/ef/f0/a8b8578cfd72c31d0e0e2189034135db8bd49203587753e48b0b9725c1e4/gqylpy_cache-1.3.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "f1cd35261e2d996477b84b3a4403f290",
"sha256": "89ec7128528810691777fdc399903bd94cb2943f3b966765276f5f713897af6f"
},
"downloads": -1,
"filename": "gqylpy_cache-1.3.4.tar.gz",
"has_sig": false,
"md5_digest": "f1cd35261e2d996477b84b3a4403f290",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6, <4",
"size": 10140,
"upload_time": "2022-12-11T11:06:47",
"upload_time_iso_8601": "2022-12-11T11:06:47.216891Z",
"url": "https://files.pythonhosted.org/packages/19/b4/a4b11c8a9deb8ac32d896be9e626b55dd584c5893e11026a8e338d82500d/gqylpy_cache-1.3.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-12-11 11:06:47",
"github": false,
"gitlab": false,
"bitbucket": false,
"lcname": "gqylpy-cache"
}