forked from GoogleCloudPlatform/golang-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
memcache.go
52 lines (42 loc) · 1.33 KB
/
memcache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2011 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package sample
import (
"net/http"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
)
// [START intro_1]
import "google.golang.org/appengine/memcache"
// [END intro_1]
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]
}