implement and use striped LRU cache to lower mutex contention (#15764)
* Implement Striped LRU cache * ci * fix Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
96d76760f0
Коммит
349b83f23a
24
services/cache/lru.go
поставляемый
24
services/cache/lru.go
поставляемый
@@ -9,21 +9,22 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/model"
|
||||
|
||||
"github.com/tinylib/msgp/msgp"
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
)
|
||||
|
||||
// LRU is a thread-safe fixed size LRU cache.
|
||||
type LRU struct {
|
||||
name string
|
||||
lock sync.RWMutex
|
||||
size int
|
||||
len int
|
||||
currentGeneration int64
|
||||
evictList *list.List
|
||||
items map[string]*list.Element
|
||||
lock sync.RWMutex
|
||||
defaultExpiry time.Duration
|
||||
name string
|
||||
invalidateClusterEvent string
|
||||
currentGeneration int64
|
||||
len int
|
||||
}
|
||||
|
||||
// LRUOptions contains options for initializing LRU cache
|
||||
@@ -32,6 +33,9 @@ type LRUOptions struct {
|
||||
Size int
|
||||
DefaultExpiry time.Duration
|
||||
InvalidateClusterEvent string
|
||||
// StripedBuckets is used only by LRUStriped and shouldn't be greater than the number
|
||||
// of CPUs available on the machine running this cache.
|
||||
StripedBuckets int
|
||||
}
|
||||
|
||||
// entry is used to hold a value in the evictList.
|
||||
@@ -43,7 +47,7 @@ type entry struct {
|
||||
}
|
||||
|
||||
// NewLRU creates an LRU of the given size.
|
||||
func NewLRU(opts *LRUOptions) Cache {
|
||||
func NewLRU(opts LRUOptions) Cache {
|
||||
return &LRU{
|
||||
name: opts.Name,
|
||||
size: opts.Size,
|
||||
@@ -141,19 +145,15 @@ func (l *LRU) set(key string, value interface{}, ttl time.Duration) error {
|
||||
|
||||
var buf []byte
|
||||
var err error
|
||||
|
||||
// We use a fast path for hot structs.
|
||||
if msgpVal, ok := value.(msgp.Marshaler); ok {
|
||||
buf, err = msgpVal.MarshalMsg(nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// Slow path for other structs.
|
||||
buf, err = msgpack.Marshal(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
l.lock.Lock()
|
||||
|
||||
149
services/cache/lru_striped.go
поставляемый
Обычный файл
149
services/cache/lru_striped.go
поставляемый
Обычный файл
@@ -0,0 +1,149 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/cespare/xxhash/v2"
|
||||
)
|
||||
|
||||
// LRUStriped keeps LRU caches in buckets in order to lower mutex contention.
|
||||
// This is achieved by hashing the input key to map it to a dedicated bucket.
|
||||
// Each bucket (an LRU cache) has its own lock that helps distributing the lock
|
||||
// contention on multiple threads/cores, leading to less wait times.
|
||||
//
|
||||
// LRUStriped implements the Cache interface with the same behavior as LRU.
|
||||
//
|
||||
// Note that, because of it's distributed nature, the fixed size cannot be strictly respected
|
||||
// and you may have a tiny bit more space for keys than you defined through LRUOptions.
|
||||
// Bucket size is computed as follows: (size / nbuckets) + (size % nbuckets)
|
||||
//
|
||||
// Because of this size limit per bucket, and because of the nature of the data, you
|
||||
// may have buckets filled unevenly, and because of this, keys will be evicted from the entire
|
||||
// cache where a simple LRU wouldn't have. Example:
|
||||
//
|
||||
// Two buckets B1 and B2, of max size 2 each, meaning, theoretically, a max size of 4:
|
||||
// * Say you have a set of 3 keys, they could fill an entire LRU cache.
|
||||
// * But if all those keys are assigned to a single bucket B1, the first key will be evicted from B1
|
||||
// * B2 will remain empty, even though there was enough memory allocated
|
||||
//
|
||||
// With 4 buckets and random UUIDs as keys, the amount of false evictions is around 5%.
|
||||
//
|
||||
// By default, the number of buckets equals the number of cpus returned from runtime.NumCPU.
|
||||
//
|
||||
// This struct is lock-free and intended to be used without lock.
|
||||
type LRUStriped struct {
|
||||
buckets []*LRU
|
||||
name string
|
||||
invalidateClusterEvent string
|
||||
}
|
||||
|
||||
func (L LRUStriped) hashkeyMapHash(key string) uint64 {
|
||||
return xxhash.Sum64String(key)
|
||||
}
|
||||
|
||||
func (L LRUStriped) keyBucket(key string) *LRU {
|
||||
return L.buckets[L.hashkeyMapHash(key)%uint64(len(L.buckets))]
|
||||
}
|
||||
|
||||
// Purge loops through each LRU cache for purging. Since LRUStriped doesn't use any lock,
|
||||
// each LRU bucket is purged after another one, which means that keys could still
|
||||
// be present after a call to Purge.
|
||||
func (L LRUStriped) Purge() error {
|
||||
for _, lru := range L.buckets {
|
||||
lru.Purge() // errors from purging LRU can be ignored as they always return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Set does the same as LRU.Set
|
||||
func (L LRUStriped) Set(key string, value interface{}) error {
|
||||
return L.keyBucket(key).Set(key, value)
|
||||
}
|
||||
|
||||
// SetWithDefaultExpiry does the same as LRU.SetWithDefaultExpiry
|
||||
func (L LRUStriped) SetWithDefaultExpiry(key string, value interface{}) error {
|
||||
return L.keyBucket(key).SetWithDefaultExpiry(key, value)
|
||||
}
|
||||
|
||||
// SetWithExpiry does the same as LRU.SetWithExpiry
|
||||
func (L LRUStriped) SetWithExpiry(key string, value interface{}, ttl time.Duration) error {
|
||||
return L.keyBucket(key).SetWithExpiry(key, value, ttl)
|
||||
}
|
||||
|
||||
// Get does the same as LRU.Get
|
||||
func (L LRUStriped) Get(key string, value interface{}) error {
|
||||
return L.keyBucket(key).Get(key, value)
|
||||
}
|
||||
|
||||
// Remove does the same as LRU.Remove
|
||||
func (L LRUStriped) Remove(key string) error {
|
||||
return L.keyBucket(key).Remove(key)
|
||||
}
|
||||
|
||||
// Keys does the same as LRU.Keys. However, because this is lock-free, keys might be
|
||||
// inserted or removed from a previously scanned LRU cache.
|
||||
// This is not as precise as using a single LRU instance.
|
||||
func (L LRUStriped) Keys() ([]string, error) {
|
||||
var keys []string
|
||||
for _, lru := range L.buckets {
|
||||
k, _ := lru.Keys() // Keys never returns any error
|
||||
keys = append(keys, k...)
|
||||
}
|
||||
return keys, nil
|
||||
}
|
||||
|
||||
// Len does the same as LRU.Len. As for LRUStriped.Keys, this call cannot be precise.
|
||||
func (L LRUStriped) Len() (int, error) {
|
||||
var size int
|
||||
for _, lru := range L.buckets {
|
||||
s, _ := lru.Len() // Len never returns any error
|
||||
size += s
|
||||
}
|
||||
return size, nil
|
||||
}
|
||||
|
||||
// GetInvalidateClusterEvent does the same as LRU.GetInvalidateClusterEvent
|
||||
func (L LRUStriped) GetInvalidateClusterEvent() string {
|
||||
return L.invalidateClusterEvent
|
||||
}
|
||||
|
||||
// Name does the same as LRU.Name
|
||||
func (L LRUStriped) Name() string {
|
||||
return L.name
|
||||
}
|
||||
|
||||
// NewLRUStriped creates a striped LRU cache using the special LRUOptions.StripedBuckets value.
|
||||
// See LRUStriped and LRUOptions for more details.
|
||||
//
|
||||
// Not that in order to prevent false eviction, this LRU cache adds 10% (computation is rounded up) of the
|
||||
// requested size to the total cache size.
|
||||
func NewLRUStriped(opts LRUOptions) (Cache, error) {
|
||||
if opts.StripedBuckets == 0 {
|
||||
return nil, fmt.Errorf("number of buckets is mandatory")
|
||||
}
|
||||
|
||||
if opts.Size < opts.StripedBuckets {
|
||||
return nil, fmt.Errorf("cache size must at least be equal to the number of buckets")
|
||||
}
|
||||
|
||||
// add 10% to the total size, before splitting
|
||||
opts.Size += int(math.Ceil(float64(opts.Size) * 10.0 / 100.0))
|
||||
// now this is the size for each bucket
|
||||
opts.Size = (opts.Size / opts.StripedBuckets) + (opts.Size % opts.StripedBuckets)
|
||||
|
||||
buckets := make([]*LRU, opts.StripedBuckets)
|
||||
for i := 0; i < opts.StripedBuckets; i++ {
|
||||
buckets[i] = NewLRU(opts).(*LRU)
|
||||
}
|
||||
|
||||
return LRUStriped{
|
||||
buckets: buckets,
|
||||
invalidateClusterEvent: opts.InvalidateClusterEvent,
|
||||
name: opts.Name,
|
||||
}, nil
|
||||
}
|
||||
91
services/cache/lru_striped_bench_test.go
поставляемый
Обычный файл
91
services/cache/lru_striped_bench_test.go
поставляемый
Обычный файл
@@ -0,0 +1,91 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package cache_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/services/cache"
|
||||
|
||||
"github.com/cespare/xxhash/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
m = 500_000
|
||||
)
|
||||
|
||||
func BenchmarkLRUStriped(b *testing.B) {
|
||||
opts := cache.LRUOptions{
|
||||
Name: "",
|
||||
Size: 128,
|
||||
DefaultExpiry: 0,
|
||||
InvalidateClusterEvent: "",
|
||||
StripedBuckets: runtime.NumCPU() - 1,
|
||||
}
|
||||
|
||||
cache, err := cache.NewLRUStriped(opts)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// prepare keys and initial cache values and set routine
|
||||
keys := make([]string, 0, m)
|
||||
// bucketKeys is to demonstrate that splitted locks is working correctly
|
||||
// by assigning one sequence of key for each bucket.
|
||||
bucketKeys := make([][]string, opts.StripedBuckets)
|
||||
for i := 0; i < m; i++ {
|
||||
key := fmt.Sprintf("%d-key-%d", i, i)
|
||||
keys = append(keys, key)
|
||||
bucketKey := xxhash.Sum64String(key) % uint64(opts.StripedBuckets)
|
||||
bucketKeys[bucketKey] = append(bucketKeys[bucketKey], key)
|
||||
}
|
||||
for i := 0; i < opts.Size; i++ {
|
||||
cache.Set(keys[i], "preflight")
|
||||
}
|
||||
|
||||
wgGet := &sync.WaitGroup{}
|
||||
wgSet := &sync.WaitGroup{}
|
||||
// need buffered chan because if the set routine finished before we write into the chan,
|
||||
// we're left without any consumer, making any write to the chan waiting forever.
|
||||
stopSet := make(chan bool, 1)
|
||||
set := func() {
|
||||
defer wgSet.Done()
|
||||
for i := 0; i < m; i++ {
|
||||
select {
|
||||
case <-stopSet:
|
||||
return
|
||||
default:
|
||||
_ = cache.Set(keys[i], "ignored")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
get := func(bucket int) {
|
||||
defer wgGet.Done()
|
||||
var out string
|
||||
for i := 0; i < m; i++ {
|
||||
_ = cache.Get(bucketKeys[bucket][i%opts.Size], &out)
|
||||
}
|
||||
}
|
||||
|
||||
b.StopTimer()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
wgSet.Add(1)
|
||||
go set()
|
||||
for j := 0; j < opts.StripedBuckets; j++ {
|
||||
wgGet.Add(1)
|
||||
go get(j)
|
||||
}
|
||||
|
||||
b.StartTimer()
|
||||
wgGet.Wait()
|
||||
b.StopTimer()
|
||||
|
||||
stopSet <- true
|
||||
wgSet.Wait()
|
||||
}
|
||||
}
|
||||
129
services/cache/lru_striped_test.go
поставляемый
Обычный файл
129
services/cache/lru_striped_test.go
поставляемый
Обычный файл
@@ -0,0 +1,129 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"hash/maphash"
|
||||
"testing"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/model"
|
||||
|
||||
"github.com/cespare/xxhash/v2"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func makeLRUPredictibleTestData(num int) [][2]string {
|
||||
kv := make([][2]string, num)
|
||||
for i := 0; i < len(kv); i++ {
|
||||
kv[i] = [2]string{
|
||||
fmt.Sprintf("%d-key-%d", i, i),
|
||||
fmt.Sprintf("%d-val-%d", i, i),
|
||||
}
|
||||
}
|
||||
return kv
|
||||
}
|
||||
|
||||
func TestNewLRUStriped(t *testing.T) {
|
||||
scache, err := NewLRUStriped(LRUOptions{StripedBuckets: 3, Size: 20})
|
||||
require.NoError(t, err)
|
||||
|
||||
cache := scache.(LRUStriped)
|
||||
|
||||
require.Len(t, cache.buckets, 3)
|
||||
assert.Equal(t, 8, cache.buckets[0].size)
|
||||
assert.Equal(t, 8, cache.buckets[1].size)
|
||||
assert.Equal(t, 8, cache.buckets[2].size)
|
||||
}
|
||||
|
||||
func TestLRUStripedKeyDistribution(t *testing.T) {
|
||||
dataset := makeLRUPredictibleTestData(100)
|
||||
|
||||
scache, err := NewLRUStriped(LRUOptions{StripedBuckets: 4, Size: len(dataset)})
|
||||
require.NoError(t, err)
|
||||
cache := scache.(LRUStriped)
|
||||
for _, kv := range dataset {
|
||||
require.NoError(t, cache.Set(kv[0], kv[1]))
|
||||
var out string
|
||||
require.NoError(t, cache.Get(kv[0], &out))
|
||||
require.Equal(t, kv[1], out)
|
||||
}
|
||||
|
||||
require.Len(t, cache.buckets, 4)
|
||||
acc := 0
|
||||
for i := 0; i < 4; i++ {
|
||||
clen, err := cache.buckets[i].Len()
|
||||
acc += clen
|
||||
assert.NoError(t, err)
|
||||
assert.GreaterOrEqual(t, clen, len(dataset)/2/4, "at least 50%/nbuckets of all keys in each bucket")
|
||||
}
|
||||
// because of the limited size of each bucket and the nature of our data,
|
||||
// we may have around 10% of our keys evicted in this scenario. removing 1% because we cannot predict
|
||||
// accurately what is happening with random data.
|
||||
assert.GreaterOrEqual(t, acc, len(dataset)-(len(dataset)*1.0/100.0))
|
||||
}
|
||||
|
||||
func TestLRUStriped_Size(t *testing.T) {
|
||||
scache, err := NewLRUStriped(LRUOptions{StripedBuckets: 2, Size: 128})
|
||||
require.NoError(t, err)
|
||||
cache := scache.(LRUStriped)
|
||||
acc := 0
|
||||
for _, bucket := range cache.buckets {
|
||||
acc += bucket.size
|
||||
}
|
||||
assert.Equal(t, 128+13+1, acc) // +10% +modulo padding
|
||||
}
|
||||
|
||||
func TestLRUStriped_HashKey(t *testing.T) {
|
||||
scache, err := NewLRUStriped(LRUOptions{StripedBuckets: 2, Size: 128})
|
||||
require.NoError(t, err)
|
||||
cache := scache.(LRUStriped)
|
||||
first := cache.hashkeyMapHash("key")
|
||||
cache.hashkeyMapHash("other_key_to_ensure_that_result_it’s_not_dependent_on_previous_input")
|
||||
second := cache.hashkeyMapHash("key")
|
||||
require.Equal(t, first, second)
|
||||
}
|
||||
|
||||
func TestLRUStriped_Get(t *testing.T) {
|
||||
cache, err := NewLRUStriped(LRUOptions{StripedBuckets: 4, Size: 128})
|
||||
require.NoError(t, err)
|
||||
var out string
|
||||
require.Equal(t, ErrKeyNotFound, cache.Get("key", &out))
|
||||
require.Zero(t, out)
|
||||
|
||||
require.NoError(t, cache.Set("key", "value"))
|
||||
require.NoError(t, cache.Get("key", &out))
|
||||
require.Equal(t, "value", out)
|
||||
}
|
||||
|
||||
var hashSink uint64
|
||||
|
||||
func BenchmarkSum64(b *testing.B) {
|
||||
cases := []string{
|
||||
"1",
|
||||
"22",
|
||||
"333",
|
||||
model.NewId(),
|
||||
model.NewId() + model.NewId(),
|
||||
}
|
||||
|
||||
for _, case_ := range cases {
|
||||
b.Run(fmt.Sprintf("maphash_string_len_%d", len(case_)), func(b *testing.B) {
|
||||
seed := maphash.MakeSeed()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
var h maphash.Hash
|
||||
h.SetSeed(seed)
|
||||
h.WriteString(case_) // documentation and code says it never fails
|
||||
hashSink = h.Sum64()
|
||||
}
|
||||
})
|
||||
b.Run(fmt.Sprintf("xxhash_string_len_%d", len(case_)), func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
hashSink = xxhash.Sum64String(case_)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
22
services/cache/lru_test.go
поставляемый
22
services/cache/lru_test.go
поставляемый
@@ -15,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
func TestLRU(t *testing.T) {
|
||||
l := NewLRU(&LRUOptions{
|
||||
l := NewLRU(LRUOptions{
|
||||
Size: 128,
|
||||
DefaultExpiry: 0,
|
||||
InvalidateClusterEvent: "",
|
||||
@@ -83,7 +83,7 @@ func TestLRU(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLRUExpire(t *testing.T) {
|
||||
l := NewLRU(&LRUOptions{
|
||||
l := NewLRU(LRUOptions{
|
||||
Size: 128,
|
||||
DefaultExpiry: 1 * time.Second,
|
||||
InvalidateClusterEvent: "",
|
||||
@@ -105,7 +105,7 @@ func TestLRUExpire(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLRUMarshalUnMarshal(t *testing.T) {
|
||||
l := NewLRU(&LRUOptions{
|
||||
l := NewLRU(LRUOptions{
|
||||
Size: 1,
|
||||
DefaultExpiry: 0,
|
||||
InvalidateClusterEvent: "",
|
||||
@@ -299,7 +299,7 @@ func BenchmarkLRU(b *testing.B) {
|
||||
|
||||
b.Run("simple=new", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
l2 := NewLRU(&LRUOptions{
|
||||
l2 := NewLRU(LRUOptions{
|
||||
Size: 1,
|
||||
DefaultExpiry: 0,
|
||||
InvalidateClusterEvent: "",
|
||||
@@ -349,7 +349,7 @@ func BenchmarkLRU(b *testing.B) {
|
||||
|
||||
b.Run("complex=new", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
l2 := NewLRU(&LRUOptions{
|
||||
l2 := NewLRU(LRUOptions{
|
||||
Size: 1,
|
||||
DefaultExpiry: 0,
|
||||
InvalidateClusterEvent: "",
|
||||
@@ -432,7 +432,7 @@ func BenchmarkLRU(b *testing.B) {
|
||||
|
||||
b.Run("User=new", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
l2 := NewLRU(&LRUOptions{
|
||||
l2 := NewLRU(LRUOptions{
|
||||
Size: 1,
|
||||
DefaultExpiry: 0,
|
||||
InvalidateClusterEvent: "",
|
||||
@@ -465,7 +465,7 @@ func BenchmarkLRU(b *testing.B) {
|
||||
|
||||
b.Run("UserMap=new", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
l2 := NewLRU(&LRUOptions{
|
||||
l2 := NewLRU(LRUOptions{
|
||||
Size: 1,
|
||||
DefaultExpiry: 0,
|
||||
InvalidateClusterEvent: "",
|
||||
@@ -545,7 +545,7 @@ func BenchmarkLRU(b *testing.B) {
|
||||
|
||||
b.Run("Post=new", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
l2 := NewLRU(&LRUOptions{
|
||||
l2 := NewLRU(LRUOptions{
|
||||
Size: 1,
|
||||
DefaultExpiry: 0,
|
||||
InvalidateClusterEvent: "",
|
||||
@@ -569,7 +569,7 @@ func BenchmarkLRU(b *testing.B) {
|
||||
|
||||
b.Run("Status=new", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
l2 := NewLRU(&LRUOptions{
|
||||
l2 := NewLRU(LRUOptions{
|
||||
Size: 1,
|
||||
DefaultExpiry: 0,
|
||||
InvalidateClusterEvent: "",
|
||||
@@ -605,7 +605,7 @@ func BenchmarkLRU(b *testing.B) {
|
||||
|
||||
b.Run("Session=new", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
l2 := NewLRU(&LRUOptions{
|
||||
l2 := NewLRU(LRUOptions{
|
||||
Size: 1,
|
||||
DefaultExpiry: 0,
|
||||
InvalidateClusterEvent: "",
|
||||
@@ -621,7 +621,7 @@ func BenchmarkLRU(b *testing.B) {
|
||||
}
|
||||
|
||||
func TestLRURace(t *testing.T) {
|
||||
l2 := NewLRU(&LRUOptions{
|
||||
l2 := NewLRU(LRUOptions{
|
||||
Size: 1,
|
||||
DefaultExpiry: 0,
|
||||
InvalidateClusterEvent: "",
|
||||
|
||||
7
services/cache/mocks/Cache.go
поставляемый
7
services/cache/mocks/Cache.go
поставляемый
@@ -2,8 +2,11 @@
|
||||
|
||||
package mocks
|
||||
|
||||
import mock "github.com/stretchr/testify/mock"
|
||||
import time "time"
|
||||
import (
|
||||
time "time"
|
||||
|
||||
mock "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
// Cache is an autogenerated mock type for the Cache type
|
||||
type Cache struct {
|
||||
|
||||
11
services/cache/mocks/Provider.go
поставляемый
11
services/cache/mocks/Provider.go
поставляемый
@@ -41,7 +41,7 @@ func (_m *Provider) Connect() error {
|
||||
}
|
||||
|
||||
// NewCache provides a mock function with given fields: opts
|
||||
func (_m *Provider) NewCache(opts *cache.CacheOptions) cache.Cache {
|
||||
func (_m *Provider) NewCache(opts *cache.CacheOptions) (cache.Cache, error) {
|
||||
ret := _m.Called(opts)
|
||||
|
||||
var r0 cache.Cache
|
||||
@@ -53,5 +53,12 @@ func (_m *Provider) NewCache(opts *cache.CacheOptions) cache.Cache {
|
||||
}
|
||||
}
|
||||
|
||||
return r0
|
||||
var r1 error
|
||||
if rf, ok := ret.Get(1).(func(*cache.CacheOptions) error); ok {
|
||||
r1 = rf(opts)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
19
services/cache/provider.go
поставляемый
19
services/cache/provider.go
поставляемый
@@ -11,12 +11,14 @@ type CacheOptions struct {
|
||||
DefaultExpiry time.Duration
|
||||
Name string
|
||||
InvalidateClusterEvent string
|
||||
Striped bool
|
||||
StripedBuckets int
|
||||
}
|
||||
|
||||
// Provider is a provider for Cache
|
||||
type Provider interface {
|
||||
// NewCache creates a new cache with given options.
|
||||
NewCache(opts *CacheOptions) Cache
|
||||
NewCache(opts *CacheOptions) (Cache, error)
|
||||
// Connect opens a new connection to the cache using specific provider parameters.
|
||||
Connect() error
|
||||
// Close releases any resources used by the cache provider.
|
||||
@@ -32,13 +34,22 @@ func NewProvider() Provider {
|
||||
}
|
||||
|
||||
// NewCache creates a new cache with given opts
|
||||
func (c *cacheProvider) NewCache(opts *CacheOptions) Cache {
|
||||
return NewLRU(&LRUOptions{
|
||||
func (c *cacheProvider) NewCache(opts *CacheOptions) (Cache, error) {
|
||||
if opts.Striped {
|
||||
return NewLRUStriped(LRUOptions{
|
||||
Name: opts.Name,
|
||||
Size: opts.Size,
|
||||
DefaultExpiry: opts.DefaultExpiry,
|
||||
InvalidateClusterEvent: opts.InvalidateClusterEvent,
|
||||
StripedBuckets: opts.StripedBuckets,
|
||||
})
|
||||
}
|
||||
return NewLRU(LRUOptions{
|
||||
Name: opts.Name,
|
||||
Size: opts.Size,
|
||||
DefaultExpiry: opts.DefaultExpiry,
|
||||
InvalidateClusterEvent: opts.InvalidateClusterEvent,
|
||||
})
|
||||
}), nil
|
||||
}
|
||||
|
||||
// Connect opens a new connection to the cache using specific provider parameters.
|
||||
|
||||
133
services/cache/provider_test.go
поставляемый
133
services/cache/provider_test.go
поставляемый
@@ -15,16 +15,39 @@ func TestNewCache(t *testing.T) {
|
||||
p := NewProvider()
|
||||
|
||||
size := 1
|
||||
c := p.NewCache(&CacheOptions{
|
||||
c, err := p.NewCache(&CacheOptions{
|
||||
Size: size,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
err := c.Set("key1", "val1")
|
||||
require.Nil(t, err)
|
||||
err = c.Set("key1", "val1")
|
||||
require.NoError(t, err)
|
||||
err = c.Set("key2", "val2")
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
err = c.Set("key3", "val3")
|
||||
require.NoError(t, err)
|
||||
l, err := c.Len()
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, size, l)
|
||||
})
|
||||
|
||||
t.Run("with only size option given", func(t *testing.T) {
|
||||
p := NewProvider()
|
||||
|
||||
size := 1
|
||||
c, err := p.NewCache(&CacheOptions{
|
||||
Size: size,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
err = c.Set("key1", "val1")
|
||||
require.NoError(t, err)
|
||||
err = c.Set("key2", "val2")
|
||||
require.NoError(t, err)
|
||||
err = c.Set("key3", "val3")
|
||||
require.NoError(t, err)
|
||||
l, err := c.Len()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, size, l)
|
||||
})
|
||||
|
||||
@@ -34,21 +57,24 @@ func TestNewCache(t *testing.T) {
|
||||
size := 1
|
||||
expiry := 1 * time.Second
|
||||
event := "clusterEvent"
|
||||
c := p.NewCache(&CacheOptions{
|
||||
c, err := p.NewCache(&CacheOptions{
|
||||
Size: size,
|
||||
Name: "name",
|
||||
DefaultExpiry: expiry,
|
||||
InvalidateClusterEvent: event,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, event, c.GetInvalidateClusterEvent())
|
||||
|
||||
err := c.SetWithDefaultExpiry("key1", "val1")
|
||||
require.Nil(t, err)
|
||||
err = c.SetWithDefaultExpiry("key1", "val1")
|
||||
require.NoError(t, err)
|
||||
err = c.SetWithDefaultExpiry("key2", "val2")
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
err = c.SetWithDefaultExpiry("key3", "val3")
|
||||
require.NoError(t, err)
|
||||
l, err := c.Len()
|
||||
require.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, size, l)
|
||||
|
||||
time.Sleep(expiry + 1*time.Second)
|
||||
@@ -58,6 +84,93 @@ func TestNewCache(t *testing.T) {
|
||||
require.Equal(t, ErrKeyNotFound, err)
|
||||
err = c.Get("key2", &v)
|
||||
require.Equal(t, ErrKeyNotFound, err)
|
||||
err = c.Get("key3", &v)
|
||||
require.Equal(t, ErrKeyNotFound, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewCache_Striped(t *testing.T) {
|
||||
t.Run("with only size option given", func(t *testing.T) {
|
||||
p := NewProvider()
|
||||
|
||||
size := 1
|
||||
c, err := p.NewCache(&CacheOptions{
|
||||
Size: size,
|
||||
Striped: true,
|
||||
StripedBuckets: 1,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
err = c.Set("key1", "val1")
|
||||
require.NoError(t, err)
|
||||
err = c.Set("key2", "val2")
|
||||
require.NoError(t, err)
|
||||
err = c.Set("key3", "val3")
|
||||
require.NoError(t, err)
|
||||
l, err := c.Len()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, size+1, l) // +10% from striping
|
||||
})
|
||||
|
||||
t.Run("with only size option given", func(t *testing.T) {
|
||||
p := NewProvider()
|
||||
|
||||
size := 1
|
||||
c, err := p.NewCache(&CacheOptions{
|
||||
Size: size,
|
||||
Striped: true,
|
||||
StripedBuckets: 1,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
err = c.Set("key1", "val1")
|
||||
require.NoError(t, err)
|
||||
err = c.Set("key2", "val2")
|
||||
require.NoError(t, err)
|
||||
err = c.Set("key3", "val3")
|
||||
require.NoError(t, err)
|
||||
l, err := c.Len()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, size+1, l) // +10% rounded up from striped lru
|
||||
})
|
||||
|
||||
t.Run("with all options specified", func(t *testing.T) {
|
||||
p := NewProvider()
|
||||
|
||||
size := 1
|
||||
expiry := 1 * time.Second
|
||||
event := "clusterEvent"
|
||||
c, err := p.NewCache(&CacheOptions{
|
||||
Size: size,
|
||||
Name: "name",
|
||||
DefaultExpiry: expiry,
|
||||
InvalidateClusterEvent: event,
|
||||
Striped: true,
|
||||
StripedBuckets: 1,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, event, c.GetInvalidateClusterEvent())
|
||||
|
||||
err = c.SetWithDefaultExpiry("key1", "val1")
|
||||
require.NoError(t, err)
|
||||
err = c.SetWithDefaultExpiry("key2", "val2")
|
||||
require.NoError(t, err)
|
||||
err = c.SetWithDefaultExpiry("key3", "val3")
|
||||
require.NoError(t, err)
|
||||
l, err := c.Len()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, size+1, l) // +10% from striping
|
||||
|
||||
time.Sleep(expiry + 1*time.Second)
|
||||
|
||||
var v string
|
||||
err = c.Get("key1", &v)
|
||||
require.Equal(t, ErrKeyNotFound, err)
|
||||
err = c.Get("key2", &v)
|
||||
require.Equal(t, ErrKeyNotFound, err)
|
||||
err = c.Get("key3", &v)
|
||||
require.Equal(t, ErrKeyNotFound, err)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user