我正在使用Python的DiskCache和回忆录装饰器来缓存对静态数据数据库的函数调用。
from diskcache import Cache
cache = Cache("database_cache)
@cache.memoize()
def fetch_document(row_id: int, user: str, password: str):
...我不希望用户和密码成为缓存密钥的一部分。
如何从密钥生成中排除参数?
发布于 2021-05-07 20:19:16
回忆录文档没有显示排除参数的选项。
您可以尝试使用源代码编写自己的装饰器。
或者自己在fetch_document内部使用fetch_document--如下所示
def fetch_document(row_id: int, user: str, password: str):
if row_id in cache:
return cache[row_id]
# ... code ...
# result = ...
cache[row_id] = result
return result 编辑:
或者创建函数的缓存版本,如下所示
def cached_fetch_document(row_id: int, user: str, password: str):
if row_id in cache:
return cache[row_id]
result = fetch_document(row_id: int, user: str, password: str)
cache[row_id] = result
return result 稍后,您可以决定是否要使用cached_fetch_document代替fetch_document。
发布于 2022-06-21 09:26:05
在5.3.0版本之后,回忆录可以使用忽略参数来忽略位置参数
https://stackoverflow.com/questions/67439103
复制相似问题