-
Notifications
You must be signed in to change notification settings - Fork 3
/
options.go
147 lines (135 loc) · 4.26 KB
/
options.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
// Copyright 2020 Ye Zi Jie. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: FishGoddess
// Email: fishgoddess@qq.com
// Created at 2021/04/05 15:58:16
package cachego
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
// Option is a function which initializes cache.
type Option func(cache *Cache)
// WithMapSize is an option setting initializing map size of cache.
func WithMapSize(mapSize int) Option {
return func(cache *Cache) {
cache.mapSize = mapSize
}
}
// WithSegmentSize is an option setting initializing segment size of cache.
func WithSegmentSize(segmentSize int) Option {
return func(cache *Cache) {
cache.segmentSize = segmentSize
}
}
// WithAutoGC is an option turning on automatically gc.
func WithAutoGC(gcDuration time.Duration) Option {
return func(cache *Cache) {
cache.AutoGc(gcDuration)
}
}
// debugPointHandler returns debug http handler.
func debugPointHandler(cache *Cache) http.Handler {
// for responding Json
writeAsJson := func(writer http.ResponseWriter, data map[string]interface{}) {
dataBytes, err := json.Marshal(data)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
writer.Write([]byte(fmt.Sprintf("err: %+v\ndata: %+v", err, data)))
return
}
writer.Header().Set("Content-Type", "application/json; charset=UTF-8")
writer.Write(dataBytes)
}
mux := http.NewServeMux()
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
writeAsJson(writer, map[string]interface{}{
"info": "Welcome to cachego debug point!",
"cache": map[string]interface{}{
"mapSize": cache.mapSize,
"segmentSize": cache.segmentSize,
},
"version": Version,
"time": time.Now().Format("2006-01-02 15:04:05"),
"points": map[string]string{
"/get": "Get a value from cache, ex: /get?key=test",
"/remove": "Remove a value from cache, ex: /remove?key=test",
"/size": "Get size of cache",
"/gc": "Do gc task",
"/detail": "List all segments' detail",
},
})
})
mux.HandleFunc("/get", func(writer http.ResponseWriter, request *http.Request) {
key := request.URL.Query().Get("key")
data, ok := cache.Get(key)
writeAsJson(writer, map[string]interface{}{
"data": data,
"ok": ok,
})
})
mux.HandleFunc("/remove", func(writer http.ResponseWriter, request *http.Request) {
key := request.URL.Query().Get("key")
cache.Remove(key)
writeAsJson(writer, map[string]interface{}{
"ok": true,
})
})
mux.HandleFunc("/size", func(writer http.ResponseWriter, request *http.Request) {
writeAsJson(writer, map[string]interface{}{
"size": cache.Size(),
})
})
mux.HandleFunc("/gc", func(writer http.ResponseWriter, request *http.Request) {
cache.Gc()
writeAsJson(writer, map[string]interface{}{
"ok": true,
})
})
mux.HandleFunc("/detail", func(writer http.ResponseWriter, request *http.Request) {
data := make(map[string]interface{}, cache.segmentSize)
for i, segment := range cache.segments {
values := make(map[string]interface{}, len(segment.data))
for k, v := range segment.data {
values[k] = map[string]interface{}{
"data": v.data,
"ttl": v.ttl,
"ctime": v.ctime.Format("2006-01-02 15:04:05"),
"alive": v.alive(),
}
}
data[fmt.Sprintf("segment.%d", i)] = map[string]interface{}{
"size": segment.aliveSize,
"values": values,
}
}
writeAsJson(writer, data)
})
return mux
}
// WithDebugPoint runs a http server and registers some handlers for debug.
// Don't use it in production!
func WithDebugPoint(address string) Option {
return func(cache *Cache) {
go func() {
err := http.ListenAndServe(address, debugPointHandler(cache))
if err != nil {
panic(fmt.Errorf("WithDebugPoint() failed to listen on address [%s] due to %s", address, err.Error()))
}
}()
}
}