保存memcache数据后,是否可以立即在GAE上使用?

发布于 2024-10-19 23:03:20 字数 311 浏览 1 评论 0原文

在Google App Engine(python)上,我需要使用memcache保存数据并在另一个页面上快速读取它。

但在我开始编码以保存内存缓存之前,然后在下一页上我使用已知密钥打开该数据,我开始怀疑数据是否会始终存在于下一页上?缓存数据并可靠地出现在下一页上需要多长时间才能读取?这是一个问题,因为它是云服务,或者即使它在一台服务器上也会是一个问题吗?或者这根本不是问题,我可以指望它出现在下一页上?

注意:此方法不是我的网络应用程序的主干,它只是我需要在一个场景中使用的一种特殊情况。另外,对于这种情况,我不想使用查询字符串、cookie 或标头值在页面之间保留数据。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

行雁书 2024-10-26 23:03:20

分布式内存缓存架构有一个内存缓存实例池,为所有 Google App Engine 应用程序提供服务;存储的数据不会在内存缓存服务器之间复制。

在 memcache 上成功保存数据后,您的数据将在任何后续请求中*,因为它存储在一个特定的 memcache 实例中。

* 一个警告:如果内存不足,值可能会从缓存中逐出

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

地狱即天堂 2024-10-26 23:03:20

我不认为 memcached 的 GAE 实现有什么特别之处,因此缓存的值在设置后将立即可用,所以是的,它将在下一页上可用,除非它因 memcached 服务器达到内存限制而过期。谷歌似乎并没有限制你可以在内存缓存中存储多少数据,只限制你可以发出的请求数量,所以实际上我怀疑这是一个问题。

依赖可用的缓存值并不是一个好的做法,在所有情况下,您都应该使用如下所示的模式。

key = generate_key()
value = memcache.get(key)
if value is None:
    value = generate_value()
    memcache.set(key, value)

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)
未蓝澄海的烟 2024-10-26 23:03:20

实际上,这些数据可能大部分时间都在那里,但在设计上您不能指望它 - 请参阅 文档

但是,当考虑是否
仅将值存储在内存缓存中
并且没有其他持久的支持
存储,请确保您的应用程序
当值为
突然不可用。 价值观可以
内存缓存随时过期

并且可能会在
为该值设置的到期期限。
例如,如果突然没有
用户的会话数据会导致
会话发生故障,该数据
可能应该存储在
除了内存缓存之外的数据存储。

(强调我的)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文