Idiots guide to app engine and memcache

≡放荡痞女 提交于 2019-12-06 07:13:40

If you cache a query, is there an accepted method for ensuring that the cache is cleared/updated when an object stored in that query is updated.

Typically you wrap your reads with a conditional to retrieve the value from the main DB if it's not in the cache. Just wrap your updates as well to fill the cache whenever you write the data. That's if you need the results to be as up to date as possible - if you're not so bothered about it being out of date just set an expiry time that is low enough for the application to have to re-request the data from the main DB often enough.

krisrak

One way to easily do this is by searching for any datastore put() call and set memcache after it. Next make sure to get data from memcache before attempting a datastore query.

Set memcache after writing to datastore:

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

Try getting data from memcache before doing a datastore query:

data = memcache.get(user_id)
if data is None:
    data = Data.get_by_key_name(user_id)
    memcache.set(user_id, data)

Using Memcache reduces app engine costs significantly. More details on how I optimized on app engine.

About reference properties, let's say you have MainModel and RefModel with a reference property 'ref' that points to a MainModel instance. Whenever you cal ref_model.ref it does a datastore get operation and retrieves the object from datastore. It does not interact with memcache in any way.

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