-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
caches.go
176 lines (144 loc) · 3.58 KB
/
caches.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
package caches
import (
"sync"
"gorm.io/gorm"
)
type Caches struct {
callbacks map[queryType]func(db *gorm.DB)
Conf *Config
queue *sync.Map
}
type Config struct {
Easer bool
Cacher Cacher
}
func (c *Caches) Name() string {
return "gorm:caches"
}
func (c *Caches) Initialize(db *gorm.DB) error {
if c.Conf == nil {
c.Conf = &Config{
Easer: false,
Cacher: nil,
}
}
if c.Conf.Easer {
c.queue = &sync.Map{}
}
callbacks := make(map[queryType]func(db *gorm.DB), 4)
callbacks[uponQuery] = db.Callback().Query().Get("gorm:query")
callbacks[uponCreate] = db.Callback().Create().Get("gorm:query")
callbacks[uponUpdate] = db.Callback().Update().Get("gorm:query")
callbacks[uponDelete] = db.Callback().Delete().Get("gorm:query")
c.callbacks = callbacks
if err := db.Callback().Query().Replace("gorm:query", c.query); err != nil {
return err
}
if err := db.Callback().Create().Replace("gorm:query", c.getMutatorCb(uponCreate)); err != nil {
return err
}
if err := db.Callback().Update().Replace("gorm:query", c.getMutatorCb(uponUpdate)); err != nil {
return err
}
if err := db.Callback().Delete().Replace("gorm:query", c.getMutatorCb(uponDelete)); err != nil {
return err
}
return nil
}
// query is a decorator around the default "gorm:query" callback
// it takes care to both ease database load and cache results
func (c *Caches) query(db *gorm.DB) {
if c.Conf.Easer == false && c.Conf.Cacher == nil {
c.callbacks[uponQuery](db)
return
}
identifier := buildIdentifier(db)
if c.checkCache(db, identifier) {
return
}
c.ease(db, identifier)
if db.Error != nil {
return
}
c.storeInCache(db, identifier)
if db.Error != nil {
return
}
}
// getMutatorCb returns a decorator which calls the Cacher's Invalidate method
func (c *Caches) getMutatorCb(typ queryType) func(db *gorm.DB) {
return func(db *gorm.DB) {
if c.Conf.Cacher != nil {
if err := c.Conf.Cacher.Invalidate(db.Statement.Context); err != nil {
_ = db.AddError(err)
}
}
if cb := c.callbacks[typ]; cb != nil { // By default, gorm has no callbacks associated with mutating behaviors
cb(db)
}
}
}
func (c *Caches) ease(db *gorm.DB, identifier string) {
if c.Conf.Easer == false {
c.callbacks[uponQuery](db)
return
}
res := ease(&queryTask{
id: identifier,
db: db,
queryCb: c.callbacks[uponQuery],
}, c.queue).(*queryTask)
if db.Error != nil {
return
}
if res.db.Statement.Dest == db.Statement.Dest {
return
}
detachedQuery := &Query[any]{
Dest: db.Statement.Dest,
RowsAffected: db.Statement.RowsAffected,
}
easedQuery := &Query[any]{
Dest: res.db.Statement.Dest,
RowsAffected: res.db.Statement.RowsAffected,
}
if err := easedQuery.copyTo(detachedQuery); err != nil {
_ = db.AddError(err)
}
detachedQuery.replaceOn(db)
}
func (c *Caches) checkCache(db *gorm.DB, identifier string) bool {
if c.Conf.Cacher != nil {
res, err := c.Conf.Cacher.Get(db.Statement.Context, identifier, &Query[any]{
Dest: db.Statement.Dest,
RowsAffected: db.Statement.RowsAffected,
})
if err != nil {
_ = db.AddError(err)
}
if res != nil {
res.replaceOn(db)
return true
}
}
return false
}
func (c *Caches) storeInCache(db *gorm.DB, identifier string) {
if c.Conf.Cacher != nil {
err := c.Conf.Cacher.Store(db.Statement.Context, identifier, &Query[any]{
Dest: db.Statement.Dest,
RowsAffected: db.Statement.RowsAffected,
})
if err != nil {
_ = db.AddError(err)
}
}
}
// queryType is used to mark callbacks
type queryType int
const (
uponQuery queryType = iota
uponCreate
uponUpdate
uponDelete
)