-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnewStore.go
61 lines (46 loc) · 1.44 KB
/
newStore.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
package carbon
import (
"os"
"regexp"
"sync"
"time"
)
// NewCarbonStore creates and returns a new CarbonStore instance.
// If a valid `cleanFrequency` is provided, it starts a goroutine to periodically clean expired keys from the store.
func NewCarbonStore(cleanFrequency time.Duration) *CarbonStore {
z := make(chan struct{})
s := CarbonStore{store: sync.Map{}, stopChan: z}
if cleanFrequency != NoClean {
go s.cleanStore(cleanFrequency)
}
return &s
}
// ImportStoreFromFile reads a file and extracts key-value pairs in the format {key=value} using a regular expression.
// The key-value pairs found in the file are then set in the store with no expiry time (NoExpiry) or a Default expiry time.
// It also starts a cleaner goroutine if `cleanFrequency` is provided.
func ImportStoreFromFile(filepath string, cleanFrequency time.Duration, defaultExpiry time.Duration) (*CarbonStore, error) {
file, err := os.ReadFile(filepath)
if err != nil {
return nil, err
}
z := make(chan struct{})
s := CarbonStore{store: sync.Map{}, stopChan: z}
pattern := `\{(\w+)=(\w+)\}`
re := regexp.MustCompile(pattern)
matches := re.FindAllStringSubmatch(string(file), -1)
var ss time.Duration
if defaultExpiry == NoExpiry {
ss = NoExpiry
}else{
ss = defaultExpiry
}
for _, match := range matches {
if len(match) == 3 {
s.Set(match[1], match[2], ss)
}
}
if cleanFrequency != NoClean {
go s.cleanStore(cleanFrequency)
}
return &s, nil
}