-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpcache.go
168 lines (145 loc) · 3.52 KB
/
httpcache.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
//"bytes"
"container/list"
"errors"
"flag"
"github.com/julienschmidt/httprouter"
"io/ioutil"
"log"
"net/http"
_ "net/http/pprof"
"runtime"
"runtime/debug"
"time"
)
var cachesize = flag.Int("cachesize", 1024000000, "Max cache size in bytes")
type Cache struct {
// Maxsize is the max size of the cache in bytes
maxSize int
curSize int
// ll is the least-recently used list. objects in the back are evicted
lru *list.List
// cache is the actual cache lookup map.
cache map[interface{}]*list.Element
}
type Key interface{}
type entry struct {
Key Key
Value *[]byte
}
func NewCache(maxSize int) *Cache {
return &Cache{
maxSize: maxSize,
curSize: 0,
lru: list.New(),
cache: make(map[interface{}]*list.Element),
}
}
func (c *Cache) Add(key Key, value *[]byte) {
for (c.curSize + len(*value)) > c.maxSize {
log.Printf("CACHE_DEL: Removing oldest")
c.RemoveOldest()
}
if ele, ok := c.cache[key]; ok {
log.Printf("CACHE_ADD: Readding: %s", key)
c.curSize = c.curSize + len(*value)
c.lru.MoveToFront(ele)
ele.Value.(*entry).Value = value
return
}
log.Printf("CACHE_ADD: Adding: %s", key)
ele := c.lru.PushFront(&entry{key, value})
//c.lru.PushFront(&entry{key, value})
c.cache[key] = ele
c.curSize = c.curSize + len(*value)
}
func (c *Cache) Get(key Key) (*[]byte, error) {
if ele, ok := c.cache[key]; ok {
c.lru.MoveToFront(ele)
return ele.Value.(*entry).Value, nil
}
return nil, errors.New("Key not found")
}
func (c *Cache) Remove(key Key) {
if ele, ok := c.cache[key]; ok {
c.removeElement(ele)
}
}
func (c *Cache) RemoveOldest() {
ele := c.lru.Back()
if ele != nil {
c.removeElement(ele)
}
}
func (c *Cache) removeElement(ele *list.Element) {
c.curSize = c.curSize - len(*ele.Value.(*entry).Value)
c.lru.Remove(ele)
e := ele.Value.(*entry)
delete(c.cache, e.Key)
}
func (c *Cache) Len() int {
return c.lru.Len()
}
func (c *Cache) Size() int {
return c.curSize
}
func (c *Cache) PUT(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
key := ps.ByName("key")
data, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
c.Add(key, &data)
log.Printf("Added object: %s", key)
log.Printf("Cache size: %d", c.Size())
log.Printf("Cache length: %d", c.Len())
}
func (c *Cache) GET(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
//time.Sleep(1 * time.Second)
key := ps.ByName("key")
data, err := c.Get(key)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(*data)
//log.Printf("Served object: %s", key)
}
func (c *Cache) WIPE(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
for c.Len() > 0 {
c.RemoveOldest()
}
runtime.GC()
}
func (c *Cache) GC(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
for k := range c.cache {
log.Println("Keys: ", k)
}
runtime.GC()
}
func GoRuntimeStats() {
for {
m := &runtime.MemStats{}
log.Println("# goroutines: ", runtime.NumGoroutine())
runtime.ReadMemStats(m)
log.Println("Memory Acquired: ", m.Sys)
log.Println("Memory Used : ", m.Alloc)
time.Sleep(2 * time.Second)
}
}
func main() {
flag.Parse()
log.Println("Default GC: ", debug.SetGCPercent(10), "New GC: 10")
go GoRuntimeStats()
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
c := NewCache(*cachesize)
router := httprouter.New()
router.POST("/wipe", c.WIPE)
router.POST("/gc", c.GC)
router.GET("/*key", c.GET)
router.PUT("/*key", c.PUT)
log.Fatal(http.ListenAndServe(":8080", router))
}