forked from jkp717/infinitive
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache.go
51 lines (40 loc) · 925 Bytes
/
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
package main
import (
"reflect"
"sync"
)
type cacheMapType map[string]interface{}
type Cache struct {
cacheMap cacheMapType
cacheMutex sync.Mutex
}
var wsCache Cache = Cache{ cacheMap: make(cacheMapType) }
var mqttCache Cache = Cache{ cacheMap: make(cacheMapType) }
func (c *Cache) update(name string, data interface{}) {
c.cacheMutex.Lock()
defer c.cacheMutex.Unlock()
old := c.cacheMap[name]
if !reflect.DeepEqual(old, data) {
Dispatcher.broadcastEvent(name, data)
c.cacheMap[name] = data
}
}
func (c *Cache) get(name string) interface{} {
c.cacheMutex.Lock()
defer c.cacheMutex.Unlock()
return c.cacheMap[name]
}
func (c *Cache) clear() {
c.cacheMutex.Lock()
defer c.cacheMutex.Unlock()
c.cacheMap = make(cacheMapType)
}
func (c *Cache) dump() cacheMapType {
c.cacheMutex.Lock()
defer c.cacheMutex.Unlock()
n := make(cacheMapType)
for k, v := range c.cacheMap {
n[k] = v
}
return n
}