GAE, memcache before DB update

*爱你&永不变心* 提交于 2019-12-12 02:26:32

问题


I have some troubles with memcache and GAE DB operations. if i update memcache rigth after DB operations, x.put(), for example, my memcache function often return old value. If i use sleep(), cache more often correct, but this is not right, in my opinion

sleep(0.2) 
data = Picture.all().order('-created').fetch(300)
memcache.set('pictures_all', data)

What i need to do, to get correct memcache?

ANSWER: Need to use parent with query, all Picture entities must have same parent, then you get strong consistant results

data = Picture.all().order('-created').ancestor(main_key()).fetch(300)
memcache.set('pictures_all', data)

回答1:


If you have the data, just update one entry in the memcache, no need to retrieve all from memcache. Something like

data.put()
memcache.set(key, data)



回答2:


You're on the right track that the problem is with eventual consistency.

Using STRONG_CONSISTENCY does solve the problem, but it'll give you scalability problems down the road - ones that will be difficult to resolve.

The solution for this is, annoyingly, more complex than it should be. I'm also not sure whether there's really a bulletproof solution given the eventual consistency behavior.

pseudocode should look something like this:

all_pictures = memcache.get('pictures_all')
if not all_pictures:
    all_pictures = convert_to_list(Picture.all().order('-created').fetch(300))
if not newdata in all_pictures:
    add_to_list_in_proper_order(all_pictures, newdata)
memcache.set('pictures_all', all_pictures)



回答3:


config = db.create_config(deadline=10, read_policy=db.STRONG_CONSISTENCY)
data = Picture.all().order('-created').fetch(300, config=config)
memcache.set('pictures_all', data)

I guess, this is solution.

EDIT: No, this is dont work




回答4:


Great.

I had the same problem and the solution was exactly what asker gave: the use of ancestors

To read:

data = Picture.all().order('-created').ancestor(main_key()).fetch(300)

To save:

pic = Picture(parent=main_key(), ...)
pic.put()


来源:https://stackoverflow.com/questions/15846971/gae-memcache-before-db-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!