-
Notifications
You must be signed in to change notification settings - Fork 0
/
flagcache.go
52 lines (45 loc) · 998 Bytes
/
flagcache.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
package main
import (
"errors"
"sync"
)
// FlagCache maintains a copy of the flags we pulled from the storage driver, with
// pre-parsed policies.
type FlagCache struct {
mu sync.RWMutex
flags map[string]*Flag
}
// NewFlagCache initializes the FlagCache
func NewFlagCache() (*FlagCache, error) {
fc := FlagCache{}
fc.flags = make(map[string]*Flag)
return &fc, nil
}
// Get a flag by name
func (fc *FlagCache) Get(flagname string) (*Flag, error) {
fc.mu.RLock()
flag, ok := fc.flags[flagname]
fc.mu.RUnlock()
if ok {
return flag, nil
}
return nil, errors.New("flag not found")
}
// Upsert replaces an existing flag
func (fc *FlagCache) Upsert(flag *Flag) error {
fc.mu.Lock()
fc.flags[flag.Name] = flag
fc.mu.Unlock()
return nil
}
// Delete removes a flag
func (fc *FlagCache) Delete(flagname string) error {
fc.mu.Lock()
delete(fc.flags, flagname)
fc.mu.Unlock()
return nil
}
// List gets all flags
func (fc *FlagCache) List() map[string]*Flag {
return fc.flags
}