Reputation: 3578
On Google App Engine (python), I need to save data with memcache and quickly read it on another page.
But before I start coding to have memcache saved, then on the next page I open up that data with the known key, I'm starting to wonder if the data will always be there on the next page? How long does it take to cache the data, and reliably be there on the next page to read? Is this an issue since it's a cloud service, or would this be an issue even if it was on one server? Or is it not an issue at all and I can count on it being there on the next page?
NOTE: This method isn't the backbone of my webapp, its just a special case I need to use for one scenario. Also, for this scenario I do not want to persist data between pages with a querystring, cookies or header values.
Upvotes: 1
Views: 739
Reputation: 74094
The distributed memcache architecture has a pool of memcache instances that serves all the Google App Engine applications; the stored data is not replicated between memcache servers.
After successfully saving data on memcache, your data will be there* for any subsequent requests because it is stored in one specific memcache instance.
* one caveat: values may be evicted from the cache in case of low memory
Upvotes: 3
Reputation: 10846
I don't believe there is anything special with the GAE implementation of memcached so cached values will be available immediately after they are set, so yes, it will be available on the next page unless it is expired because the memcached server hit it's memory limit. Google don't appear to limit how much data you can store in memcache, only the number of requests you can make so in reality I doubt that this is a problem.
It is not good practice to rely on a cached value being available, in all cases you should use a pattern like the one below.
key = generate_key()
value = memcache.get(key)
if value is None:
value = generate_value()
memcache.set(key, value)
Upvotes: 3
Reputation: 36514
In practice, that data will probably be there most of the time, but you cannot count on it, by design -- see the docs:
However, when considering whether to store a value solely in the memcache and not backed by other persistent storage, be sure that your application behaves acceptably when the value is suddenly not available. Values can expire from the memcache at any time, and may be expired prior to the expiration deadline set for the value. For example, if the sudden absence of a user's session data would cause the session to malfunction, that data should probably be stored in the datastore in addition to the memcache.
(emphasis mine)
Upvotes: 2