在Go中将对象存储在GAE的memcache中

Min*_*ang 16 google-app-engine memcached go

我想使用Go在GAE的memcache中存储一个对象.gae文档仅显示如何在此处存储[]字节:https://developers.google.com/appengine/docs/go/memcache/overview

当然,有一般方法将对象序列化为[]字节,通过它我可以完成任务.但是通过阅读memcache引用,我发现memcache中有一个"对象"Item:

// Object is the Item's value for use with a Codec.
Object interface{}
Run Code Online (Sandbox Code Playgroud)

这似乎是一个在memcache中存储对象的内置机制.但是,gae文档没有提供示例代码.

有人能告诉我一个例子吗?提前致谢

Min*_*ang 26

好吧,我只是想出了自己.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}
Run Code Online (Sandbox Code Playgroud)

谢谢大家