Google App Engine Objectify - 加载单个对象或键列表?

Mat*_*mer 3 java google-app-engine objectify

我正在努力掌握Go​​ogle App Engine编程,并想知道这两种方法之间的区别是什么 - 如果有实际差异的话.

方法A)

public Collection<Conference> getConferencesToAttend(Profile profile)
{
    List<String> keyStringsToAttend = profile.getConferenceKeysToAttend();
    List<Conference> conferences = new ArrayList<Conference>();
    for(String conferenceString : keyStringsToAttend)
    {
        conferences.add(ofy().load().key(Key.create(Conference.class,conferenceString)).now());
    }
    return conferences; 
}
Run Code Online (Sandbox Code Playgroud)

方法B)

public Collection<Conference> getConferencesToAttend(Profile profile)
    List<String> keyStringsToAttend = profile.getConferenceKeysToAttend();
    List<Key<Conference>> keysToAttend = new ArrayList<>();
    for (String keyString : keyStringsToAttend) {
        keysToAttend.add(Key.<Conference>create(keyString));
    }
    return ofy().load().keys(keysToAttend).values();
}
Run Code Online (Sandbox Code Playgroud)

"conferenceKeysToAttend"列表保证只有唯一的会议 - 即使我选择了两种备选方案中的哪一种呢?如果是这样,为什么?

tj-*_*ess 5

方法A逐个加载实体,而方法B执行批量加载,这样更便宜,因为您只需要向Google的数据中心进行一次网络往返.您可以通过多次加载一堆密钥来测量两种方法所花费的时间来观察这一点.

在进行批量加载时,如果数据存储区操作引发异常,则需要对加载的实体保持谨慎.即使没有加载某些实体,操作也可能成功.

  • 这应该标记为正确的答案.始终支持批量操作. (3认同)