/
cache.go
254 lines (228 loc) · 7.93 KB
/
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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package iot
import (
"errors"
"fmt"
"github.com/SENERGY-Platform/platform-connector-lib/model"
"github.com/SENERGY-Platform/platform-connector-lib/security"
"github.com/SENERGY-Platform/platform-connector-lib/statistics"
"github.com/SENERGY-Platform/service-commons/pkg/cache"
"github.com/SENERGY-Platform/service-commons/pkg/cache/memcached"
"github.com/SENERGY-Platform/service-commons/pkg/signal"
"log"
"runtime/debug"
"sync"
"time"
)
type PreparedCache struct {
iot *Iot
cache *cache.Cache
deviceExpiration int32
deviceTypeExpiration int32
characteristicExpiration int32
Debug bool
protocol map[string]model.Protocol
mux sync.RWMutex
}
type Cache struct {
parent *PreparedCache
token security.JwtToken
}
func NewCache(iot *Iot, deviceExpiration int32, deviceTypeExpiration int32, characteristicExpiration int32, maxIdleConns int, timeout time.Duration, memcachedServer ...string) (*PreparedCache, error) {
cacheConf := cache.Config{
ReadCacheHook: func(duration time.Duration) {
statistics.CacheRead(duration)
},
CacheMissHook: func() {
statistics.CacheMiss()
},
CacheInvalidationSignalHooks: map[cache.Signal]cache.ToKey{
signal.Known.DeviceTypeCacheInvalidation: func(signalValue string) (cacheKey string) {
return "dt." + signalValue
},
signal.Known.CharacteristicCacheInvalidation: func(signalValue string) (cacheKey string) {
return characteristicCachePrefix + signalValue
},
},
}
if len(memcachedServer) > 0 {
cacheConf.L2Provider = memcached.NewProvider(maxIdleConns, timeout, memcachedServer...)
}
c, err := cache.New(cacheConf)
if err != nil {
return nil, err
}
return &PreparedCache{iot: iot, deviceExpiration: deviceExpiration, deviceTypeExpiration: deviceTypeExpiration, characteristicExpiration: characteristicExpiration, cache: c, protocol: map[string]model.Protocol{}}, nil
}
func (this *PreparedCache) WithToken(token security.JwtToken) *Cache {
return &Cache{parent: this, token: token}
}
func (this *PreparedCache) GetDevice(token security.JwtToken, id string) (result model.Device, err error) {
if this.deviceExpiration == 0 {
return this.iot.GetDevice(id, token)
}
pl, err := token.GetPayload()
if err != nil {
return result, err
}
result, err = cache.Use(this.cache, "device."+pl.UserId+"."+id, func() (model.Device, error) {
if this.Debug {
log.Printf("DEBUG: load device %v from repository\n", id)
}
return this.iot.GetDevice(id, token)
}, func(device model.Device) error {
if device.Id == "" {
return fmt.Errorf("missing device.id")
}
if device.DeviceTypeId == "" {
return fmt.Errorf("missing device.device_type_id")
}
return nil
}, time.Duration(this.deviceExpiration)*time.Second)
if err != nil {
return result, err
}
return result, err
}
func (this *PreparedCache) GetDeviceByLocalId(token security.JwtToken, deviceUrl string) (result model.Device, err error) {
if this.deviceExpiration == 0 {
return this.iot.GetDeviceByLocalId(deviceUrl, token)
}
pl, err := token.GetPayload()
if err != nil {
return result, err
}
return cache.Use(this.cache, "device_url."+pl.UserId+"."+deviceUrl, func() (model.Device, error) {
if this.Debug {
log.Printf("DEBUG: load device %v from repository\n", deviceUrl)
}
return this.iot.GetDeviceByLocalId(deviceUrl, token)
}, func(device model.Device) error {
if device.Id == "" {
return fmt.Errorf("missing device.id")
}
if device.DeviceTypeId == "" {
return fmt.Errorf("missing device.device_type_id")
}
return nil
}, time.Duration(this.deviceExpiration)*time.Second)
}
func (this *PreparedCache) CreateDevice(token security.JwtToken, device model.Device) (result model.Device, err error) {
result, err = this.iot.CreateDevice(device, token)
if err == nil {
this.cacheDevice(token, result)
}
return result, err
}
func (this *PreparedCache) cacheDevice(token security.JwtToken, device model.Device) (err error) {
var pl security.JwtPayload
pl, err = token.GetPayload()
if err != nil {
return err
}
if this.Debug {
log.Printf("DEBUG: cache device %#v", device)
}
err = this.cache.Set("device_url."+pl.UserId+"."+device.LocalId, device, time.Duration(this.deviceExpiration)*time.Second)
if err != nil {
return err
}
err = this.cache.Set("device."+pl.UserId+"."+device.Id, device, time.Duration(this.deviceExpiration)*time.Second)
if err != nil {
return err
}
return nil
}
func (this *PreparedCache) UpdateDevice(token security.JwtToken, device model.Device) (result model.Device, err error) {
result, err = this.iot.UpdateDevice(device, token)
if err == nil {
this.cacheDevice(token, device)
}
return
}
func (this *PreparedCache) CreateDeviceType(token security.JwtToken, deviceType model.DeviceType) (result model.DeviceType, err error) {
result, err = this.iot.CreateDeviceType(deviceType, token)
if err == nil {
this.cache.Set("dt."+result.Id, result, time.Duration(this.deviceTypeExpiration)*time.Second)
}
return
}
func (this *PreparedCache) UpdateDeviceType(token security.JwtToken, deviceType model.DeviceType) (result model.DeviceType, err error) {
result, err = this.iot.UpdateDeviceType(deviceType, token)
if err == nil {
this.cache.Set("dt."+result.Id, result, time.Duration(this.deviceTypeExpiration)*time.Second)
}
return
}
func (this *PreparedCache) EnsureLocalDeviceExistence(token security.JwtToken, device model.Device) (result model.Device, err error) {
result, err = this.GetDeviceByLocalId(token, device.LocalId)
if err == security.ErrorNotFound {
result, err = this.CreateDevice(token, device)
}
return
}
func (this *PreparedCache) GetDeviceType(token security.JwtToken, id string) (result model.DeviceType, err error) {
if id == "" {
debug.PrintStack()
log.Println("ERROR: on GetDeviceType() missing id")
return result, errors.New("missing id")
}
if this.deviceTypeExpiration == 0 {
return this.iot.GetDeviceType(id, token)
}
return cache.Use(this.cache, "dt."+id, func() (model.DeviceType, error) {
return this.iot.GetDeviceType(id, token)
}, func(deviceType model.DeviceType) error {
if deviceType.Id == "" {
return errors.New("missing device_type.id")
}
return nil
}, time.Duration(this.deviceTypeExpiration)*time.Second)
}
func (this *PreparedCache) GetProtocol(token security.JwtToken, id string) (result model.Protocol, err error) {
protocol, ok := this.readProtocolFromCache(id)
if ok {
return protocol, nil
}
protocol, err = this.iot.GetProtocol(id, token)
if err != nil {
return protocol, err
}
this.writeProtocolToCache(id, protocol)
return protocol, err
}
func (this *PreparedCache) readProtocolFromCache(id string) (result model.Protocol, ok bool) {
this.mux.RLock()
defer this.mux.RUnlock()
result, ok = this.protocol[id]
return
}
func (this *PreparedCache) writeProtocolToCache(id string, protocol model.Protocol) {
this.mux.Lock()
defer this.mux.Unlock()
this.protocol[id] = protocol
return
}
func (this *PreparedCache) GetCache() *cache.Cache {
return this.cache
}
func (this *PreparedCache) InvalidateDeviceTypeCache(deviceTypeId string) {
this.cache.Remove("dt." + deviceTypeId)
}
func (this *Cache) GetDevice(id string) (result model.Device, err error) {
return this.parent.GetDevice(this.token, id)
}
func (this *Cache) GetDeviceByLocalId(deviceUrl string) (result model.Device, err error) {
return this.parent.GetDeviceByLocalId(this.token, deviceUrl)
}
func (this *Cache) CreateDevice(device model.Device) (result model.Device, err error) {
return this.parent.CreateDevice(this.token, device)
}
func (this *Cache) EnsureLocalDeviceExistence(device model.Device) (result model.Device, err error) {
return this.parent.EnsureLocalDeviceExistence(this.token, device)
}
func (this *Cache) GetDeviceType(id string) (result model.DeviceType, err error) {
return this.parent.GetDeviceType(this.token, id)
}
func (this *Cache) GetProtocol(id string) (result model.Protocol, err error) {
return this.parent.GetProtocol(this.token, id)
}