2020-01-09 03:57:28 -05:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
|
// See LICENSE.txt for license information.
|
|
|
|
|
|
|
|
|
|
package cache
|
|
|
|
|
|
|
|
|
|
import (
|
2020-07-18 04:01:06 -04:00
|
|
|
"errors"
|
2020-01-09 03:57:28 -05:00
|
|
|
"time"
|
2021-07-15 01:32:17 -04:00
|
|
|
|
2023-06-11 01:24:35 -04:00
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
2020-01-09 03:57:28 -05:00
|
|
|
)
|
|
|
|
|
|
2020-07-18 04:01:06 -04:00
|
|
|
// ErrKeyNotFound is the error when the given key is not found
|
|
|
|
|
var ErrKeyNotFound = errors.New("key not found")
|
|
|
|
|
|
|
|
|
|
// Cache is a representation of a cache store that aims to replace cache.Cache
|
2020-01-09 03:57:28 -05:00
|
|
|
type Cache interface {
|
|
|
|
|
// Purge is used to completely clear the cache.
|
2020-07-18 04:01:06 -04:00
|
|
|
Purge() error
|
2020-01-09 03:57:28 -05:00
|
|
|
|
2020-07-18 04:01:06 -04:00
|
|
|
// SetWithDefaultExpiry adds the given key and value to the store with the default expiry. If
|
2022-01-19 23:37:27 -05:00
|
|
|
// the key already exists, it will overwrite the previous value
|
2022-07-05 02:46:50 -04:00
|
|
|
SetWithDefaultExpiry(key string, value any) error
|
2020-01-09 03:57:28 -05:00
|
|
|
|
2020-07-18 04:01:06 -04:00
|
|
|
// SetWithExpiry adds the given key and value to the cache with the given expiry. If the key
|
2022-01-19 23:37:27 -05:00
|
|
|
// already exists, it will overwrite the previous value
|
2022-07-05 02:46:50 -04:00
|
|
|
SetWithExpiry(key string, value any, ttl time.Duration) error
|
2020-01-09 03:57:28 -05:00
|
|
|
|
2020-07-18 04:01:06 -04:00
|
|
|
// Get the content stored in the cache for the given key, and decode it into the value interface.
|
2024-08-13 04:48:25 -04:00
|
|
|
// Returns ErrKeyNotFound if the key is missing from the cache
|
2022-07-05 02:46:50 -04:00
|
|
|
Get(key string, value any) error
|
2020-01-09 03:57:28 -05:00
|
|
|
|
2024-08-13 04:48:25 -04:00
|
|
|
// GetMulti returns values for multiple keys in a single operation.
|
|
|
|
|
// Returns ErrKeyNotFound if the key is missing from the cache.
|
2024-08-05 23:58:41 -04:00
|
|
|
GetMulti(keys []string, values []any) []error
|
|
|
|
|
|
2020-07-18 04:01:06 -04:00
|
|
|
// Remove deletes the value for a given key.
|
|
|
|
|
Remove(key string) error
|
2020-01-09 03:57:28 -05:00
|
|
|
|
2024-08-13 04:48:25 -04:00
|
|
|
// RemoveMulti deletes multiple keys in a single operation.
|
|
|
|
|
RemoveMulti(keys []string) error
|
|
|
|
|
|
|
|
|
|
// Scan allows incremental iteration over the entire key-space
|
|
|
|
|
// in a performant manner. It provides a callback that consumers
|
|
|
|
|
// can use to process the keys. If the callback returns an error,
|
|
|
|
|
// the scan stops, returning the same error.
|
|
|
|
|
Scan(f func([]string) error) error
|
2020-01-09 03:57:28 -05:00
|
|
|
|
|
|
|
|
// GetInvalidateClusterEvent returns the cluster event configured when this cache was created.
|
2021-07-15 01:32:17 -04:00
|
|
|
GetInvalidateClusterEvent() model.ClusterEvent
|
2020-01-09 03:57:28 -05:00
|
|
|
|
2020-07-18 04:01:06 -04:00
|
|
|
// Name returns the name of the cache
|
|
|
|
|
Name() string
|
2020-01-09 03:57:28 -05:00
|
|
|
}
|
2024-09-19 00:08:38 -04:00
|
|
|
|
|
|
|
|
// ExternalCache is a super-set of the Cache interface with
|
|
|
|
|
// a couple of more methods that allows for more efficient cache updates.
|
|
|
|
|
// This can be achieved because the cache is external and an update
|
|
|
|
|
// is visible to all nodes.
|
|
|
|
|
type ExternalCache interface {
|
|
|
|
|
Cache
|
|
|
|
|
// Increment will increment the
|
|
|
|
|
// number stored at that key by the value.
|
|
|
|
|
Increment(key string, val int) error
|
|
|
|
|
// Decrement will decrement the
|
|
|
|
|
// number stored at that key by the value.
|
|
|
|
|
Decrement(key string, val int) error
|
|
|
|
|
}
|