-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_proxy.go
39 lines (30 loc) · 852 Bytes
/
cache_proxy.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
package cache_proxy_demo
type CacheProxy interface {
Execute(qryOption any, readModelType any) (readModel any, err error)
}
type DatabaseGetFunc func(qryOption any) (readModel any, err error)
type TransformQryOptionToCacheKey func(qryOption any) (key string)
type BaseCacheProxy struct {
Transform TransformQryOptionToCacheKey
Cache Cache
GetDB DatabaseGetFunc
}
func (proxy *BaseCacheProxy) Execute(qryOption any, readModelType any) (readModel any, err error) {
key := proxy.Transform(qryOption)
// cache.get
val, err := proxy.Cache.GetValue(key, readModelType)
if err == nil {
return val, nil
}
// db.get
readModel, err = proxy.GetDB(qryOption)
if err != nil {
return readModelType, err
}
// cache.set
err = proxy.Cache.SetValue(key, readModel)
if err != nil {
return readModel, err
}
return readModel, nil
}