A simple and intuitive package named cascadestore provided by the go-appengine-sessioncascade project to implement and combine Memcache, Datastore, the request context, or any combination of them, as session backends under AppEngine.
Example:
package handlers
import (
"net/http"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
"github.com/dsoprea/goappenginesessioncascade"
)
const (
sessionName = "MainSession"
)
var (
sessionSecret = []byte("SessionSecret")
sessionStore = cascadestore.NewCascadeStore(cascadestore.DistributedBackends, sessionSecret)
)
func HandleRequest(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
if session, err := sessionStore.Get(r, sessionName); err != nil {
panic(err)
} else {
if vRaw, found := session.Values["ExistingKey"]; found == false {
log.Debugf(ctx, "Existing value not found.")
} else {
v := vRaw.(string)
log.Debugf(ctx, "Existing value: [%s]", v)
}
session.Values["NewKey"] = "NewValue"
if err := session.Save(r, w); err != nil {
panic(err)
}
}
}
Like this:
Like Loading...