This repository has been archived by the owner on May 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memcache.go
79 lines (66 loc) · 1.95 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// 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 memcache implements a simple memcache-alike.
package memcache
import (
"io"
"net/http"
"strconv"
"sync"
"appengine"
)
func init() {
http.HandleFunc("/_ah/start", handleStart)
http.HandleFunc("/_ah/stop", handleStop)
}
var (
mu sync.Mutex
cache map[string][]byte
)
func handleStart(w http.ResponseWriter, r *http.Request) {
// This handler is executed when an instance is started.
// If it responds with a HTTP 2xx or 404 response then it is ready to go.
// Otherwise, the instance is terminated and restarted.
// The instance will receive traffic after this handler returns.
c := appengine.NewContext(r)
http.HandleFunc("/memcache/get", handleGet)
http.HandleFunc("/memcache/set", handleSet)
cache = make(map[string][]byte)
c.Infof("Memcache module started.")
io.WriteString(w, "OK")
}
func handleStop(w http.ResponseWriter, r *http.Request) {
// This handler is executed when an instance is being shut down.
// It has 30s before it will be terminated.
// When this is called, no new requests will reach the instance.
c := appengine.NewContext(r)
c.Infof("Memcache module stopped.")
}
func handleGet(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
key := r.FormValue("key")
mu.Lock()
value, ok := cache[key]
mu.Unlock()
if !ok {
w.WriteHeader(http.StatusNoContent)
c.Infof("No data for key %q", key)
return
}
w.Header().Set("Content-Length", strconv.Itoa(len(value)))
w.Write(value)
c.Infof("Returned %q => %q", key, value)
}
func handleSet(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
c := appengine.NewContext(r)
key, value := r.FormValue("key"), r.FormValue("value")
mu.Lock()
cache[key] = []byte(value)
mu.Unlock()
c.Infof("Stored %q => %q", key, value)
}