【问题标题】:Store an object in memcache of GAE in Go在 Go 中的 GAE 的 memcache 中存储一个对象
【发布时间】:2012-10-27 04:53:58
【问题描述】:
我想使用 Go 在 GAE 的内存缓存中存储一个对象。 gae 文档仅显示如何在此处存储 []byte:https://developers.google.com/appengine/docs/go/memcache/overview
当然,有一些通用的方法可以将对象序列化为 []byte,通过这些方法可以完成我的任务。但是通过阅读memcache参考,我发现memcache Item中有一个“Object”:
// Object is the Item's value for use with a Codec.
Object interface{}
这似乎是一种将对象存储在内存缓存中的内置机制。但是,gae 文档没有提供示例代码。
谁能给我举个例子?提前致谢
【问题讨论】:
标签:
google-app-engine
memcached
go
【解决方案1】:
好的,我只是自己想通了。 memcache pkg 有两个内置的 Codec:gob 和 json。只需使用其中一个(或者当然可以创建自己的编解码器):
var in, out struct {I int;}
// Put in into memcache
in.I = 100
item := &memcache.Item {
Key: "TestKey",
Object: in,
}
memcache.Gob.Set(c, item) // error checking omitted for convenience
// retrieve the value
memcache.Gob.Get(c, "TestKey", &out)
fmt.Fprint(w, out) // will print {100}
谢谢大家