Name | funccache JSON |
Version |
2.0.1
JSON |
| download |
home_page | http://gqylpy.com |
Summary | True to its name, `funccache` implements the cache function. Cache the return value of a callable object or all methods defined in a class. |
upload_time | 2024-04-15 01:14:04 |
maintainer | None |
docs_url | None |
author | 竹永康 |
requires_python | >=3.8 |
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/funccache.svg?style=flat-square")](https://github.com/gqylpy/funccache/releases/latest)
[![Python Versions](https://img.shields.io/pypi/pyversions/funccache)](https://pypi.org/project/funccache)
[![License](https://img.shields.io/pypi/l/funccache)](https://github.com/gqylpy/funccache/blob/master/LICENSE)
[![Downloads](https://static.pepy.tech/badge/funccache)](https://pepy.tech/project/funccache)
# funccache
English | [中文](https://github.com/gqylpy/funccache/blob/master/README_CN.md)
`funccache`, as its name suggests, is a framework developed by the GQYLPY team that implements function caching. It can cache the return values of specific functions or all methods defined within a class.
> _What if you have a function in your program that gets called multiple times and always returns the same value? To improve code efficiency, you might call the function once, store the return value in a variable, and then use that variable instead of repeatedly calling the function. Sound familiar? That's great! But now, we're here to introduce a more elegant solution: using the `funccache` module to directly cache function return values._
`funccache` can be used in two ways: as a metaclass to cache the return values of all methods defined in its metaclass instances, or as a decorator to cache the return values of decorated functions.
<kbd>pip3 install funccache</kbd>
### Caching Return Values of Class Methods
```python
import funccache
class Alpha(metaclass=funccache):
...
```
In this case, all methods and `property` attributes defined in the `Alpha` class will have their return values cached in the `__cache_pool__` attribute after being called once by an instance of the class. Subsequent calls, as long as the parameters remain the same, will directly retrieve values from `__cache_pool__` without re-executing the related code, significantly reducing program overhead and improving code readability.
By default, this caching functionality only applies to individual instances, and each instance has its own `__cache_pool__` attribute. However, if you want all instances of `Alpha` to share the same cache, you can enable the `__shared_instance_cache__` attribute:
```python
class Alpha(metaclass=funccache):
__shared_instance_cache__ = True
```
Setting the class attribute `__shared_instance_cache__ = True` creates the `__cache_pool__` attribute in the `Alpha` class itself, rather than in each individual instance of `Alpha`.
The cache never expires by default, but you can set a time-to-live (TTL) for the cache using the class attribute `__ttl__`:
```python
class Alpha(metaclass=funccache):
__ttl__ = 60
```
If you want a specific method or `property` to not be cached, you can add it to the `__not_cache__` list:
```python
class Alpha(metaclass=funccache):
__not_cache__ = [method_obj_or_method_name, ...]
```
Additionally, subclasses of `Alpha` will also inherit the caching functionality.
### Caching Return Values of Functions
```python
import funccache
@funccache
def alpha():
...
```
In this case, the return value of the `alpha` function will be cached after it is called once. Subsequent calls, as long as the parameters remain the same, will directly retrieve the value from the cache without re-executing the `alpha` function.
The cache never expires by default, but if you want the cache to expire after a certain period, you can use `funccache.ttl`:
```python
@funccache.ttl(60)
def alpha():
...
```
You can even cache based on the number of calls using `funccache.count`:
```python
@funccache.count(3)
def alpha():
...
```
The decorator usage can also achieve singleton class behavior, as long as the instantiation parameters are consistent:
```python
@funccache
class Alpha:
...
```
Raw data
{
"_id": null,
"home_page": "http://gqylpy.com",
"name": "funccache",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": null,
"author": "\u7af9\u6c38\u5eb7",
"author_email": "<gqylpy@outlook.com>",
"download_url": "https://files.pythonhosted.org/packages/1b/0d/ef5a09035f2fd0d631a04cf960be59fc460abae7236e16ec219d79f5a92f/funccache-2.0.1.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/funccache.svg?style=flat-square\")](https://github.com/gqylpy/funccache/releases/latest)\n[![Python Versions](https://img.shields.io/pypi/pyversions/funccache)](https://pypi.org/project/funccache)\n[![License](https://img.shields.io/pypi/l/funccache)](https://github.com/gqylpy/funccache/blob/master/LICENSE)\n[![Downloads](https://static.pepy.tech/badge/funccache)](https://pepy.tech/project/funccache)\n\n# funccache\nEnglish | [\u4e2d\u6587](https://github.com/gqylpy/funccache/blob/master/README_CN.md)\n\n`funccache`, as its name suggests, is a framework developed by the GQYLPY team that implements function caching. It can cache the return values of specific functions or all methods defined within a class.\n\n> _What if you have a function in your program that gets called multiple times and always returns the same value? To improve code efficiency, you might call the function once, store the return value in a variable, and then use that variable instead of repeatedly calling the function. Sound familiar? That's great! But now, we're here to introduce a more elegant solution: using the `funccache` module to directly cache function return values._\n\n`funccache` can be used in two ways: as a metaclass to cache the return values of all methods defined in its metaclass instances, or as a decorator to cache the return values of decorated functions.\n\n<kbd>pip3 install funccache</kbd>\n\n### Caching Return Values of Class Methods\n\n```python\nimport funccache\n\nclass Alpha(metaclass=funccache):\n ...\n```\nIn this case, all methods and `property` attributes defined in the `Alpha` class will have their return values cached in the `__cache_pool__` attribute after being called once by an instance of the class. Subsequent calls, as long as the parameters remain the same, will directly retrieve values from `__cache_pool__` without re-executing the related code, significantly reducing program overhead and improving code readability.\n\nBy default, this caching functionality only applies to individual instances, and each instance has its own `__cache_pool__` attribute. However, if you want all instances of `Alpha` to share the same cache, you can enable the `__shared_instance_cache__` attribute:\n```python\nclass Alpha(metaclass=funccache):\n __shared_instance_cache__ = True\n```\nSetting the class attribute `__shared_instance_cache__ = True` creates the `__cache_pool__` attribute in the `Alpha` class itself, rather than in each individual instance of `Alpha`.\n\nThe cache never expires by default, but you can set a time-to-live (TTL) for the cache using the class attribute `__ttl__`:\n```python\nclass Alpha(metaclass=funccache):\n __ttl__ = 60\n```\n\nIf you want a specific method or `property` to not be cached, you can add it to the `__not_cache__` list:\n\n```python\nclass Alpha(metaclass=funccache):\n __not_cache__ = [method_obj_or_method_name, ...]\n```\nAdditionally, subclasses of `Alpha` will also inherit the caching functionality.\n\n### Caching Return Values of Functions\n\n```python\nimport funccache\n\n@funccache\ndef alpha():\n ...\n```\nIn this case, the return value of the `alpha` function will be cached after it is called once. Subsequent calls, as long as the parameters remain the same, will directly retrieve the value from the cache without re-executing the `alpha` function.\n\nThe cache never expires by default, but if you want the cache to expire after a certain period, you can use `funccache.ttl`:\n```python\n@funccache.ttl(60)\ndef alpha():\n ...\n```\n\nYou can even cache based on the number of calls using `funccache.count`:\n```python\n@funccache.count(3)\ndef alpha():\n ...\n```\n\nThe decorator usage can also achieve singleton class behavior, as long as the instantiation parameters are consistent:\n```python\n@funccache\nclass Alpha:\n ...\n```\n",
"bugtrack_url": null,
"license": "Apache 2.0",
"summary": "True to its name, `funccache` implements the cache function. Cache the return value of a callable object or all methods defined in a class.",
"version": "2.0.1",
"project_urls": {
"Homepage": "http://gqylpy.com",
"Source": "https://github.com/gqylpy/funccache"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "e8f34c0b776f4284bd767a0c0739cd5f30b5970d7273433f8f3cc3b326b0f780",
"md5": "989b19211878e1addd5820b15fa99297",
"sha256": "aa445b268bb7c15106812c0f7f875c80331f43d3cae0e346c9f2f87dc931291e"
},
"downloads": -1,
"filename": "funccache-2.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "989b19211878e1addd5820b15fa99297",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 11968,
"upload_time": "2024-04-15T01:14:02",
"upload_time_iso_8601": "2024-04-15T01:14:02.858308Z",
"url": "https://files.pythonhosted.org/packages/e8/f3/4c0b776f4284bd767a0c0739cd5f30b5970d7273433f8f3cc3b326b0f780/funccache-2.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "1b0def5a09035f2fd0d631a04cf960be59fc460abae7236e16ec219d79f5a92f",
"md5": "78104fb644f180556d0b2b1d5e1085ba",
"sha256": "8f68a4538e5378f373b74721ed6f275f68346297b87db5e7fcb7ba341c3f7cff"
},
"downloads": -1,
"filename": "funccache-2.0.1.tar.gz",
"has_sig": false,
"md5_digest": "78104fb644f180556d0b2b1d5e1085ba",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 12294,
"upload_time": "2024-04-15T01:14:04",
"upload_time_iso_8601": "2024-04-15T01:14:04.037929Z",
"url": "https://files.pythonhosted.org/packages/1b/0d/ef5a09035f2fd0d631a04cf960be59fc460abae7236e16ec219d79f5a92f/funccache-2.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-04-15 01:14:04",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "gqylpy",
"github_project": "funccache",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "funccache"
}