-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache_script.go
87 lines (70 loc) · 1.58 KB
/
cache_script.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
package server
import "sync"
type cacheTypeScript struct {
lock *sync.RWMutex
all []Script
enabled []Script
byName map[string]int
byID map[string]int
}
// ScriptCache the script cache
var ScriptCache = &cacheTypeScript{lock: &sync.RWMutex{}}
// Update populate the script cache, will panic if not able to populate
func (c *cacheTypeScript) Update() {
c.lock.Lock()
defer c.lock.Unlock()
c.all = []Script{}
c.enabled = []Script{}
c.byName = map[string]int{}
c.byID = map[string]int{}
scripts := ScriptStore.AllScripts()
c.all = scripts
for i, script := range scripts {
if script.Enabled {
c.enabled = append(c.enabled, script)
}
c.byName[script.Name] = i
c.byID[script.ID] = i
}
log.Debug("Updated script cache")
}
// All get all scripts
func (c *cacheTypeScript) All() []Script {
c.lock.RLock()
defer c.lock.RUnlock()
all := make([]Script, len(c.all))
for i, script := range c.all {
all[i] = script
}
return all
}
// Enabled get all enabled scripts
func (c *cacheTypeScript) Enabled() []Script {
c.lock.RLock()
defer c.lock.RUnlock()
enabled := make([]Script, len(c.enabled))
for i, script := range c.enabled {
enabled[i] = script
}
return enabled
}
// ByName get an script by its name
func (c *cacheTypeScript) ByName(name string) *Script {
c.lock.RLock()
defer c.lock.RUnlock()
idx, k := c.byName[name]
if !k {
return nil
}
return &c.all[idx]
}
// ByID get an script by its ID
func (c *cacheTypeScript) ByID(id string) *Script {
c.lock.RLock()
defer c.lock.RUnlock()
idx, k := c.byID[id]
if !k {
return nil
}
return &c.all[idx]
}