forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
211 lines (184 loc) · 5.52 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
/*
* 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"
"fmt"
"os"
"strconv"
"time"
"github.com/allegro/bigcache"
"github.com/pydio/cells/common/dao"
"github.com/pydio/cells/common/proto/activity"
"github.com/pydio/cells/common/utils/cache"
"github.com/pydio/cells/x/configx"
"github.com/pydio/cells/x/jsonx"
)
func WithCache(dao DAO) DAO {
cacheConfig := bigcache.DefaultConfig(5 * time.Minute)
cacheConfig.Shards = 64
cacheConfig.MaxEntriesInWindow = 10 * 60 * 64
cacheConfig.MaxEntrySize = 200
cacheConfig.HardMaxCacheSize = 8
if limit := os.Getenv("CELLS_CACHES_HARD_LIMIT"); limit != "" {
if l, e := strconv.ParseInt(limit, 10, 64); e == nil {
if l < 8 {
fmt.Println("[ENV] ## WARNING ## CELLS_CACHES_HARD_LIMIT cannot use a value lower than 8 (MB).")
} else {
cacheConfig.HardMaxCacheSize = int(l)
}
}
}
useBatch := false
if _, o := dao.(batchDAO); o {
useBatch = true
}
return &Cache{
dao: dao,
cache: cache.NewInstrumentedCache("activities", cacheConfig),
useBatch: useBatch,
}
}
type Cache struct {
dao DAO
cache *cache.InstrumentedCache
useBatch bool
done chan bool
input chan *batchActivity
inner []*batchActivity
}
func (c *Cache) Init(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(values)
}
func (c *Cache) startBatching() {
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) GetConn() dao.Conn {
return c.dao.GetConn()
}
func (c *Cache) SetConn(conn dao.Conn) {
c.dao.SetConn(conn)
}
func (c *Cache) CloseConn() error {
if c.useBatch {
c.stopBatching()
}
return c.dao.CloseConn()
}
func (c *Cache) Driver() string {
return c.dao.Driver()
}
func (c *Cache) Prefix() string {
return c.dao.Prefix()
}
func (c *Cache) PostActivity(ownerType activity.OwnerType, ownerId string, boxName BoxName, object *activity.Object, publishCtx context.Context) error {
if !c.useBatch {
return c.dao.PostActivity(ownerType, ownerId, boxName, object, publishCtx)
} else {
c.input <- &batchActivity{
Object: object,
ownerType: ownerType,
ownerId: ownerId,
boxName: boxName,
publishCtx: publishCtx,
}
return nil
}
}
func (c *Cache) UpdateSubscription(subscription *activity.Subscription) error {
// Clear cache
c.cache.Delete(subscription.ObjectType.String() + "-" + subscription.ObjectId)
return c.dao.UpdateSubscription(subscription)
}
func (c *Cache) ListSubscriptions(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, e := c.cache.Get(k); e == nil {
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(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(userId string) int {
return c.dao.CountUnreadForUser(userId)
}
func (c *Cache) ActivitiesFor(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(ownerType, ownerId, boxName, refBoxOffset, reverseOffset, limit, result, done)
}
func (c *Cache) StoreLastUserInbox(userId string, boxName BoxName, last []byte, activityId string) error {
return c.dao.StoreLastUserInbox(userId, boxName, last, activityId)
}
func (c *Cache) Delete(ownerType activity.OwnerType, ownerId string) error {
return c.dao.Delete(ownerType, ownerId)
}
func (c *Cache) Purge(logger func(string), ownerType activity.OwnerType, ownerId string, boxName BoxName, minCount, maxCount int, updatedBefore time.Time, compactDB, clearBackup bool) error {
return c.dao.Purge(logger, ownerType, ownerId, boxName, minCount, maxCount, updatedBefore, compactDB, clearBackup)
}