python - 在 memcache.get() 之前测试字符串是否是有效键

标签 python google-app-engine python-memcached

Google App Engine 中是否有一个函数可以在不使用 db.get() 的情况下调用 memcache.get(key) 之前测试字符串是否为有效的“字符串键” code> 还是先 db.get_by_key_name()

在我的例子中, key 是从用户的获取请求传递的: obj = memcache.get(self.request.get("obj"))

不知何故,我想知道该字符串是否是有效的 key 字符串,而不先调用数据库,这会破坏使用内存缓存的目的。

最佳答案

这可能是确定 key 字符串是否有效的最有效(也是实用)的方法。代码显然会在尝试从内存缓存/数据存储区检索实体之前为您执行该测试。更好的是,如有必要,Google 会更新该代码。

try:
    obj = memcache.get(self.request.get("obj"))
except BadKeyError:
    # give a friendly error message here

此外,请考虑切换到 ndb。对键执行 get() 会自动使用两级缓存:本地缓存和内存缓存。您不需要为 memcache 编写单独的代码。

关于python - 在 memcache.get() 之前测试字符串是否是有效键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16859674/

相关文章:

python - Facebook Messenger 机器人代码错误

python 3 : Looking for alternatives to gevent and pylibmc/python-memcached

ubuntu - 增加 Memcached 最大项目大小

python - ResourceWarning : python-memcached not closing socket?

python - SpaCy 提取形容词,位于动词之前,不是停用词也不是标点符号

Python线程处理数据库行

python - 将列表项转换为具有默认值的字典键

用于访问 Hg、Git 和可能的 Bazaar 存储库的 Python 包装器?

javascript - AJAX Post 使用 Python 和 javascript 存储 JSON

python - 使用谷歌应用程序引擎和Python,如何检查复选框是否被标记?