Google App Engine 数据存储区查询返回过时数据
Google App Engine Datastore query returning stale data
我有一个基于 GAE 的项目,使用 db
Datastore
。在我的项目中,我每 15 分钟 运行 一个作业,并在 Datastore
中更新数据。然后我有一个查询 Datastore
并显示结果的端点。但是,我得到的结果是陈旧的,而不是更新的结果。似乎 GAE 没有命中缓存中的 Datastore
和 returns 数据,但我不确定。这是我的代码:
class MainHandler(webapp2.RequestHandler):
def get(self):
query = Contests.all()
contests_data = query.fetch(1) # fetch the data from datastore
self.response.write(contests_data[0].data)
class DataBaseHandler(webapp2.RequestHandler):
# the job that runs every 15 minutes
def get(self):
contests_data = get_all_contests() # get the new data
query = Contests.all()
contests = query.fetch(1)
contests[1].data = contests_data # update the data
db.put(contests[0])
self.response.write(json.dumps({"message":"updated"}))
这是我的模型:
class Contests(db.Model):
"""Models Contests"""
data = db.TextProperty(default="{}")
created = db.DateTimeProperty(auto_now_add=True)
modified = db.DateTimeProperty(auto_now=True)
我尝试使用 memcache.flush_all()
清除缓存,但即使那样也不能解决问题。为什么 GAE return 没有更新数据?我该如何解决这个问题?
编辑:我将查询更改为祖先查询,但数据仍然陈旧(而且已经有几天了)。
main.py
class MainHandler(webapp2.RequestHandler):
def get(self):
contest_list = ContestList.get_or_insert('contest_list', name='ContestList')
query = Contests.all()
query.ancestor(contest_list)
contests = query.fetch(1)
self.response.write(contests[0].data)
class DataBaseHandler(webapp2.RequestHandler):
def get(self):
contests_data = get_all_contests()
contest_list = ContestList.get_or_insert('contest_list', name='ContestList')
query = Contests.all()
query.ancestor(contest_list)
contests = query.fetch(1)
contests[0].data = contests_data
contests[0].put()
self.response.write(json.dumps({"message":"updated"}))
models.py
:
class ContestList(db.Model):
name = db.StringProperty()
class Contests(db.Model):
"""Models Contests"""
data = db.TextProperty(default="{}")
created = db.DateTimeProperty(auto_now_add=True)
modified = db.DateTimeProperty(auto_now=True)
我什至尝试使用 key
:
获取数据
class MainHandler(webapp2.RequestHandler):
def get(self):
q = ndb.Key('Contests', 'contest_data').get()
if q:
self.response.write(q.data)
else:
self.response.write("error")
class DataBaseHandler(webapp2.RequestHandler):
def get(self):
Contests(key=ndb.Key('Contests', "contest_data"), data=get_all_contests()).put()
self.response.write(json.dumps({"message":"updated"}))
GAE 没有命中数据存储的是什么?我在这里错过了什么?
数据存储仅对查询具有最终一致性。在复制数据期间,确实有可能遇到“陈旧”数据。
要确保在查询中强制执行强一致性,您需要使用祖先查询或按键获取。
阅读更多相关内容here
例如:祖先查询
(documentation) 为此,您显然需要稍微更改一下结构。我不知道模型“Contest”的祖先是什么,但假设它们都属于“mainAncestor”实体。
当您创建实体时,您需要有类似的东西:
newContest = Contest(parent=mainAncestor)
然后当你检索时,你在你的代码中添加这个:
query = Contests.all()
query.ancestor(mainAncestor)
contests = query.fetch(1)
这将强制您的数据保持高度一致。你显然需要创建那个“mainAncestor”,然后它会导致你需要确保你解决的其他警告(你只能每秒写入一次实体组(实体分组在同一祖先下),例如).
我有一个基于 GAE 的项目,使用 db
Datastore
。在我的项目中,我每 15 分钟 运行 一个作业,并在 Datastore
中更新数据。然后我有一个查询 Datastore
并显示结果的端点。但是,我得到的结果是陈旧的,而不是更新的结果。似乎 GAE 没有命中缓存中的 Datastore
和 returns 数据,但我不确定。这是我的代码:
class MainHandler(webapp2.RequestHandler):
def get(self):
query = Contests.all()
contests_data = query.fetch(1) # fetch the data from datastore
self.response.write(contests_data[0].data)
class DataBaseHandler(webapp2.RequestHandler):
# the job that runs every 15 minutes
def get(self):
contests_data = get_all_contests() # get the new data
query = Contests.all()
contests = query.fetch(1)
contests[1].data = contests_data # update the data
db.put(contests[0])
self.response.write(json.dumps({"message":"updated"}))
这是我的模型:
class Contests(db.Model):
"""Models Contests"""
data = db.TextProperty(default="{}")
created = db.DateTimeProperty(auto_now_add=True)
modified = db.DateTimeProperty(auto_now=True)
我尝试使用 memcache.flush_all()
清除缓存,但即使那样也不能解决问题。为什么 GAE return 没有更新数据?我该如何解决这个问题?
编辑:我将查询更改为祖先查询,但数据仍然陈旧(而且已经有几天了)。
main.py
class MainHandler(webapp2.RequestHandler):
def get(self):
contest_list = ContestList.get_or_insert('contest_list', name='ContestList')
query = Contests.all()
query.ancestor(contest_list)
contests = query.fetch(1)
self.response.write(contests[0].data)
class DataBaseHandler(webapp2.RequestHandler):
def get(self):
contests_data = get_all_contests()
contest_list = ContestList.get_or_insert('contest_list', name='ContestList')
query = Contests.all()
query.ancestor(contest_list)
contests = query.fetch(1)
contests[0].data = contests_data
contests[0].put()
self.response.write(json.dumps({"message":"updated"}))
models.py
:
class ContestList(db.Model):
name = db.StringProperty()
class Contests(db.Model):
"""Models Contests"""
data = db.TextProperty(default="{}")
created = db.DateTimeProperty(auto_now_add=True)
modified = db.DateTimeProperty(auto_now=True)
我什至尝试使用 key
:
class MainHandler(webapp2.RequestHandler):
def get(self):
q = ndb.Key('Contests', 'contest_data').get()
if q:
self.response.write(q.data)
else:
self.response.write("error")
class DataBaseHandler(webapp2.RequestHandler):
def get(self):
Contests(key=ndb.Key('Contests', "contest_data"), data=get_all_contests()).put()
self.response.write(json.dumps({"message":"updated"}))
GAE 没有命中数据存储的是什么?我在这里错过了什么?
数据存储仅对查询具有最终一致性。在复制数据期间,确实有可能遇到“陈旧”数据。
要确保在查询中强制执行强一致性,您需要使用祖先查询或按键获取。
阅读更多相关内容here
例如:祖先查询
(documentation) 为此,您显然需要稍微更改一下结构。我不知道模型“Contest”的祖先是什么,但假设它们都属于“mainAncestor”实体。
当您创建实体时,您需要有类似的东西:
newContest = Contest(parent=mainAncestor)
然后当你检索时,你在你的代码中添加这个:
query = Contests.all()
query.ancestor(mainAncestor)
contests = query.fetch(1)
这将强制您的数据保持高度一致。你显然需要创建那个“mainAncestor”,然后它会导致你需要确保你解决的其他警告(你只能每秒写入一次实体组(实体分组在同一祖先下),例如).