如何清除NDB中特定模型的缓存 [英] How to clear cache for specific model in NDB

查看:87
本文介绍了如何清除NDB中特定模型的缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在转换到NDB,我使用了两个模型集:一个基于普通旧 google.appengine.ext.db ,另一个基于新花式 google.appengine.ext.ndb

I am in the process of transitioning to the NDB, and I am using two model sets: one based in plain old google.appengine.ext.db and one based on new fancy google.appengine.ext.ndb.

我想使用基于NDB的模型进行读取 - 只保留NDB中内置的缓存,同时能够使用旧模型存储更改(并表明需要时将缓存更新到NDB)。

I would like to use NDB-based models for read-only and retain the caching that's built into NDB, while being able to store changes using the old models (and signal the need to update caches to the NDB when needed).

如何根据旧的 db

How can I flush/clear cache for a specific model instance in the NDB while saving changes in the model based on old db?

推荐答案

我会建议为那些重复的模型类禁用缓存;最好安全,不要抱歉。这很容易通过将

I would recommend just disabling the cache for those model classes that you have in duplicate; better be safe than sorry. This is easily done by putting

   _use_memcache = False
   _use_cache = False

在每个ndb.Model子类中(即在属性声明之前或之后)。相关文档位于: https://developers.google.com/appengine / docs / python / ndb / cache#policy_functions (最后查找表格)。

inside each ndb.Model subclass (i.e. before or after the property declarations). Docs for this are here: https://developers.google.com/appengine/docs/python/ndb/cache#policy_functions (look for the table towards the end).

如果您确实只想清除缓存使用旧的db.Model子类写一个实体,而不是上面的,你可以尝试下面的(假设ent是一个db.Model子类实例):

If you really want to clear the cache only when you write an entity using a old db.Model subclass, instead of the above you can try the following (assume ent is a db.Model subclass instance):

  ndbkey = ndb.Key.from_old_key(ent.key())
  ndbkey.delete(use_datastore=False)

这会从memcache和上下文缓存中删除密钥,但不会将其从数据存储中删除。但是,当您尝试使用其NDB密钥(或甚至当它作为查询结果返回时)读取它时,它似乎将被删除,直到当前的HTTP请求处理程序完成,并且它将不会使用memcache约30秒。

This deletes the key from memcache and from the context cache but does not delete it from the datastore. When you try to read it back using its NDB key (or even when it comes back as a query result), however, it will appear to be deleted until the current HTTP request handler finishes, and it will not use memcache for about 30 seconds.

这篇关于如何清除NDB中特定模型的缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆