1
0
Fork 0
mirror of https://github.com/imjasonh/appengine-value synced 2026-07-06 22:12:22 +00:00

GetMulti, untested

This commit is contained in:
Jason Hall 2014-05-18 16:39:11 -04:00
parent 973b7ec198
commit c44df16c62
2 changed files with 46 additions and 2 deletions

View file

@ -2,10 +2,10 @@ package value
import (
"errors"
"time"
"fmt"
"net/http"
"html/template"
"net/http"
"time"
"appengine"
"appengine/datastore"

View file

@ -67,6 +67,50 @@ func Get(c appengine.Context, key string) string {
return e.Value
}
func GetMulti(c appengine.Context, key ...string) map[string]string {
m := map[string]string{}
// Get whatever values we can from memcache
mi, err := memcache.GetMulti(c, key)
if err != nil {
c.Errorf("error getting multi from memcache: %v", err)
}
for k, i := range mi {
m[k[len(MemcacheKeyPrefix):]] = string(i.Value)
}
if len(mi) == len(key) {
// All values found in memcahe!
return m
}
// Get values not found in memcache from datastore.
keys := []*datastore.Key{}
for _, k := range key {
if _, ok := mi[k]; !ok {
keys = append(keys, datastore.NewKey(c, Kind, k, 0, nil))
}
}
fromDS := []e{}
if err := datastore.GetMulti(c, keys, fromDS); err != nil {
c.Errorf("error getting multi from datastore: %v", err)
return map[string]string{}
}
items := []*memcache.Item{}
for i, de := range fromDS {
m[keys[i].StringID()] = de.Value
items = append(items, &memcache.Item{
Key: MemcacheKeyPrefix+keys[i].StringID(),
Value: []byte(de.Value),
})
}
// Store values in memcache for next time.
if err := memcache.SetMulti(c, items); err != nil {
c.Errorf("error setting multi in memcache: %v", err)
}
return m
}
var vals = map[string]*string{}
func String(key string) *string {