This repository has been archived by the owner on Apr 18, 2020. It is now read-only.
forked from trickstercache/trickster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
106 lines (89 loc) · 3.17 KB
/
redis.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
/**
* Copyright 2018 Comcast Cable Communications Management, LLC
* 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.
*/
package main
import (
"sync"
"time"
"github.com/go-kit/kit/log/level"
"github.com/go-redis/redis"
)
// RedisCache represents a redis cache object that conforms to the Cache interface
type RedisCache struct {
T *TricksterHandler
Config RedisCacheConfig
client *redis.Client
CacheKeys sync.Map
}
// Connect connects to the configured Redis endpoint
func (r *RedisCache) Connect() error {
level.Info(r.T.Logger).Log("event", "connecting to redis", "protocol", r.Config.Protocol, "Endpoint", r.Config.Endpoint)
r.client = redis.NewClient(&redis.Options{
Network: r.Config.Protocol,
Addr: r.Config.Endpoint,
})
if r.Config.Password != "" {
r.client.Options().Password = r.Config.Password
}
go r.Reap()
return r.client.Ping().Err()
}
// Store places the the data into the Redis Cache using the provided Key and TTL
func (r *RedisCache) Store(cacheKey string, data string, ttl int64) error {
level.Debug(r.T.Logger).Log("event", "redis cache store", "key", cacheKey)
return r.client.Set(cacheKey, data, time.Second*time.Duration(ttl)).Err()
}
// Retrieve gets data from the Redis Cache using the provided Key
func (r *RedisCache) Retrieve(cacheKey string) (string, error) {
level.Debug(r.T.Logger).Log("event", "redis cache retrieve", "key", cacheKey)
return r.client.Get(cacheKey).Result()
}
// Reap continually iterates through the cache to find expired elements and removes them
func (r *RedisCache) Reap() {
for {
r.ReapOnce()
time.Sleep(time.Duration(r.T.Config.Caching.ReapSleepMS) * time.Millisecond)
}
}
// ReapOnce makes a single iteration through the Response Channels to remove orphaned channels due to Redis Cache Expiration
func (r *RedisCache) ReapOnce() {
var keys []string
r.T.ChannelCreateMtx.Lock()
for key := range r.T.ResponseChannels {
keys = append(keys, key)
}
r.T.ChannelCreateMtx.Unlock()
if len(keys) > 0 {
results, err := r.client.MGet(keys...).Result()
if err != nil {
level.Debug(r.T.Logger).Log("event", "error fetching values in bulk from redis cache", "MGetDetail", err)
}
for i, key := range keys {
if results[i] == nil {
level.Debug(r.T.Logger).Log("event", "redis cache reap", "key", key)
r.T.ChannelCreateMtx.Lock()
// Close out the channel if it exists
if _, ok := r.T.ResponseChannels[key]; ok {
close(r.T.ResponseChannels[key])
delete(r.T.ResponseChannels, key)
}
r.T.ChannelCreateMtx.Unlock()
}
}
}
}
// Close disconnects from the Redis Cache
func (r *RedisCache) Close() error {
level.Info(r.T.Logger).Log("event", "closing redis connection")
r.client.Close()
return nil
}