-
Notifications
You must be signed in to change notification settings - Fork 1
/
caching.go
84 lines (71 loc) · 1.76 KB
/
caching.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
package main
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
)
// Cache is the adapter contract between the application and the cache library.
type Cache interface {
Put(key string, value interface{})
PutAll(map[string]interface{})
Get(key string) interface{}
GetAll(keys []string) map[string]interface{}
Clean(key string)
CleanAll()
}
// RedisCache holds a Redis connection pool.
type RedisCache struct {
conn *redis.Client
ctx context.Context
}
// GetCachingMechanism initializes and returns a caching mechanism.
func GetCachingMechanism() Cache {
cch := &RedisCache{
conn: redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
}),
}
cch.ctx = context.Background()
return cch
}
// Put adds an entry in the cache.
func (rc *RedisCache) Put(key string, value interface{}) {
if err := rc.conn.Set(rc.ctx, key, value, 0); err != nil {
fmt.Println(err)
}
}
// PutAll adds the entries of a map in the cache.
func (rc *RedisCache) PutAll(entries map[string]interface{}) {
for k, v := range entries {
rc.Put(k, v)
}
}
// Get gets an entry from the cache.
func (rc *RedisCache) Get(key string) interface{} {
value, err := rc.conn.Get(rc.ctx, key).Result()
if err != nil {
fmt.Println(err)
return ""
}
return value
}
// GetAll gets all the entries of a map from the cache.
func (rc *RedisCache) GetAll(keys []string) map[string]interface{} {
entries := make(map[string]interface{})
for _, k := range keys {
entries[k] = rc.Get(k)
}
return entries
}
// Clean cleans a entry from the cache.
func (rc *RedisCache) Clean(key string) {
if err := rc.conn.Del(rc.ctx, key); err != nil {
fmt.Println(err)
}
}
// CleanAll cleans the entire cache.
func (rc *RedisCache) CleanAll() {
rc.conn.FlushDB(rc.ctx)
}