如何修复 Python cache3 ImportError: cannot import name 'SafeCache' from 'cache3'

问题:

你想按快速入门所示使用 cache3 的 SafeCache

safecache_example.py
from cache3 import SafeCache
cache = SafeCache()

但你却看到以下错误消息:

error.txt
ImportError: cannot import name 'SafeCache' from 'cache3' (/usr/local/lib/python3.10/dist-packages/cache3/__init__.py)

解决方案

cache3 已更新,但文档仍未修复。这是一个已知 bug

最接近的等价物是带 thread_safe=TrueCache(),一个支持标记的内存缓存:

cache_replacement_example.py
from cache3 import Cache
cache = Cache(name="mycache", thread_safe=True)

如果你不需要标记,考虑 MiniCache

minicache_example.py
from cache3 import MiniCache
cache = MiniCache(name="mycache", thread_safe=True)

Check out similar posts by category: Python