英文:
Memcache item with any Expiration expires immediately on Google App Engine with Go
问题
这段代码片段:
err = memcache.JSON.Set(c, &memcache.Item{
Key: mkey,
Object: &total,
Expiration: 600,
})
紧接着是第二次调用:
_, err := memcache.JSON.Get(c, mkey, &total);
...导致缓存未命中。
将Expiration值更改为0会导致缓存命中,但我无法控制项目何时过期。
我是否误解了过期时间的工作原理?
英文:
This code snippet:
err = memcache.JSON.Set(c, &memcache.Item{
Key: mkey,
Object: &total,
Expiration: 600,
})
followed by a second call with this:
_, err := memcache.JSON.Get(c, mkey, &total);
...results in a cache miss.
Simply changing the Expiration value to 0 results in cache hits, but then I can't control when the items expire.
Am I misreading how expiration is supposed to work?
答案1
得分: 4
由于memcache.Item使用Time.Duration(纳秒),最好使用秒来指定Expiration
字段:
time.Second * 600
memcache文档中提到:
// Expiration是项目在缓存中保留的最长时间。
// 零值表示项目没有过期时间。
// 忽略亚秒精度。
// 获取项目时不设置此值。
Expiration time.Duration
英文:
Since the memcache.Item does use Time.Duration (nanosecond), it is best to specify the Expiration
field using seconds:
time.Second * 600
The memcache doc mentions:
// 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论