【问题标题】:How to stop Objectify from returning cached values?如何阻止 Objectify 返回缓存值?
【发布时间】:2013-03-25 04:30:16
【问题描述】:

我正在像这样使用 Objectify 保存一个对象:

Thing th = new Thing();
th.identifier = thingId;
th.name = thingName;
th.json = thingData;
ofy().save().entity(thing);
pResponse.setStatus(200);
pResponse.getWriter().println("OK");

我使用 GAE 数据存储浏览器验证数据库中的值已更新。我在本地运行。然后我加载所有这样的东西:

Map<String, List<Thing>> responseJsonMap = new HashMap<String, List<Thing>>();
List<Thing> things = ofy().load().type(Thing.class).list();         
responseJsonMap.put("things", things);
pResponse.setContentType("application/json");
try {
    GSON.toJson(responseJsonMap, pResponse.getWriter());
} ...

我得到的是保存之前存在的数据。我尝试关闭实体上的缓存并调用ofy().clear(),但都不起作用。如果我重新启动服务器或等待足够长的时间,保存的数据就会通过。我也尝试在保存后添加.now(),但这不是必需的,因为我可以在数据存储中验证操作已完成。我真的很希望能够加载我刚刚保存的数据。我做错了什么?

【问题讨论】:

  • 我正在使用 Objectify4.0b1。

标签: objectify


【解决方案1】:

我做错的是没有阅读手册而忘记了一个步骤。

Objectify 需要一个过滤器来清理任何线程本地事务 保持在末尾的上下文和挂起的异步操作 一个要求。将此添加到您的 WEB-INF/web.xml:

<filter>
     <filter-name>ObjectifyFilter</filter-name>
     <filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
</filter>
<filter-mapping>
     <filter-name>ObjectifyFilter</filter-name>
     <url-pattern>/*</url-pattern> 
</filter-mapping>

好吧,检查一下你是否得到了你真的没想到的缓存值。

【讨论】:

    【解决方案2】:

    除了 ObjectifyFilter 问题之外,您最有可能看到的是数据存储区中查询的最终一致性。

    看到这个:https://developers.google.com/appengine/docs/java/datastore/structuring_for_strong_consistency

    【讨论】:

    • 这是一件值得关注的好事情,但是由于我在本地运行,所以我还没有遇到这个问题。
    猜你喜欢
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多