-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoda.go
92 lines (76 loc) · 1.7 KB
/
soda.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
package soda
import (
"encoding/json"
"errors"
"fmt"
"github.com/spaolacci/murmur3"
"github.com/wunderlist/ttlcache"
"github.com/gin-gonic/gin"
"net/http"
"strings"
"time"
)
var cache *ttlcache.Cache
type response struct {
Header http.Header
Data []byte
Status int
ContentType string
}
//SetRespCache ...
//save resp to cache
func SetRespCache(c *gin.Context, data []byte) error {
if c.Request.Method != "GET" {
return errors.New("request type is not GET")
}
if c.Writer.Status() != 200 {
return errors.New("response code not 200")
}
key := keyFromRequest(c.Request)
resp := &response{}
resp.Header = c.Writer.Header()
resp.Data = data
resp.Status = c.Writer.Status()
resp.ContentType = c.ContentType()
value, err := json.Marshal(resp)
if err != nil {
cache.Set(key, string(value))
}
return nil
}
//SodaMiddleware ...
//returns a middleware which overrides the call
//and returns data from cache
func SodaMiddleware() gin.HandlerFunc {
cache = ttlcache.NewCache(time.Second * 60)
return func(c *gin.Context) {
//pass if request method not GET
if c.Request.Method != "GET" {
return
}
key := keyFromRequest(c.Request)
value, keyExists := cache.Get(key)
if keyExists {
if value != "" {
return
}
var resp response
err := json.Unmarshal([]byte(value), resp)
if err != nil {
cache.Set(key, "")
return
}
for key, value := range resp.Header {
c.Header(key, strings.Join(value, ", "))
}
c.Data(resp.Status, resp.ContentType, resp.Data)
c.Abort()
}
}
}
func keyFromRequest(request *http.Request) string {
requestURI := request.RequestURI
hash := murmur3.Sum64([]byte(requestURI))
key := fmt.Sprint(hash)
return key
}