Tache
Tache 是一个 Python 的缓存框架。它基于如下的目标而设计:
- 同时支持 Python2 和 Python3
- 支持缓存普通函数/实例方法/类方法/静态方法
- 支持 Batch 批量缓存
- 支持基于 Tag 的缓存和失效
- 支持基于参数显式声明 key 格式
Contents
Features
- 默认缓存空值,防止穿透
- 基于tag 批量失效缓存
- batch 批量缓存
- 支持
YAMLJSONPICKLE多种 Backend Serializer
Getting Started
- 基本用法
import random
import fakeredis
from tache import RedisCache
redis_client = fakeredis.FakeStrictRedis()
cache = RedisCache(conn=redis_client, format="JSON")
@cache.cached()
def add(a, b):
return a + b + random.randint(1,100)
result1 = add(5, 6)
# 缓存生效值不变
assert add(5, 6) == result1
# 失效缓存
add.invalidate(5, 6)
assert add(5, 6) != result1
- 基于 tag 的批量缓存失效
tag 可以是固定也可以是动态的,其中动态参数代表在函数中的参数位置。 失效某个 tag 时,代表这个函数下拥有相同 tag 的缓存全部失效。
@cache.cached(tags=["a:{0}"])
def add(a, b):
return a + b + random.randint(1,100)
result1 = add(5, 6)
result2 = add(5, 7)
add.invalidate_tag("a:5")
assert result1 != add(5, 6)
assert result2 != add(5, 7)
- refresh 刷新缓存
当调用refresh 时,将会重新刷新缓存并返回最新值。
class A(object):
def __init__(self):
self.extra = 0
@cache.cached()
def add(self, a, b):
self.extra += 1
return a + b + self.extra
a = A()
assert a.add(5, 6) == 12
assert a.extra == 1
assert a.add.refresh(5, 6) == 13
assert a.extra == 2
- batch 缓存模式
@cache.batch()
def get_comments(*comment_ids):
return [get_comment(c) for c in comment_ids]
get_comments(1,2,3,4,5) # no cache, 调用完毕全部缓存
get_comments(2,3,4,5,6) # 2,3,4,5 从缓存中取,6 在调用完缓存
get_comments.invalidate(3,4,5) # 失效 3,4,5 的缓存
- 显式声明 Key
Tache 允许你显式声明 Key 的生成规则, 不论代码如何重构, 生成的 key 都不会改变。
class B:
def __init__(self):
self.count = 0
@cache.cached("counter.B.add|{0}-{1}")
def add(self, a, b):
self.count += 1
return a + b + self.count
Notice
- 支持
classmethod/staticmethod描述符, 但在使用classmethod时目前必须把classmethod放在内层
class AC(object):
@cache.cached()
@classmethod
def add(cls, a, b):
return a + b + random.randint(1,100)
- 设置 namespace, 处理对象属性修改的问题
key 的生成规则默认为 namespace:module.classname.func|arg1-arg2|tag1-tag2。
其中 namespace 为空, classname 不存在时也为空。
class A(object):
@cache.cache(namespace="v1")
def add(self, a, b):
return db.execute(sql).fetchone()
这个例子中,如果数据库字段发生更改,可以通过修改 namespace 的方式,让新老代码使用不同的缓存结果。
Metadata
Release files for tache 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| tache-0.2.1.tar.gz | 10.5 kB | Without provenance | Details |
Release files / tache-0.2.1.tar.gz
| Download URL | tache-0.2.1.tar.gz |
|---|---|
| Size | 10.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5b5c3d886bfc7d79002a6fd3d9ca07cea5e4711385c357660dd8bfdfbaa115a5
|
|
BLAKE2b-256 checksum How to use checksums |
df0598a90c919a91db9406e7000b9a4e2b4145625171c96e1b471e06aa66a578
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.15.0 pkginfo/1.5.0.1 requests/2.11.1 setuptools/39.2.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/2.7.12
|
Security for tache 0.2.1
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.