Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Siyuan Liu
2020-07-18 01:01:06 -07:00
коммит произвёл GitHub
родитель 3f46cf6f60
Коммит c7f7bef9ec
22 изменённых файлов: 111 добавлений и 578 удалений

61
services/cache/cache.go поставляемый
Просмотреть файл

@@ -4,57 +4,46 @@
package cache
import (
"errors"
"time"
)
// Cache is a representation of any cache store that has keys and values
// 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
type Cache interface {
// Purge is used to completely clear the cache.
Purge()
Purge() error
// Add adds the given key and value to the store without an expiry.
Add(key string, value interface{})
// Set adds the given key and value to the store without an expiry. If the key already exists,
// it will overwrite the previous value.
Set(key string, value interface{}) error
// AddWithDefaultExpires adds the given key and value to the store with the default expiry.
AddWithDefaultExpires(key string, value interface{})
// SetWithDefaultExpiry adds the given key and value to the store with the default expiry. If
// the key already exists, it will overwrite the previoous value
SetWithDefaultExpiry(key string, value interface{}) error
// AddWithExpiresInSecs adds the given key and value to the cache with the given expiry.
AddWithExpiresInSecs(key string, value interface{}, expireAtSecs int64)
// SetWithExpiry adds the given key and value to the cache with the given expiry. If the key
// already exists, it will overwrite the previoous value
SetWithExpiry(key string, value interface{}, ttl time.Duration) error
// 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 string) (value interface{}, ok bool)
// Get the content stored in the cache for the given key, and decode it into the value interface.
// Return ErrKeyNotFound if the key is missing from the cache
Get(key string, value interface{}) error
// 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 string, value interface{}, ttl time.Duration) (actual interface{}, loaded bool)
// Remove deletes the value for a key.
Remove(key string)
// Remove deletes the value for a given key.
Remove(key string) error
// Keys returns a slice of the keys in the cache.
Keys() []string
Keys() ([]string, error)
// Len returns the number of items in the cache.
Len() int
// Name identifies this cache instance among others in the system.
Name() string
Len() (int, error)
// 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()
// Name returns the name of the cache
Name() string
}

Просмотреть файл

@@ -1,7 +1,7 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package cache2
package cache
import (
"container/list"

232
services/cache/lru/lru.go поставляемый
Просмотреть файл

@@ -1,232 +0,0 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
// This files was copied/modified from https://github.com/hashicorp/golang-lru
// which was (see below)
// This package provides a simple LRU cache. It is based on the
// LRU implementation in groupcache:
// https://github.com/golang/groupcache/tree/master/lru
package lru
import (
"container/list"
"sync"
"time"
"github.com/mattermost/mattermost-server/v5/services/cache"
)
// Cache is a thread-safe fixed size LRU cache.
type Cache struct {
size int
evictList *list.List
items map[string]*list.Element
lock sync.RWMutex
name string
defaultExpiry int64
invalidateClusterEvent string
currentGeneration int64
len int
}
// CacheProvider is an implementation of cache.Provider to create a new Lru Cache
type CacheProvider struct{}
// NewCache creates a new lru.Cache with given size.
func (c *CacheProvider) NewCache(size int) cache.Cache {
return New(size)
}
// NewCacheWithParams creates a new lru.Cache with the given parameters.
func (c *CacheProvider) NewCacheWithParams(size int, name string, defaultExpiry int64, invalidateClusterEvent string) cache.Cache {
return NewWithParams(size, name, defaultExpiry, invalidateClusterEvent)
}
// Connect opens a new connection to the cache using specific provider parameters.
func (c *CacheProvider) Connect() {
}
// Close releases any resources used by the cache provider.
func (c *CacheProvider) Close() {
}
// entry is used to hold a value in the evictList.
type entry struct {
key string
value interface{}
expires time.Time
generation int64
}
// New creates an LRU of the given size.
func New(size int) *Cache {
return &Cache{
size: size,
evictList: list.New(),
items: make(map[string]*list.Element, size),
}
}
// NewWithParams creates an LRU with the given parameters.
func NewWithParams(size int, name string, defaultExpiry int64, invalidateClusterEvent string) *Cache {
lru := New(size)
lru.name = name
lru.defaultExpiry = defaultExpiry
lru.invalidateClusterEvent = invalidateClusterEvent
return lru
}
// Purge is used to completely clear the cache.
func (c *Cache) Purge() {
c.lock.Lock()
defer c.lock.Unlock()
c.len = 0
c.currentGeneration++
}
// Add adds the given key and value to the store without an expiry.
func (c *Cache) Add(key string, value interface{}) {
c.AddWithExpiresInSecs(key, value, 0)
}
// AddWithDefaultExpires adds the given key and value to the store with the default expiry.
func (c *Cache) AddWithDefaultExpires(key string, value interface{}) {
c.AddWithExpiresInSecs(key, value, c.defaultExpiry)
}
// AddWithExpiresInSecs adds the given key and value to the cache with the given expiry.
func (c *Cache) AddWithExpiresInSecs(key string, value interface{}, expireAtSecs int64) {
c.lock.Lock()
defer c.lock.Unlock()
c.add(key, value, time.Duration(expireAtSecs)*time.Second)
}
func (c *Cache) add(key string, value interface{}, ttl time.Duration) {
var expires time.Time
if ttl > 0 {
expires = time.Now().Add(ttl)
}
// Check for existing item, ignoring expiry since we'd update anyway.
if ent, ok := c.items[key]; ok {
c.evictList.MoveToFront(ent)
e := ent.Value.(*entry)
e.value = value
e.expires = expires
if e.generation != c.currentGeneration {
e.generation = c.currentGeneration
c.len++
}
return
}
// Add new item
ent := &entry{key, value, expires, c.currentGeneration}
entry := c.evictList.PushFront(ent)
c.items[key] = entry
c.len++
if c.evictList.Len() > c.size {
c.removeElement(c.evictList.Back())
}
}
// 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.
func (c *Cache) Get(key string) (value interface{}, ok bool) {
c.lock.Lock()
defer c.lock.Unlock()
return c.getValue(key)
}
func (c *Cache) getValue(key string) (value interface{}, ok bool) {
if ent, ok := c.items[key]; ok {
e := ent.Value.(*entry)
if e.generation != c.currentGeneration || (!e.expires.IsZero() && time.Now().After(e.expires)) {
c.removeElement(ent)
return nil, false
}
c.evictList.MoveToFront(ent)
return ent.Value.(*entry).value, true
}
return nil, false
}
// 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.
func (c *Cache) GetOrAdd(key string, value interface{}, ttl time.Duration) (actual interface{}, loaded bool) {
c.lock.Lock()
defer c.lock.Unlock()
// Check for existing item
if actualValue, ok := c.getValue(key); ok {
return actualValue, true
}
c.add(key, value, ttl)
return value, false
}
// Remove deletes the value for a key.
func (c *Cache) Remove(key string) {
c.lock.Lock()
defer c.lock.Unlock()
if ent, ok := c.items[key]; ok {
c.removeElement(ent)
}
}
// Keys returns a slice of the keys in the cache, from oldest to newest.
func (c *Cache) Keys() []string {
c.lock.RLock()
defer c.lock.RUnlock()
keys := make([]string, c.len)
i := 0
for ent := c.evictList.Back(); ent != nil; ent = ent.Prev() {
e := ent.Value.(*entry)
if e.generation == c.currentGeneration {
keys[i] = e.key
i++
}
}
return keys
}
// Len returns the number of items in the cache.
func (c *Cache) Len() int {
c.lock.RLock()
defer c.lock.RUnlock()
return c.len
}
// Name identifies this cache instance among others in the system.
func (c *Cache) Name() string {
return c.name
}
// GetInvalidateClusterEvent returns the cluster event configured when this cache was created.
func (c *Cache) GetInvalidateClusterEvent() string {
return c.invalidateClusterEvent
}
func (c *Cache) removeElement(e *list.Element) {
c.evictList.Remove(e)
kv := e.Value.(*entry)
if kv.generation == c.currentGeneration {
c.len--
}
delete(c.items, kv.key)
}

125
services/cache/lru/lru_test.go поставляемый
Просмотреть файл

@@ -1,125 +0,0 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
// This files was copied/modified from https://github.com/hashicorp/golang-lru
// which was (see below)
// This package provides a simple LRU cache. It is based on the
// LRU implementation in groupcache:
// https://github.com/golang/groupcache/tree/master/lru
package lru
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLRU(t *testing.T) {
l := New(128)
for i := 0; i < 256; i++ {
l.Add(fmt.Sprintf("%d", i), i)
}
require.Equalf(t, l.Len(), 128, "bad len: %v", l.Len())
for i, k := range l.Keys() {
v, ok := l.Get(k)
require.True(t, ok, "bad key: %v", k)
require.Equalf(t, fmt.Sprintf("%d", v), k, "bad key: %v", k)
require.Equalf(t, i+128, v, "bad key: %v", k)
}
for i := 0; i < 128; i++ {
_, ok := l.Get(fmt.Sprintf("%d", i))
require.False(t, ok, "should be evicted")
}
for i := 128; i < 256; i++ {
_, ok := l.Get(fmt.Sprintf("%d", i))
require.True(t, ok, "should not be evicted")
}
for i := 128; i < 192; i++ {
l.Remove(fmt.Sprintf("%d", i))
_, ok := l.Get(fmt.Sprintf("%d", i))
require.False(t, ok, "should be deleted")
}
l.Get("192") // expect 192 to be last key in l.Keys()
for i, k := range l.Keys() {
require.Falsef(t, i < 63 && k != fmt.Sprintf("%d", i+193), "out of order key: %v", k)
require.Falsef(t, i == 63 && k != "192", "out of order key: %v", k)
}
l.Purge()
require.Equalf(t, l.Len(), 0, "bad len: %v", l.Len())
_, ok := l.Get("200")
require.False(t, ok, "should contain nothing")
}
func TestLRUExpire(t *testing.T) {
l := New(128)
l.AddWithExpiresInSecs("1", 1, 1)
l.AddWithExpiresInSecs("2", 2, 1)
l.AddWithExpiresInSecs("3", 3, 0)
time.Sleep(time.Millisecond * 2100)
r1, ok := l.Get("1")
require.False(t, ok, r1)
_, ok2 := l.Get("3")
require.True(t, ok2, "should exist")
}
func TestLRUGetOrAdd(t *testing.T) {
l := New(128)
// First GetOrAdd should save
value, loaded := l.GetOrAdd("1", 1, 0)
assert.Equal(t, 1, value)
assert.False(t, loaded)
// Second GetOrAdd should load original value, ignoring new value
value, loaded = l.GetOrAdd("1", 10, 0)
assert.Equal(t, 1, value)
assert.True(t, loaded)
// Third GetOrAdd should still load original value
value, loaded = l.GetOrAdd("1", 1, 0)
assert.Equal(t, 1, value)
assert.True(t, loaded)
// First GetOrAdd on a new key should save
value, loaded = l.GetOrAdd("2", 2, 0)
assert.Equal(t, 2, value)
assert.False(t, loaded)
l.Remove("1")
// GetOrAdd after a remove should save
value, loaded = l.GetOrAdd("1", 10, 0)
assert.Equal(t, 10, value)
assert.False(t, loaded)
// GetOrAdd after another key was removed should load original value for key
value, loaded = l.GetOrAdd("2", 2, 0)
assert.Equal(t, 2, value)
assert.True(t, loaded)
// GetOrAdd should expire
value, loaded = l.GetOrAdd("3", 3, 500*time.Millisecond)
assert.Equal(t, 3, value)
assert.False(t, loaded)
value, loaded = l.GetOrAdd("3", 4, 500*time.Millisecond)
assert.Equal(t, 3, value)
assert.True(t, loaded)
time.Sleep(1 * time.Second)
value, loaded = l.GetOrAdd("3", 5, 500*time.Millisecond)
assert.Equal(t, 5, value)
assert.False(t, loaded)
}

Просмотреть файл

@@ -1,7 +1,7 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package cache2
package cache
import (
"fmt"
@@ -9,8 +9,6 @@ import (
"time"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/services/cache/lru"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -212,14 +210,6 @@ func TestLRUMarshalUnMarshal(t *testing.T) {
func BenchmarkLRU(b *testing.B) {
value1 := "simplestring"
b.Run("simple=old", func(b *testing.B) {
for i := 0; i < b.N; i++ {
l := lru.New(1)
l.Add("test", value1)
_, ok := l.Get("test")
require.True(b, ok)
}
})
b.Run("simple=new", func(b *testing.B) {
for i := 0; i < b.N; i++ {
@@ -270,14 +260,7 @@ func BenchmarkLRU(b *testing.B) {
"key9": "value value value value value value value value value value9",
},
}
b.Run("complex=old", func(b *testing.B) {
for i := 0; i < b.N; i++ {
l := lru.New(1)
l.Add("test", value2)
_, ok := l.Get("test")
require.True(b, ok)
}
})
b.Run("complex=new", func(b *testing.B) {
for i := 0; i < b.N; i++ {
l2 := NewLRU(&LRUOptions{
@@ -361,14 +344,6 @@ func BenchmarkLRU(b *testing.B) {
TermsOfServiceCreateAt: 111111,
}
b.Run("User=old", func(b *testing.B) {
for i := 0; i < b.N; i++ {
l := lru.New(1)
l.Add("test", user)
_, ok := l.Get("test")
require.True(b, ok)
}
})
b.Run("User=new", func(b *testing.B) {
for i := 0; i < b.N; i++ {
l2 := NewLRU(&LRUOptions{
@@ -449,14 +424,6 @@ func BenchmarkLRU(b *testing.B) {
},
}
b.Run("Post=old", func(b *testing.B) {
for i := 0; i < b.N; i++ {
l := lru.New(1)
l.Add("test", post)
_, ok := l.Get("test")
require.True(b, ok)
}
})
b.Run("Post=new", func(b *testing.B) {
for i := 0; i < b.N; i++ {
l2 := NewLRU(&LRUOptions{
@@ -480,14 +447,7 @@ func BenchmarkLRU(b *testing.B) {
LastActivityAt: 111111,
ActiveChannel: "ActiveChannel",
}
b.Run("Status=old", func(b *testing.B) {
for i := 0; i < b.N; i++ {
l := lru.New(1)
l.Add("test", status)
_, ok := l.Get("test")
require.True(b, ok)
}
})
b.Run("Status=new", func(b *testing.B) {
for i := 0; i < b.N; i++ {
l2 := NewLRU(&LRUOptions{

Просмотреть файл

Просмотреть файл

@@ -2,8 +2,10 @@
package mocks
import cache2 "github.com/mattermost/mattermost-server/v5/services/cache2"
import mock "github.com/stretchr/testify/mock"
import (
cache "github.com/mattermost/mattermost-server/v5/services/cache"
mock "github.com/stretchr/testify/mock"
)
// Provider is an autogenerated mock type for the Provider type
type Provider struct {
@@ -39,15 +41,15 @@ func (_m *Provider) Connect() error {
}
// NewCache provides a mock function with given fields: opts
func (_m *Provider) NewCache(opts *cache2.CacheOptions) cache2.Cache {
func (_m *Provider) NewCache(opts *cache.CacheOptions) cache.Cache {
ret := _m.Called(opts)
var r0 cache2.Cache
if rf, ok := ret.Get(0).(func(*cache2.CacheOptions) cache2.Cache); ok {
var r0 cache.Cache
if rf, ok := ret.Get(0).(func(*cache.CacheOptions) cache.Cache); ok {
r0 = rf(opts)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(cache2.Cache)
r0 = ret.Get(0).(cache.Cache)
}
}

Просмотреть файл

@@ -1,7 +1,7 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package cache2
package cache
import "time"

Просмотреть файл

@@ -1,7 +1,7 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package cache2
package cache
import (
"testing"

Просмотреть файл

@@ -1,49 +0,0 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package cache2
import (
"errors"
"time"
)
// ErrKeyNotFound is the error when the given key is not found
var ErrKeyNotFound = errors.New("key not found")
// Cache (under package cache2) is a representation of a cache store that aims to replace cache.Cache
type Cache interface {
// Purge is used to completely clear the cache.
Purge() error
// Set adds the given key and value to the store without an expiry. If the key already exists,
// it will overwrite the previous value.
Set(key string, value interface{}) error
// SetWithDefaultExpiry adds the given key and value to the store with the default expiry. If
// the key already exists, it will overwrite the previoous value
SetWithDefaultExpiry(key string, value interface{}) error
// SetWithExpiry adds the given key and value to the cache with the given expiry. If the key
// already exists, it will overwrite the previoous value
SetWithExpiry(key string, value interface{}, ttl time.Duration) error
// Get the content stored in the cache for the given key, and decode it into the value interface.
// Return ErrKeyNotFound if the key is missing from the cache
Get(key string, value interface{}) error
// Remove deletes the value for a given key.
Remove(key string) error
// Keys returns a slice of the keys in the cache.
Keys() ([]string, error)
// Len returns the number of items in the cache.
Len() (int, error)
// GetInvalidateClusterEvent returns the cluster event configured when this cache was created.
GetInvalidateClusterEvent() string
// Name returns the name of the cache
Name() string
}