-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
65 lines (57 loc) · 942 Bytes
/
helper.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
package ex
import (
"sync"
"os"
"time"
)
type exprCtx struct {
exx *XExpr
mt time.Time
}
var (
exprCtxCache map[string]*exprCtx
lock *sync.Mutex
)
func InitCache() {
if lock != nil {
return
}
lock = &sync.Mutex{}
exprCtxCache = make(map[string]*exprCtx)
}
func LoadFileFromCache(path string, vars map[string]interface{}) (ctx *XExpr, existing bool, err error) {
lock.Lock()
defer lock.Unlock()
exprC, ok := exprCtxCache[path]
if !ok {
ctx = New()
if err = ctx.LoadFile(path, vars); err != nil {
return
}
fi, _ := os.Stat(path)
exprC = &exprCtx{
exx: ctx,
mt: fi.ModTime(),
}
exprCtxCache[path] = exprC
return
}
fi, e := os.Stat(path)
if e != nil {
err = e
return
}
mt := fi.ModTime()
if !exprC.mt.Equal(mt) {
ctx = New()
if err = ctx.LoadFile(path, vars); err != nil {
return
}
exprC.exx = ctx
exprC.mt = mt
} else {
existing = true
ctx = exprC.exx
}
return
}