-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod_Cache.go
58 lines (47 loc) · 1.06 KB
/
mod_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 main
import (
"log"
"os"
"strings"
"sync"
"time"
)
type Cache struct {
Mapping map[string]string
mux sync.Mutex
}
var cache = Cache{}
func (c *Cache) Update(filePath string) {
content := ReadFile(filePath)
lines := strings.Split(strings.Replace(content, "\r\n", "\n", -1), "\n")
c.mux.Lock()
c.Mapping = make(map[string]string)
for _, e := range lines {
parts := strings.Split(e, "=")
if (len(parts) == 2) && (parts[0] != "") && (parts[1] != "") { //TODO ADD TRUE VALIDATION
c.Mapping[parts[0]] = parts[1]
} else {
log.Println("File structure mapping.txt is incorrect")
}
}
c.mux.Unlock()
}
// CacheAutoUpdater - Update cache
func CacheAutoUpdater(filePath string) error {
go cache.Update(filePath)
initialStat, err := os.Stat(filePath)
if err != nil {
log.Fatal(err)
}
for {
stat, err := os.Stat(filePath)
if err != nil {
log.Fatal(err)
}
if stat.Size() != initialStat.Size() || stat.ModTime() != initialStat.ModTime() {
go cache.Update(filePath)
initialStat = stat
}
time.Sleep(3 * time.Second)
}
}