-
Notifications
You must be signed in to change notification settings - Fork 167
/
cache.go
197 lines (172 loc) · 5.58 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
/*
* Copyright (c) 2019-2021. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package activity
import (
"context"
"time"
"github.com/pydio/cells/v4/common/proto/activity"
"github.com/pydio/cells/v4/common/runtime"
"github.com/pydio/cells/v4/common/utils/cache"
"github.com/pydio/cells/v4/common/utils/configx"
"github.com/pydio/cells/v4/common/utils/jsonx"
)
func WithCache(dao DAO) DAO {
useBatch := false
if _, o := dao.(batchDAO); o {
useBatch = true
}
c, _ := cache.OpenCache(context.TODO(), runtime.CacheURL("activities", "evictionTime", "5m"))
return &Cache{
DAO: dao,
cache: c,
useBatch: useBatch,
}
}
type Cache struct {
DAO
cache cache.Cache
useBatch bool
done chan bool
input chan *batchActivity
inner []*batchActivity
closed bool
}
func (c *Cache) Init(ctx context.Context, values configx.Values) error {
if c.useBatch {
c.done = make(chan bool)
c.input = make(chan *batchActivity)
c.inner = make([]*batchActivity, 0, 500)
go c.startBatching()
}
return c.DAO.Init(ctx, values)
}
func (c *Cache) startBatching() {
defer func() {
c.closed = true
close(c.input)
}()
for {
select {
case a := <-c.input:
c.inner = append(c.inner, a)
if len(c.inner) >= 500 {
c.flushBatch()
}
case <-time.After(5 * time.Second):
c.flushBatch()
case <-c.done:
c.flushBatch()
return
}
}
}
func (c *Cache) stopBatching() {
close(c.done)
}
func (c *Cache) flushBatch() {
if len(c.inner) == 0 {
return
}
c.DAO.(batchDAO).BatchPost(c.inner)
c.inner = c.inner[:0]
}
func (c *Cache) CloseConn(ctx context.Context) error {
if c.useBatch {
c.stopBatching()
}
return c.DAO.CloseConn(ctx)
}
func (c *Cache) PostActivity(ctx context.Context, ownerType activity.OwnerType, ownerId string, boxName BoxName, object *activity.Object, publish bool) error {
if !c.useBatch || c.closed {
return c.DAO.PostActivity(ctx, ownerType, ownerId, boxName, object, publish)
} else {
var publishCtx context.Context
if publish {
publishCtx = runtime.ForkContext(context.Background(), ctx)
}
c.input <- &batchActivity{
Object: object,
ownerType: ownerType,
ownerId: ownerId,
boxName: boxName,
publishCtx: publishCtx,
}
return nil
}
}
func (c *Cache) UpdateSubscription(ctx context.Context, subscription *activity.Subscription) error {
// Clear cache
c.cache.Delete(subscription.ObjectType.String() + "-" + subscription.ObjectId)
return c.DAO.UpdateSubscription(ctx, subscription)
}
func (c *Cache) ListSubscriptions(ctx context.Context, objectType activity.OwnerType, objectIds []string) (res []*activity.Subscription, e error) {
var filtered []string
toCache := make(map[string][]*activity.Subscription)
for _, id := range objectIds {
// We'll cache an empty slice by default
toCache[id] = []*activity.Subscription{}
k := objectType.String() + "-" + id
if v, ok := c.cache.GetBytes(k); ok {
var subs []*activity.Subscription
if e := jsonx.Unmarshal(v, &subs); e == nil {
res = append(res, subs...)
continue
}
}
filtered = append(filtered, id)
}
ss, e := c.DAO.ListSubscriptions(ctx, objectType, filtered)
if e != nil {
return
}
res = append(res, ss...)
for _, s := range res {
toCache[s.ObjectId] = append(toCache[s.ObjectId], s)
}
for i, t := range toCache {
if data, e := jsonx.Marshal(t); e == nil {
c.cache.Set(objectType.String()+"-"+i, data)
}
}
return
}
func (c *Cache) CountUnreadForUser(ctx context.Context, userId string) int {
return c.DAO.CountUnreadForUser(ctx, userId)
}
func (c *Cache) ActivitiesFor(ctx context.Context, ownerType activity.OwnerType, ownerId string, boxName BoxName, refBoxOffset BoxName, reverseOffset int64, limit int64, result chan *activity.Object, done chan bool) error {
return c.DAO.ActivitiesFor(ctx, ownerType, ownerId, boxName, refBoxOffset, reverseOffset, limit, result, done)
}
func (c *Cache) StoreLastUserInbox(ctx context.Context, userId string, boxName BoxName, activityId string) error {
return c.DAO.StoreLastUserInbox(ctx, userId, boxName, activityId)
}
func (c *Cache) Delete(ctx context.Context, ownerType activity.OwnerType, ownerId string) error {
return c.DAO.Delete(ctx, ownerType, ownerId)
}
func (c *Cache) Purge(ctx context.Context, logger func(string, int), ownerType activity.OwnerType, ownerId string, boxName BoxName, minCount, maxCount int, updatedBefore time.Time, compactDB, clearBackup bool) error {
return c.DAO.Purge(ctx, logger, ownerType, ownerId, boxName, minCount, maxCount, updatedBefore, compactDB, clearBackup)
}
// AllActivities is used for internal migrations only
func (c *Cache) allActivities(ctx context.Context) (chan *docActivity, int, error) {
return c.DAO.allActivities(ctx)
}
// AllSubscriptions is used for internal migrations only
func (c *Cache) allSubscriptions(ctx context.Context) (chan *activity.Subscription, int, error) {
return c.DAO.allSubscriptions(ctx)
}