How Go Should Easily Convert Data to Bytes or Strings

I am developing a couple of applications using the Google App Engine Go SDK that use Memcache as a buffer for loading data from a data warehouse. Since Memcache can only store data as []byte , I often find myself creating functions for encoding various structures as strings, as well as functions for modifying a process. Needless to say, this is rather tedious when I need to do such things 5 ​​times.

Is there an easy way to convert any arbitrary structure that can be stored in the Datastore into []byte in order to save it in Memcache and then load it back without creating custom code for various structures in GAE Golang?

+8
google-app-engine data-structures serialization go memcached
source share
2 answers

http://golang.org/pkg/encoding/gob or http://golang.org/pkg/encoding/json can turn arbitrary data types into [] byte nodes if certain rules apply to encoded data structures. You probably need one of them that will be encoded with smaller sizes, but json is easier with other languages ​​if required.

+5
source share

It seemed to me that I needed the same thing. So I created a package called:

AEGo / ds

Documentation | A source

go get github.com/scotch/aego/ds

It uses the same API as "appengine/datastore" , so it will work as a replacement.

 import "github.com/scotch/aego/v1/ds" u = &User{Name: "Bob"} key := datastore.NewKey(c, "User", "bob", 0, nil) key, err := ds.Put(c, key, u) u = new(User) err = ds.Get(c, key, u) 

By default, it caches all Put and Get in memcache, but you can change this behavior by calling the ds.Register method:

 ds.Register("User", true, false, false) 

The Register method takes a string representing Kind and 3 bool - userDatastore , useMemcache , useMemory . Passing true will cause AEgo/ds to keep the record in this repository. The Storage is useful for records that you do not expect to change, but may contain outdated data if you have more than one instance.

Supported Methods:

 Put PutMulti Get GetMulti Delete DeleteMulti AllocateIDs 

Note. Currently, cashing out occurs only with Get . GetMulti is retrieved from the data store.

AEgo/ds is work, but the code is well tested. Any feedback would be appreciated.

And to answer this question, how do I serialize entities for gob to save memcache.

0
source share

All Articles