Skip to main content

LOGO Release Python Versions License Downloads

gqylpy-cache

如其名,gqylpy_cache 实现缓存功能,由 GQYLPY 团队研发的一个框架,可缓存某个函数或某个类中定义的所有方法的返回值。

你的程序中有一个函数会被多次调用,并且返回值不变,你会怎么做?为提高代码效率,你会先调用一次该函数并把返回值存到一个变量,之后就使用这个变量,而不是重复调用函数。是这样吗?你已经很不错了。但现在,我们要传授你一种比之更简明的方案,使用 gqylpy_cache 模块直接缓存函数返回值。

gqylpy_cache 有两种使用方式:当做元类使用,将缓存其元类实例中定义的所有方法的返回值;当做装饰器使用,将缓存被装饰函数的返回值。

pip3 install gqylpy_cache

缓存类中方法的返回值
import gqylpy_cache

class Alpha(metaclass=gqylpy_cache):
    ...

此时,类 Alpha 中定义的所有方法以及property属性,在被其实例调用一次后,返回值都将被缓存,缓存在 __cache_pool__ 属性中。此后的每次调用,只要参数不变,都是直接从 __cache_pool__ 中取值,不会重复执行相关代码,大幅减少程序功耗并提高代码可读性。

上述缓存功能默认只作用于单个实例,每个实例都有自己的 __cache_pool__ 属性,若希望 Alpha 的所有实例共享同一份缓存,可启用 __shared_instance_cache__ 属性:

class Alpha(metaclass=gqylpy_cache):
    __shared_instance_cache__ = True

设置类属性 __shared_instance_cache__ = True 后,属性 __cache_pool__ 将被创建在 Alpha 类中,而不是 Alpha 的每个实例中。

若希望某个方法或property不被缓存,可将其加入到 __not_cache__ 列表中:

class Alpha(metaclass=gqylpy_cache):
    __not_cache__ = [method_obj_or_method_name, ...]

另外,Alpha 的子类也拥有上述缓存功能。

缓存函数返回值
import gqylpy_cache

@gqylpy_cache
def alpha():
    ...

此时,函数 alpha 在被调用一次后,其返回值将被缓存。此后的每次调用,只要参数不变,都是直接从缓存中取值,而不会重复执行 alpha 函数。

装饰器的用法亦可获得单例类,只要实例化参数一致:

@gqylpy_cache
class Alpha:
    ...

另外一种兼容编辑器语法提示的用法:

from gqylpy_cache import cache

@cache
def alpha():
    ...

Metadata

Release files for gqylpy-cache 1.3.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gqylpy-cache 1.3.4
File Size Uploaded Provenance
gqylpy_cache-1.3.4.tar.gz 10.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for gqylpy-cache 1.3.4
File Interpreter ABI Platform
gqylpy_cache-1.3.4-py3-none-any.whl Python 3 none any Details

Total release size: 21.2 kB

Release files / gqylpy_cache-1.3.4.tar.gz

Download URL gqylpy_cache-1.3.4.tar.gz
Size 10.1 kB
Tags Source
SHA-256 checksum
How to use checksums
89ec7128528810691777fdc399903bd94cb2943f3b966765276f5f713897af6f
BLAKE2b-256 checksum
How to use checksums
19b4a4b11c8a9deb8ac32d896be9e626b55dd584c5893e11026a8e338d82500d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / gqylpy_cache-1.3.4-py3-none-any.whl

Download URL gqylpy_cache-1.3.4-py3-none-any.whl
Size 11.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e3a3cf2feffc1818c5121aecd57add1dfd23a512b1fdb70ea77545f71d2fadf1
BLAKE2b-256 checksum
How to use checksums
eff0a8b8578cfd72c31d0e0e2189034135db8bd49203587753e48b0b9725c1e4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Security for gqylpy-cache 1.3.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page