【问题标题】:Memcache item with any Expiration expires immediately on Google App Engine with Go任何 Expiration 的 Memcache 项目在 Google App Engine 上立即过期
【发布时间】:2014-10-06 18:22:06
【问题描述】:

这段代码sn-p:

err = memcache.JSON.Set(c, &memcache.Item{
    Key:        mkey,
    Object:     &total,
    Expiration: 600,
})

接着是第二次调用:

_, err := memcache.JSON.Get(c, mkey, &total); 

...导致缓存未命中。
简单地将 Expiration 值更改为 0 会导致缓存命中,但我无法控制项目何时过期。

我是否误读了到期应该如何工作?

【问题讨论】:

  • 您是否尝试过大于 600 的值? Time.Duration (golang.org/pkg/time/#Duration) 以纳秒为单位。也许 600 纳秒有点短。
  • @VonC:我不知道这里使用的是什么包,但我见过的那些确实在几秒钟内设置了 Expiration,但值得检查 ;)
  • @JimB 我正在查看 google appengine 内存缓存项,它确实使用了Time.Duration:cloud.google.com/appengine/docs/go/memcache/reference#Item
  • @VonC:很好,我没有注意到 app-engine 标签。那可能就是问题所在。
  • 文档说它是以秒为单位指定的,但事实并非如此。 time.Second * 600 工作。

标签: google-app-engine go memcached


【解决方案1】:

由于memcache.Item 确实使用Time.Duration(纳秒),因此最好使用秒来指定Expiration 字段:

 time.Second * 600

memcache 文档提到:

// Expiration is the maximum duration that the item will stay
// in the cache.
// The zero value means the Item has no expiration time.
// Subsecond precision is ignored.
// This is not set when getting items.
Expiration time.Duration

【讨论】:

  • 文档声明该值以秒为单位 (cloud.google.com/appengine/docs/go/memcache) “应用程序可以在存储值时提供过期时间,即相对于添加值时的秒数,或者作为未来的绝对 Unix 纪元时间(从 1970 年 1 月 1 日午夜开始的秒数)。"。你是对的,它在 Nanos 中。谢谢!
  • @user3224681 是的,但该字段在 Time.Duration 中,并且确实提到“亚秒精度被忽略”:因此低于 time.Second * 1 的任何值都可能起作用.
猜你喜欢
  • 1970-01-01
  • 2011-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-11
  • 1970-01-01
  • 2013-03-11
  • 1970-01-01
相关资源
最近更新 更多