/
config_cache.go
58 lines (48 loc) · 1.33 KB
/
config_cache.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
package configcat
import (
"fmt"
"time"
)
// ConfigCache is a cache API used to make custom cache implementations.
type ConfigCache interface {
// get reads the configuration from the cache.
Get(key string) (string, error)
// set writes the configuration into the cache.
Set(key string, value string) error
}
type configCache interface {
// get reads the configuration from the cache.
get(key string) (*config, error)
// set writes the configuration into the cache.
set(key string, value *config) error
}
type inMemoryConfigCache map[string]*config
// get reads the configuration from the cache.
func (cache inMemoryConfigCache) get(key string) (*config, error) {
return cache[key], nil
}
// set writes the configuration into the cache.
func (cache inMemoryConfigCache) set(key string, value *config) error {
cache[key] = value
return nil
}
func adaptCache(c ConfigCache) configCache {
return &cacheAdaptor{c}
}
type cacheAdaptor struct {
c ConfigCache
}
func (c *cacheAdaptor) get(key string) (*config, error) {
val, err := c.c.Get(key)
if err != nil {
return nil, err
}
conf, err := parseConfig([]byte(val), "", time.Time{})
if err != nil {
return nil, fmt.Errorf("cannot parse config from cache: %v", err)
}
return conf, nil
}
func (c *cacheAdaptor) set(key string, value *config) error {
return c.c.Set(key, value.body())
}