为什么 google 数据存储第二次查询 returns 修改后的实例(在第一次查询后不调用 put)?
why does google datastore second query returns modified instance (without calling put after first query)?
我 运行 在查询数据存储时遇到了一种非常违反直觉的情况。
步骤如下:
使用第一个查询检索对象:list_objects = MyModel.query(cls.name == name).fetch()
修改list_objects:for o in list_objects: o.value = new_value
第二次检索对象:list_objects_2 = MyModel.query(cls.name == name).fetch()
现在 list_objects_2 中的所有对象都有 new_value,尽管在第 2 步中,从未调用 put 来实际修改数据存储,我检查了我的数据存储之间,且值为原值
修改list_objects_2:for o in list_objects_2: o.value = new_new_value
,现在list_objects1都有new_new_value
我也试过 fetch(keys_only = True)
和 k.get()
,但同样的问题。
我的印象是查询是针对实际数据存储的,为什么它一直返回相同的实例而不是构建新实例?是由于某种缓存造成的吗?
查看这篇文章https://cloud.google.com/appengine/docs/python/ndb/cache
The in-context cache is fast; this cache lives in memory. When an NDB
function writes to the Datastore, it also writes to the in-context
cache. When an NDB function reads an entity, it checks the in-context
cache first. If the entity is found there, no Datastore interaction
takes place.
When an NDB function queries the Datastore, the result list is
retrieved from the Datastore. However, if any individual result is in
the in-context cache, it is used in place of the value retrieved from
the Datastore query. Query results are written back to the in-context
cache if the cache policy says so (but never to Memcache).
1) list_objects = MyModel.query(cls.name == name).fetch()
预热上下文缓存
2) for o in list_objects: o.value = new_value
仅修改内存中的对象
3) list_objects_2 = MyModel.query(cls.name == name).fetch()
实际上没有发生数据存储交互,您从缓存中获取相同的对象
4) for o in list_objects_2: o.value = new_new_value
仅修改内存中的对象
5) fetch(keys_only = True)
和 k.get()
没有发生数据存储交互
我 运行 在查询数据存储时遇到了一种非常违反直觉的情况。
步骤如下:
使用第一个查询检索对象:
list_objects = MyModel.query(cls.name == name).fetch()
修改list_objects:
for o in list_objects: o.value = new_value
第二次检索对象:
list_objects_2 = MyModel.query(cls.name == name).fetch()
现在 list_objects_2 中的所有对象都有 new_value,尽管在第 2 步中,从未调用 put 来实际修改数据存储,我检查了我的数据存储之间,且值为原值
修改list_objects_2:
for o in list_objects_2: o.value = new_new_value
,现在list_objects1都有new_new_value
我也试过 fetch(keys_only = True)
和 k.get()
,但同样的问题。
我的印象是查询是针对实际数据存储的,为什么它一直返回相同的实例而不是构建新实例?是由于某种缓存造成的吗?
查看这篇文章https://cloud.google.com/appengine/docs/python/ndb/cache
The in-context cache is fast; this cache lives in memory. When an NDB function writes to the Datastore, it also writes to the in-context cache. When an NDB function reads an entity, it checks the in-context cache first. If the entity is found there, no Datastore interaction takes place.
When an NDB function queries the Datastore, the result list is retrieved from the Datastore. However, if any individual result is in the in-context cache, it is used in place of the value retrieved from the Datastore query. Query results are written back to the in-context cache if the cache policy says so (but never to Memcache).
1) list_objects = MyModel.query(cls.name == name).fetch()
预热上下文缓存
2) for o in list_objects: o.value = new_value
仅修改内存中的对象
3) list_objects_2 = MyModel.query(cls.name == name).fetch()
实际上没有发生数据存储交互,您从缓存中获取相同的对象
4) for o in list_objects_2: o.value = new_new_value
仅修改内存中的对象
5) fetch(keys_only = True)
和 k.get()
没有发生数据存储交互