-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache.go
100 lines (82 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
package plc
import (
"errors"
"fmt"
"reflect"
"sync"
)
type Cache struct {
reader Reader
cache map[string]interface{}
mutex sync.RWMutex
}
var _ = Reader(&Cache{}) // Compiler makes sure this type is a Reader
// NewCache returns a Cache which caches the most recent value passed through it.
// Values are cached by reading them through NewCache as a Reader.
// Cached values are accessed through CacheReader.
func NewCache(reader Reader) *Cache {
return &Cache{
reader: reader,
cache: map[string]interface{}{},
}
}
func (r *Cache) ReadTag(name string, value interface{}) error {
err := r.reader.ReadTag(name, value)
if err != nil {
return fmt.Errorf("Cache: %w", err)
}
r.mutex.Lock()
r.cache[name] = reflect.Indirect(reflect.ValueOf(value)).Interface()
r.mutex.Unlock()
return nil
}
// ReadCachedTag acts the same as ReadTag, but returns the cached value.
// A read of a value not in the cache will return ErrTagNotFound.
func (r *Cache) ReadCachedTag(name string, value interface{}) error {
r.mutex.RLock()
cVal, ok := r.cache[name]
r.mutex.RUnlock()
if !ok {
return ErrTagNotFound{name}
}
val := reflect.ValueOf(value)
if val.Kind() != reflect.Ptr {
return ErrNonPointerRead{TagName: name, Kind: val.Kind()}
}
vToSet := val.Elem()
if !vToSet.CanSet() {
return errors.New("Provided value for tag '" + name + "' cannot be set")
}
vToSet.Set(reflect.ValueOf(cVal))
return nil
}
func (r *Cache) Keys() []string {
r.mutex.RLock()
defer r.mutex.RUnlock()
keys := make([]string, 0, len(r.cache))
for key := range r.cache {
keys = append(keys, key)
}
return keys
}
type CacheReader struct {
cache *Cache
}
// CacheReader returns a Reader which calls ReadCachedTag.
func (r *Cache) CacheReader() CacheReader {
return CacheReader{cache: r}
}
func (r CacheReader) ReadTag(name string, value interface{}) error {
err := r.cache.ReadCachedTag(name, value)
if err != nil {
return fmt.Errorf("CacheReader: %w", err)
}
return nil
}
type ErrTagNotFound struct {
Name string
}
func (err ErrTagNotFound) Error() string {
return "Cache tag '" + err.Name + "' could not be found"
}
func (err ErrTagNotFound) Unwrap() error { return ErrBadRequest }