-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
73 lines (57 loc) · 1.25 KB
/
string.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
package api
import (
"fmt"
"strconv"
"github.com/mmirolim/gokvc/cache"
"github.com/valyala/fasthttp"
)
func get(ctx *fasthttp.RequestCtx) {
key := ctx.QueryArgs().PeekBytes(PKEY)
if v, ok := cache.GET(key); ok {
ctx.SetBody(v)
return
}
ctx.SetStatusCode(fasthttp.StatusNotFound)
}
func set(ctx *fasthttp.RequestCtx) {
var ttl int
args := ctx.QueryArgs()
key := args.PeekBytes(PKEY)
val := args.PeekBytes(PVAL)
if args.HasBytes(PTTL) {
ttl = args.GetUintOrZero("t")
}
if cache.SET(key, val, ttl) {
ctx.SetBody(OK)
return
}
ctx.SetStatusCode(fasthttp.StatusBadRequest)
}
func del(ctx *fasthttp.RequestCtx) {
key := ctx.QueryArgs().PeekBytes(PKEY)
if cache.DEL(key) {
ctx.SetBody(OK)
return
}
ctx.SetStatusCode(fasthttp.StatusNotFound)
}
func sttl(ctx *fasthttp.RequestCtx) {
key := ctx.QueryArgs().PeekBytes(PKEY)
r := cache.TTL(key)
if r == cache.KeyNotExistCode {
ctx.SetStatusCode(fasthttp.StatusNotFound)
}
// do not make alloc for common ttl codes
v, ok := cache.TtlKeyCodes[r]
if ok {
ctx.SetBody(v)
return
}
ctx.SetBody([]byte(strconv.Itoa(r)))
}
func skeys(ctx *fasthttp.RequestCtx) {
fmt.Fprintf(ctx, "%s", cache.KEYS())
}
func slen(ctx *fasthttp.RequestCtx) {
ctx.SetBody([]byte(strconv.Itoa(cache.LEN())))
}