This repository was archived by the owner on May 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcache.go
More file actions
107 lines (96 loc) · 2.38 KB
/
cache.go
File metadata and controls
107 lines (96 loc) · 2.38 KB
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
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/go-redis/redis/v8"
"github.com/kataras/iris/v12"
"time"
)
var cache *redis.Client
func SetupCache() {
cache = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", conf.Redis.Host, conf.Redis.Port),
Password: conf.Redis.Password,
DB: conf.Redis.DB,
})
_, err := cache.Ping(context.Background()).Result()
if err != nil {
logger.Fatal("Failed to connect to Redis server: ", err)
} else {
logger.Debug("Connected to Redis server successfully")
}
if conf.Debug {
cache.FlushAll(context.Background())
logger.Debug("Flushed all cache")
}
}
func SetCache(ctx iris.Context, key string, value string) error {
return cache.Set(ctx.Request().Context(), key, value, conf.Redis.Expiration*time.Second).Err()
}
func GetCache(ctx iris.Context, key string) (val string, ok bool) {
val, err := cache.Get(ctx.Request().Context(), key).Result()
if err == redis.Nil {
return "", false
} else if err != nil {
return "", false
} else if val == "" {
return "", false
}
return val, true
}
func SetJSONCache(ctx iris.Context, key string, value interface{}) error {
val, err := json.Marshal(value)
if err != nil {
return err
}
return SetCache(ctx, key, string(val))
}
func GetJSONCache(ctx iris.Context, key string) (value AnalysisData, ok bool) {
val, ok := GetCache(ctx, key)
if !ok {
return AnalysisData{}, false
}
err := json.Unmarshal([]byte(val), &value)
if err != nil {
return AnalysisData{}, false
}
return value, true
}
// CachedHandler is a decorator for handlers to enable caching their response.
func CachedHandler(h iris.Handler) iris.Handler {
return func(ctx iris.Context) {
path := ctx.Path()
data, ok := GetJSONCache(ctx, path)
if ok {
if conf.Debug {
logger.Debugf("Hit cache of %s", path)
}
EndBody(ctx, data)
} else {
h(ctx)
}
}
}
func EndBody(ctx iris.Context, data AnalysisData) {
if data.Err != "" {
ThrowError(ctx, data.Err, data.Code)
} else {
err := ctx.JSON(data.Data)
if err != nil {
return
}
}
}
func EndBodyWithCache(ctx iris.Context, data AnalysisData) {
err := SetJSONCache(ctx, ctx.Path(), data)
if conf.Debug {
logger.Debugf("Set cache of %s", ctx.Path())
}
if err != nil {
logger.Errorf("Failed to set cache: %s", err.Error())
ThrowError(ctx, err.Error(), iris.StatusInternalServerError)
return
}
EndBody(ctx, data)
}