Skip to content

Commit c7f7bef

Browse files
liusy182mattermod
andauthored
move cache2 package to cache (mattermost#14921)
Co-authored-by: Mattermod <[email protected]>
1 parent 3f46cf6 commit c7f7bef

22 files changed

+110
-577
lines changed

api4/apitestlib.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func setupTestHelper(dbStore store.Store, searchEngine *searchengine.Broker, ent
9898
}
9999
if includeCache {
100100
// Adds the cache layer to the test store
101-
s.Store = localcachelayer.NewLocalCacheLayer(s.Store, s.Metrics, s.Cluster, s.CacheProvider2)
101+
s.Store = localcachelayer.NewLocalCacheLayer(s.Store, s.Metrics, s.Cluster, s.CacheProvider)
102102
}
103103

104104
th := &TestHelper{

api4/openGraph.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import (
88
"time"
99

1010
"github.com/mattermost/mattermost-server/v5/model"
11-
"github.com/mattermost/mattermost-server/v5/services/cache2"
11+
"github.com/mattermost/mattermost-server/v5/services/cache"
1212
)
1313

1414
const OPEN_GRAPH_METADATA_CACHE_SIZE = 10000
1515

16-
var openGraphDataCache = cache2.NewLRU(&cache2.LRUOptions{
16+
var openGraphDataCache = cache.NewLRU(&cache.LRUOptions{
1717
Size: OPEN_GRAPH_METADATA_CACHE_SIZE,
1818
})
1919

api4/system.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"github.com/mattermost/mattermost-server/v5/audit"
1717
"github.com/mattermost/mattermost-server/v5/mlog"
1818
"github.com/mattermost/mattermost-server/v5/model"
19-
"github.com/mattermost/mattermost-server/v5/services/cache2"
19+
"github.com/mattermost/mattermost-server/v5/services/cache"
2020
"github.com/mattermost/mattermost-server/v5/services/filesstore"
2121
)
2222

@@ -26,7 +26,7 @@ const (
2626
MAX_SERVER_BUSY_SECONDS = 86400
2727
)
2828

29-
var redirectLocationDataCache = cache2.NewLRU(&cache2.LRUOptions{
29+
var redirectLocationDataCache = cache.NewLRU(&cache.LRUOptions{
3030
Size: REDIRECT_LOCATION_CACHE_SIZE,
3131
})
3232

app/helper_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func setupTestHelper(dbStore store.Store, enterprise bool, includeCacheLayer boo
7676

7777
if includeCacheLayer {
7878
// Adds the cache layer to the test store
79-
s.Store = localcachelayer.NewLocalCacheLayer(s.Store, s.Metrics, s.Cluster, s.CacheProvider2)
79+
s.Store = localcachelayer.NewLocalCacheLayer(s.Store, s.Metrics, s.Cluster, s.CacheProvider)
8080
}
8181

8282
th := &TestHelper{

app/post.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"github.com/mattermost/mattermost-server/v5/mlog"
1616
"github.com/mattermost/mattermost-server/v5/model"
1717
"github.com/mattermost/mattermost-server/v5/plugin"
18-
"github.com/mattermost/mattermost-server/v5/services/cache2"
18+
"github.com/mattermost/mattermost-server/v5/services/cache"
1919
"github.com/mattermost/mattermost-server/v5/store"
2020
"github.com/mattermost/mattermost-server/v5/utils"
2121
)
@@ -121,7 +121,7 @@ func (a *App) deduplicateCreatePost(post *model.Post) (foundPost *model.Post, er
121121
// it hasn't previously been seen.
122122
var postId string
123123
nErr := a.Srv().seenPendingPostIdsCache.Get(post.PendingPostId, &postId)
124-
if nErr == cache2.ErrKeyNotFound {
124+
if nErr == cache.ErrKeyNotFound {
125125
a.Srv().seenPendingPostIdsCache.SetWithExpiry(post.PendingPostId, unknownPostId, PENDING_POST_IDS_CACHE_TTL)
126126
return nil, nil
127127
}

app/post_metadata.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/dyatlov/go-opengraph/opengraph"
1818
"github.com/mattermost/mattermost-server/v5/mlog"
1919
"github.com/mattermost/mattermost-server/v5/model"
20-
"github.com/mattermost/mattermost-server/v5/services/cache2"
20+
"github.com/mattermost/mattermost-server/v5/services/cache"
2121
"github.com/mattermost/mattermost-server/v5/utils/imgutils"
2222
"github.com/mattermost/mattermost-server/v5/utils/markdown"
2323
)
@@ -31,7 +31,7 @@ const LINK_CACHE_SIZE = 10000
3131
const LINK_CACHE_DURATION = 1 * time.Hour
3232
const MaxMetadataImageSize = MaxOpenGraphResponseSize
3333

34-
var linkCache = cache2.NewLRU(&cache2.LRUOptions{
34+
var linkCache = cache.NewLRU(&cache.LRUOptions{
3535
Size: LINK_CACHE_SIZE,
3636
})
3737

app/server.go

+10-22
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ import (
3636
"github.com/mattermost/mattermost-server/v5/model"
3737
"github.com/mattermost/mattermost-server/v5/plugin"
3838
"github.com/mattermost/mattermost-server/v5/services/cache"
39-
"github.com/mattermost/mattermost-server/v5/services/cache/lru"
40-
"github.com/mattermost/mattermost-server/v5/services/cache2"
4139
"github.com/mattermost/mattermost-server/v5/services/filesstore"
4240
"github.com/mattermost/mattermost-server/v5/services/httpservice"
4341
"github.com/mattermost/mattermost-server/v5/services/imageproxy"
@@ -110,9 +108,9 @@ type Server struct {
110108
newStore func() store.Store
111109

112110
htmlTemplateWatcher *utils.HTMLTemplateWatcher
113-
sessionCache cache2.Cache
114-
seenPendingPostIdsCache cache2.Cache
115-
statusCache cache2.Cache
111+
sessionCache cache.Cache
112+
seenPendingPostIdsCache cache.Cache
113+
statusCache cache.Cache
116114
configListenerId string
117115
licenseListenerId string
118116
logListenerId string
@@ -163,8 +161,6 @@ type Server struct {
163161

164162
CacheProvider cache.Provider
165163

166-
CacheProvider2 cache2.Provider
167-
168164
tracer *tracing.Tracer
169165
timestampLastDiagnosticSent time.Time
170166
}
@@ -247,22 +243,18 @@ func NewServer(options ...Option) (*Server, error) {
247243

248244
// at the moment we only have this implementation
249245
// in the future the cache provider will be built based on the loaded config
250-
s.CacheProvider = new(lru.CacheProvider)
251-
252-
s.CacheProvider.Connect()
253-
254-
s.CacheProvider2 = cache2.NewProvider()
255-
if err := s.CacheProvider2.Connect(); err != nil {
246+
s.CacheProvider = cache.NewProvider()
247+
if err := s.CacheProvider.Connect(); err != nil {
256248
return nil, errors.Wrapf(err, "Unable to connect to cache provider")
257249
}
258250

259-
s.sessionCache = s.CacheProvider2.NewCache(&cache2.CacheOptions{
251+
s.sessionCache = s.CacheProvider.NewCache(&cache.CacheOptions{
260252
Size: model.SESSION_CACHE_SIZE,
261253
})
262-
s.seenPendingPostIdsCache = s.CacheProvider2.NewCache(&cache2.CacheOptions{
254+
s.seenPendingPostIdsCache = s.CacheProvider.NewCache(&cache.CacheOptions{
263255
Size: PENDING_POST_IDS_CACHE_SIZE,
264256
})
265-
s.statusCache = s.CacheProvider2.NewCache(&cache2.CacheOptions{
257+
s.statusCache = s.CacheProvider.NewCache(&cache.CacheOptions{
266258
Size: model.STATUS_CACHE_SIZE,
267259
})
268260

@@ -303,7 +295,7 @@ func NewServer(options ...Option) (*Server, error) {
303295
s.sqlStore,
304296
s.Metrics,
305297
s.Cluster,
306-
s.CacheProvider2,
298+
s.CacheProvider,
307299
),
308300
s.SearchEngine,
309301
s.Config(),
@@ -702,11 +694,7 @@ func (s *Server) Shutdown() error {
702694
}
703695

704696
if s.CacheProvider != nil {
705-
s.CacheProvider.Close()
706-
}
707-
708-
if s.CacheProvider2 != nil {
709-
if err = s.CacheProvider2.Close(); err != nil {
697+
if err = s.CacheProvider.Close(); err != nil {
710698
mlog.Error("Unable to cleanly shutdown cache", mlog.Err(err))
711699
}
712700
}

migrations/helper_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func setupTestHelper(enterprise bool) *TestHelper {
4747
panic(err)
4848
}
4949
// Adds the cache layer to the test store
50-
s.Store = localcachelayer.NewLocalCacheLayer(s.Store, s.Metrics, s.Cluster, s.CacheProvider2)
50+
s.Store = localcachelayer.NewLocalCacheLayer(s.Store, s.Metrics, s.Cluster, s.CacheProvider)
5151

5252
th := &TestHelper{
5353
App: app.New(app.ServerConnector(s)),

services/cache/cache.go

+24-35
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,46 @@
44
package cache
55

66
import (
7+
"errors"
78
"time"
89
)
910

10-
// Cache is a representation of any cache store that has keys and values
11+
// ErrKeyNotFound is the error when the given key is not found
12+
var ErrKeyNotFound = errors.New("key not found")
13+
14+
// Cache is a representation of a cache store that aims to replace cache.Cache
1115
type Cache interface {
1216
// Purge is used to completely clear the cache.
13-
Purge()
14-
15-
// Add adds the given key and value to the store without an expiry.
16-
Add(key string, value interface{})
17+
Purge() error
1718

18-
// AddWithDefaultExpires adds the given key and value to the store with the default expiry.
19-
AddWithDefaultExpires(key string, value interface{})
19+
// Set adds the given key and value to the store without an expiry. If the key already exists,
20+
// it will overwrite the previous value.
21+
Set(key string, value interface{}) error
2022

21-
// AddWithExpiresInSecs adds the given key and value to the cache with the given expiry.
22-
AddWithExpiresInSecs(key string, value interface{}, expireAtSecs int64)
23+
// SetWithDefaultExpiry adds the given key and value to the store with the default expiry. If
24+
// the key already exists, it will overwrite the previoous value
25+
SetWithDefaultExpiry(key string, value interface{}) error
2326

24-
// Get returns the value stored in the cache for a key, or nil if no value is present. The ok result indicates whether value was found in the cache.
25-
Get(key string) (value interface{}, ok bool)
27+
// SetWithExpiry adds the given key and value to the cache with the given expiry. If the key
28+
// already exists, it will overwrite the previoous value
29+
SetWithExpiry(key string, value interface{}, ttl time.Duration) error
2630

27-
// GetOrAdd returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.
28-
// This API intentionally deviates from the Add-only variants above for simplicity. We should simplify the entire API in the future.
29-
GetOrAdd(key string, value interface{}, ttl time.Duration) (actual interface{}, loaded bool)
31+
// Get the content stored in the cache for the given key, and decode it into the value interface.
32+
// Return ErrKeyNotFound if the key is missing from the cache
33+
Get(key string, value interface{}) error
3034

31-
// Remove deletes the value for a key.
32-
Remove(key string)
35+
// Remove deletes the value for a given key.
36+
Remove(key string) error
3337

3438
// Keys returns a slice of the keys in the cache.
35-
Keys() []string
39+
Keys() ([]string, error)
3640

3741
// Len returns the number of items in the cache.
38-
Len() int
39-
40-
// Name identifies this cache instance among others in the system.
41-
Name() string
42+
Len() (int, error)
4243

4344
// GetInvalidateClusterEvent returns the cluster event configured when this cache was created.
4445
GetInvalidateClusterEvent() string
45-
}
4646

47-
// Provider defines how to create new caches
48-
type Provider interface {
49-
// Connect opens a new connection to the cache using specific provider parameters.
50-
Connect()
51-
52-
// NewCache creates a new cache with given size.
53-
NewCache(size int) Cache
54-
55-
// NewCacheWithParams creates a new cache with the given parameters.
56-
NewCacheWithParams(size int, name string, defaultExpiry int64, invalidateClusterEvent string) Cache
57-
58-
// Close releases any resources used by the cache provider.
59-
Close()
47+
// Name returns the name of the cache
48+
Name() string
6049
}

services/cache2/lru.go services/cache/lru.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
22
// See LICENSE.txt for license information.
33

4-
package cache2
4+
package cache
55

66
import (
77
"container/list"

0 commit comments

Comments
 (0)