forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache.go
114 lines (92 loc) · 2.16 KB
/
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
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
108
109
110
111
112
113
114
package util
import (
"fmt"
"maps"
"slices"
"sync"
"github.com/evcc-io/evcc/util/encode"
)
// Cache is a data store
type Cache struct {
mu sync.RWMutex
val map[string]Param
}
// flush is the value type used as parameter for flushing the cache.
// Flushing is implemented by closing the channel. At this time, it is guaranteed
// that the cache has catched up processing all pending messages.
type flush chan struct{}
// Flusher returns a new flush channel
func Flusher() flush {
return make(flush)
}
// NewCache creates cache
func NewCache() *Cache {
return &Cache{
val: make(map[string]Param),
}
}
// Run adds input channel's values to cache
func (c *Cache) Run(in <-chan Param) {
log := NewLogger("cache")
for p := range in {
if flushC, ok := p.Val.(flush); ok {
close(flushC)
continue
}
key := p.Key
if p.Loadpoint != nil {
key = fmt.Sprintf("lp-%d/%s", *p.Loadpoint+1, key)
}
log.TRACE.Printf("%s: %v", key, p.Val)
c.Add(p.UniqueID(), p)
}
}
// State provides a structured copy of the cached values.
// Loadpoints are aggregated as loadpoints array.
// Result values are formatted using encoder.
func (c *Cache) State(enc encode.Encoder) map[string]any {
c.mu.RLock()
defer c.mu.RUnlock()
res := make(map[string]any)
lps := make(map[int]map[string]any)
for _, param := range c.val {
if param.Loadpoint == nil {
res[param.Key] = enc.Encode(param.Val)
} else {
lp, ok := lps[*param.Loadpoint]
if !ok {
lp = make(map[string]any)
lps[*param.Loadpoint] = lp
}
lp[param.Key] = enc.Encode(param.Val)
}
}
// convert map to array
loadpoints := make([]map[string]any, len(lps))
for id, lp := range lps {
loadpoints[id] = lp
}
res["loadpoints"] = loadpoints
return res
}
// All provides a copy of the cached values
func (c *Cache) All() []Param {
c.mu.RLock()
defer c.mu.RUnlock()
return slices.Collect(maps.Values(c.val))
}
// Add entry to cache
func (c *Cache) Add(key string, param Param) {
c.mu.Lock()
defer c.mu.Unlock()
c.val[key] = param
}
// Get entry from cache
func (c *Cache) Get(key string) Param {
c.mu.RLock()
defer c.mu.RUnlock()
if val, ok := c.val[key]; ok {
return val
}
return Param{}
}