-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_repo.go
72 lines (59 loc) · 1.35 KB
/
redis_repo.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
package provider
import (
"context"
"fmt"
"os"
"time"
"github.com/redis/go-redis/v9"
)
type RedisCacheRepository struct {
rdb *redis.Client
}
func (r *RedisCacheRepository) IsFlagStatusCached(
ctx context.Context,
params *cacheParameters,
) (bool, error) {
cacheKey := genFlagStatusCacheKey(params)
result, err := r.rdb.Exists(ctx, cacheKey).Result()
if err != nil {
return false, err
}
return result >= 1, nil
}
func (r *RedisCacheRepository) GetFlagStatus(
ctx context.Context,
params *cacheParameters,
) (bool, error) {
cacheKey := genFlagStatusCacheKey(params)
return r.rdb.Get(ctx, cacheKey).Bool()
}
func (r *RedisCacheRepository) CacheFlagStatus(
ctx context.Context,
params *cacheParameters,
status bool,
) error {
// The TTL in seconds of the keys stored in the Redis cache.
cacheTTL, _ := time.ParseDuration(os.Getenv("FLAGGER_CACHE_TTL"))
cacheKey := genFlagStatusCacheKey(params)
return r.rdb.SetEx(
ctx,
cacheKey,
status,
cacheTTL,
).Err()
}
// getFlagStatusCacheKey generates the cache key used for caching the flag
// status.
func genFlagStatusCacheKey(params *cacheParameters) string {
return fmt.Sprintf(
"status:%v:%v:%v",
params.ProjectKey,
params.EnvironmentName,
params.FlagName,
)
}
func NewProviderCacheRepository(rdb *redis.Client) *RedisCacheRepository {
return &RedisCacheRepository{
rdb: rdb,
}
}