本文整理汇总了Golang中google/golang.org/appengine/memcache.Get函数的典型用法代码示例。如果您正苦于以下问题:Golang Get函数的具体用法?Golang Get怎么用?Golang Get使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Get函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: handleIndex
func handleIndex(res http.ResponseWriter, req *http.Request) {
cookie, _ := req.Cookie("sessionid")
if cookie == nil {
id, _ := uuid.NewV4()
cookie = &http.Cookie{
Name: "sessionid",
Value: id.String(),
}
http.SetCookie(res, cookie)
}
ctx := appengine.NewContext(req)
item, _ := memcache.Get(ctx, cookie.Value)
if item == nil {
m := map[string]string{
"email": "[email protected]",
}
bs, _ := json.Marshal(m)
item = &memcache.Item{
Key: cookie.Value,
Value: bs,
}
}
fmt.Fprintln(res, string(item.Value))
}
示例2: index
func index(res http.ResponseWriter, req *http.Request) {
html := ``
//Build Cookie
id, _ := uuid.NewV4()
cookie := &http.Cookie{
Name: "my-cookie",
Value: id.String(),
HttpOnly: true,
}
http.SetCookie(res, cookie)
//Store memcache
ctx := appengine.NewContext(req)
item := memcache.Item{
Key: id.String(),
Value: []byte("Matthew"),
}
memcache.Set(ctx, &item)
//Get uuid from cookie
cookieGet, _ := req.Cookie("my-cookie")
if cookieGet != nil {
html += `UUID from cookie: ` + cookieGet.Value + `<br>`
}
//Get uuid and value from memcache
ctx = appengine.NewContext(req)
item0, _ := memcache.Get(ctx, id.String())
if item0 != nil {
html += `Value from Memcache using uuid: ` + string(item0.Value) + `<br>`
}
res.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprint(res, html)
}
示例3: getResources
func getResources(w http.ResponseWriter, r *http.Request) *appError {
//omitted code to get top, filter et cetera from the Request
c := appengine.NewContext(r)
h := fnv.New64a()
h.Write([]byte("rap_query" + top + filter + orderby + skip))
cacheKey := h.Sum64()
cv, err := memcache.Get(c, fmt.Sprint(cacheKey))
if err == nil {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(cv.Value)
return nil
}
//omitting code that get resources from DataStore
//cache this result with it's query as the key
memcache.Set(c, &memcache.Item{
Key: fmt.Sprint(cacheKey),
Value: result,
})
//return the json version
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(result)
return nil
}
示例4: fromCache
func fromCache(ctx context.Context, bucket, name string) (io.ReadCloser, error) {
item, err := memcache.Get(ctx, key(bucket, name))
if err != nil {
return nil, err
}
return ioutil.NopCloser(bytes.NewReader(item.Value)), nil
}
示例5: noConfusion
func noConfusion(res http.ResponseWriter, req *http.Request) {
var html string
// get cookie value
var id string
cookie, _ := req.Cookie("session-id")
if cookie != nil {
id = cookie.Value
html += `
<br>
<p>Value from cookie: ` + id + `</p>
`
}
// get memcache value
ctx := appengine.NewContext(req)
item, _ := memcache.Get(ctx, id)
if item != nil {
html += `
<br>
<p>
Value from memcache: ` + string(item.Value) + `
</p>
`
}
res.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprint(res, html)
}
示例6: handler
func handler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
// [START intro_2]
// Create an Item
item := &memcache.Item{
Key: "lyric",
Value: []byte("Oh, give me a home"),
}
// Add the item to the memcache, if the key does not already exist
if err := memcache.Add(ctx, item); err == memcache.ErrNotStored {
log.Infof(ctx, "item with key %q already exists", item.Key)
} else if err != nil {
log.Errorf(ctx, "error adding item: %v", err)
}
// Change the Value of the item
item.Value = []byte("Where the buffalo roam")
// Set the item, unconditionally
if err := memcache.Set(ctx, item); err != nil {
log.Errorf(ctx, "error setting item: %v", err)
}
// Get the item from the memcache
if item, err := memcache.Get(ctx, "lyric"); err == memcache.ErrCacheMiss {
log.Infof(ctx, "item not in the cache")
} else if err != nil {
log.Errorf(ctx, "error getting item: %v", err)
} else {
log.Infof(ctx, "the lyric is %q", item.Value)
}
// [END intro_2]
}
示例7: genCookie
func genCookie(res http.ResponseWriter, req *http.Request) *http.Cookie {
cookie, err := req.Cookie("session-id")
if err != nil {
cookie = newVisitor(req)
http.SetCookie(res, cookie)
return cookie
}
// make sure set cookie uses our current structure
if strings.Count(cookie.Value, "|") != 2 {
cookie = newVisitor(req)
http.SetCookie(res, cookie)
return cookie
}
if tampered(cookie.Value) {
cookie = newVisitor(req)
http.SetCookie(res, cookie)
return cookie
}
ctx := appengine.NewContext(req)
id := strings.Split(cookie.Value, "|")[0]
item, _ := memcache.Get(ctx, id)
if item == nil {
cookie = newVisitor(req)
http.SetCookie(res, cookie)
return cookie
}
return cookie
}
示例8: TestHook
func TestHook(t *testing.T) {
body := `{"bucket": "dummy", "name": "path/obj"}`
cacheKey := storage.CacheKey("dummy", "path/obj")
req, _ := testInstance.NewRequest("POST", "/-/hook/gcs", strings.NewReader(body))
ctx := appengine.NewContext(req)
item := &memcache.Item{Key: cacheKey, Value: []byte("ignored")}
if err := memcache.Set(ctx, item); err != nil {
t.Fatal(err)
}
// must remove cached item
res := httptest.NewRecorder()
http.DefaultServeMux.ServeHTTP(res, req)
if res.Code != http.StatusOK {
t.Errorf("res.Code = %d; want %d", res.Code, http.StatusOK)
}
if _, err := memcache.Get(ctx, cacheKey); err != memcache.ErrCacheMiss {
t.Fatalf("memcache.Get(%q): %v; want ErrCacheMiss", cacheKey, err)
}
// cache misses must not respond with an error code
req, _ = testInstance.NewRequest("POST", "/-/hook/gcs", strings.NewReader(body))
res = httptest.NewRecorder()
http.DefaultServeMux.ServeHTTP(res, req)
if res.Code != http.StatusOK {
t.Errorf("res.Code = %d; want %d", res.Code, http.StatusOK)
}
}
示例9: PrimaryPublicCertificates
// PrimaryPublicCertificates returns primary's PublicCertificates.
func PrimaryPublicCertificates(c context.Context, primaryURL string) (*PublicCertificates, error) {
cacheKey := fmt.Sprintf("pub_certs:%s", primaryURL)
var pubCerts []byte
setCache := false
item, err := memcache.Get(c, cacheKey)
if err != nil {
setCache = true
if err != memcache.ErrCacheMiss {
log.Warningf(c, "failed to get cert from cache: %v", err)
}
pubCerts, err = downloadCert(urlfetch.Client(c), primaryURL)
if err != nil {
log.Errorf(c, "failed to download cert: %v", err)
return nil, err
}
} else {
pubCerts = item.Value
}
pc := &PublicCertificates{}
if err = json.Unmarshal(pubCerts, pc); err != nil {
log.Errorf(c, "failed to unmarshal cert: %v %v", string(pubCerts), err)
return nil, err
}
if setCache {
err = memcache.Set(c, &memcache.Item{
Key: cacheKey,
Value: pubCerts,
Expiration: time.Hour,
})
if err != nil {
log.Warningf(c, "failed to set cert to cache: %v", err)
}
}
return pc, nil
}
示例10: get
func get(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
http.Redirect(w, r, WEBSITE, http.StatusFound)
return
}
// Use RequestURI instead of r.URL.Path, as we need the encoded form:
key := extractKey(r)
parts := strings.Split(key, "/")
if len(parts) == 3 {
context := appengine.NewContext(r)
item, err := memcache.Get(context, key)
if err == nil {
w.Header().Add("X-Content-Type-Options", "nosniff")
contentType, _ := url.QueryUnescape(parts[0])
if !imageTypes.MatchString(contentType) {
contentType = "application/octet-stream"
}
w.Header().Add("Content-Type", contentType)
w.Header().Add(
"Cache-Control",
fmt.Sprintf("public,max-age=%d", EXPIRATION_TIME),
)
w.Write(item.Value)
return
}
}
http.Error(w, "404 Not Found", http.StatusNotFound)
}
示例11: index
func index(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
// get cookie UUID, or set
cookie, _ := req.Cookie("sessionid")
if cookie == nil {
cookie = createCookie()
http.SetCookie(res, cookie)
}
// get memcache session data
item, _ := memcache.Get(ctx, cookie.Value)
if item == nil {
http.Redirect(res, req, "/login", 302)
return
}
// unmarshal
var m map[string]string
json.Unmarshal(item.Value, &m)
// authenticate
if m["loggedin"] == "false" || m["loggedin"] == nil {
http.Redirect(res, req, "/login", 302)
return
}
// upload photo
src, hdr, err := req.FormFile("data")
if req.Method == "POST" && err == nil {
uploadPhoto(src, hdr, m)
}
// save session
session.Save(req, res)
// get photos
data := getPhotos(session)
// execute template
tpl.ExecuteTemplate(res, "index.html", data)
}
示例12: getUser
func getUser(req *http.Request, name string) model {
ctx := appengine.NewContext(req)
//Dank Memcache
item, _ := memcache.Get(ctx, name)
var m model
if item != nil {
err := json.Unmarshal(item.Value, &m)
if err != nil {
fmt.Printf("error unmarhsalling: %v", err)
return model{}
}
}
//Datastore
var m2 model
key := datastore.NewKey(ctx, "Users", name, 0, nil)
err := datastore.Get(ctx, key, &m2)
if err == datastore.ErrNoSuchEntity {
return model{}
} else if err != nil {
return model{}
}
//Reset Dank Memecache
bs := marshalModel(m2)
item1 := memcache.Item{
Key: m2.Name,
Value: bs,
}
memcache.Set(ctx, &item1)
return m2
}
示例13: handleMessage
// return a message based on its id
func handleMessage(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
// get key from URL
key := strings.SplitN(req.URL.Path, "/", 3)[2]
// get item from memcache
item, err := memcache.Get(ctx, key)
if err != nil {
http.NotFound(res, req)
return
}
var secretKey [32]byte
bs, err := hex.DecodeString(req.FormValue("secret"))
if err != nil || len(bs) != 32 {
http.NotFound(res, req)
return
}
copy(secretKey[:], bs)
msg, err := decrypt(string(item.Value), secretKey)
if err != nil {
http.NotFound(res, req)
return
}
// if this is the first time the message is viewed
if item.Flags == 0 {
item.Expiration = 30 * time.Second
item.Flags = 1
memcache.Set(ctx, item)
}
res.Write([]byte(msg))
}
示例14: retrieveMemc
func retrieveMemc(id string, req *http.Request) (model, error) {
var m model
ctx := appengine.NewContext(req)
item, err := memcache.Get(ctx, id)
if err != nil {
// get data from datastore
log.Infof(ctx, "VALUE FROM DATASTORE: %s", err)
m, err = retrieveDstore(id, req)
if err != nil {
return m, err
}
// put data in memcache
storeMemc(m, id, req)
return m, nil
}
// unmarshal from JSON
log.Infof(ctx, "VALUE FROM MEMCACHE")
err = json.Unmarshal(item.Value, &m)
if err != nil {
log.Errorf(ctx, "ERROR retrieveMemc unmarshal: %s", err)
return m, err
}
return m, nil
}
示例15: fromCache
func fromCache(ctx context.Context, key *datastore.Key) (p *Passenger, err error) {
item, err := memcache.Get(ctx, key.Encode())
if err != nil {
return nil, err
}
p = new(Passenger)
err = gob.NewDecoder(bytes.NewReader(item.Value)).Decode(&p)
return
}