Files
mattermost/services/cache/cache.go
Hector 62b57143c8 [MM 19840] Create a Cache Interface abstraction to support multiple cache backends (#13384)
* Refactor to use structured logging

* Properly formatted with gofmt

* created interface Cache, but construction of cache is still coupled.

* Implementing cache factories to build caches

* Simple redis implementation without error handling. Keys and values by default are string

* refactor NewLocalCacheLayer to inject cache factory

* Removed redis impl to focus on cache abstraction

* CacheFactory injected on sqlsupplier and saved in Store struct

* remove useless private method

* replace concrete declaration of lru cache to cache abstraction

* discard spaces

* Renamed factory to provider because concrete implementations of factories may hold an state (such as a redis client for example)

* refactor to include all caches in the same package cache and subpackages

* method close cache. This method will be used for concrete implementations that need to release resources (close a connection, etc)

* closing cacheprovider and releasing resources while closing sql store

* fixed merge conflict fail

* gofmt files

* remove unused property from post_store

* naming refactor to avoid stutter. Added godocs on interface

* Store doesnt know anything about the cache and provider. Cache provider will be built after loading config and injected in localCacheLayer

* fixed broken test

* cache provider initialized before RunOldAppInitialization which initializes the localcachelayer

* move statusCache to server to initialize it with the new cache provider

* update terms_service and channel_layer to have new cacheProvider

* gofmt

* Add Connect method to the cache provider

* mock cacheprovider in user_layer_test

Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com>
Co-authored-by: mattermod <mattermod@users.noreply.github.com>
2020-01-09 09:57:28 +01:00

61 lines
2.2 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package cache
import (
"time"
)
// Cache is a representation of any cache store that has keys and values
type Cache interface {
// Purge is used to completely clear the cache.
Purge()
// Add adds the given key and value to the store without an expiry.
Add(key, value interface{})
// AddWithDefaultExpires adds the given key and value to the store with the default expiry.
AddWithDefaultExpires(key, value interface{})
// AddWithExpiresInSecs adds the given key and value to the cache with the given expiry.
AddWithExpiresInSecs(key, value interface{}, expireAtSecs int64)
// 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.
Get(key interface{}) (value interface{}, ok bool)
// 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.
// This API intentionally deviates from the Add-only variants above for simplicity. We should simplify the entire API in the future.
GetOrAdd(key, value interface{}, ttl time.Duration) (actual interface{}, loaded bool)
// Remove deletes the value for a key.
Remove(key interface{})
// Keys returns a slice of the keys in the cache.
Keys() []interface{}
// Len returns the number of items in the cache.
Len() int
// Name identifies this cache instance among others in the system.
Name() string
// GetInvalidateClusterEvent returns the cluster event configured when this cache was created.
GetInvalidateClusterEvent() string
}
// Provider defines how to create new caches
type Provider interface {
// Connect opens a new connection to the cache using specific provider parameters.
Connect()
// NewCache creates a new cache with given size.
NewCache(size int) Cache
// NewCacheWithParams creates a new cache with the given parameters.
NewCacheWithParams(size int, name string, defaultExpiry int64, invalidateClusterEvent string) Cache
// Close releases any resources used by the cache provider.
Close()
}