python - 检查变量是否设置为 NO_VALUE

标签 python python-2.7 dogpile.cache

我正在使用dogpile 在我的python 程序中进行缓存。 在程序初始化期间,我清理了缓存 SimpleCache.set_fault_injector(NO_VALUE)

@staticmethod
def set_fault_injector(injector):
    SimpleCache.save("fault_injector", injector)

然后我用以下方法检索对象:

@staticmethod
def get_fault_injector():
    return SimpleCache.get("fault_injector")

injector = get_fault_injector()

在我的测试中,我检查 injector 变量是否设置为 NO_VALUE,但比较返回 False(它应该为TRUE)。如何检查injector是否设置为NO_VALUE?我清除缓存的方式是否正确?

In [3]: injector
Out[3]: <dogpile.cache.api.NoValue at 0x7fdde0dfd750>

In [4]: NO_VALUE
Out[4]: <dogpile.cache.api.NoValue at 0x7fdde16dbed0>

In [5]: injector == NO_VALUE
Out[5]: False

In [6]: type(injector)
Out[6]: dogpile.cache.api.NoValue

In [7]: type(NO_VALUE)
Out[7]: dogpile.cache.api.NoValue

In [8]: injector is NO_VALUE
Out[8]: False

[9]: type(injector) == type(NO_VALUE)
Out[9]: True

最佳答案

我没有 Dogpile 的经验,但是,根据我从文档中了解到的情况,当缺少值时,dogpile 通常会返回 dogpile.cache.api.NoValue 的特定实例,这是NO_CACHE

这里的问题可能与您显式地将缓存键设置为 NO_CACHE 相关,这最终会导致您留下 NoValue 类的 2 个不同实例。

在我看来,NO_CACHE并不是真正用于取消设置 key ,我相信如果您实际使用.delete("fault_injector")<,您的问题就会得到解决 或您的缓存包装器可用的任何删除方法。

关于python - 检查变量是否设置为 NO_VALUE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35368666/

相关文章:

python - 如何在每次出现特定元素之间按字母顺序排列列表元素?

python - (逻辑索引)对numpy数组中的RGBA值执行公式

python - 使用 dogpile 根据修改时间缓存下载的文件

python - 测试函数/区域是否缓存在 Beaker/Dogpile 中

python - 如何检测空物体?

python - 列表python的有限排列

python - 确保 matplotlib 颜色条在多个图形中看起来相同

python - 大容量插入错误代码 3 : The system cannot find the path specified

python - Python Numpy 中意外的傅立叶变换结果