mirror of
https://github.com/cubefs/cubefs.git
synced 2026-08-02 02:00:56 +00:00
refactor(common): refactor memcache with lru
Signed-off-by: slasher <shenjie1@oppo.com>
This commit is contained in:
parent
fcaac542ca
commit
dde69b6af0
@ -127,11 +127,11 @@ func NewVolumeGetter(clusterID proto.ClusterID, service ServiceController,
|
||||
expiration = _defaultCacheExpiration
|
||||
}
|
||||
|
||||
mc, err := memcache.NewMemCache(ctx, _defaultCacheSize)
|
||||
mc, err := memcache.NewMemCache(_defaultCacheSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
punishCache, err := memcache.NewMemCache(ctx, 1024)
|
||||
punishCache, err := memcache.NewMemCache(1024)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -175,7 +175,7 @@ func (v *volumeGetterImpl) Get(ctx context.Context, vid proto.Vid, isCache bool)
|
||||
punishedPhy.IsPunish = true
|
||||
phy = &punishedPhy
|
||||
} else {
|
||||
v.punishCache.Set(id, nil)
|
||||
v.punishCache.Remove(id)
|
||||
}
|
||||
}()
|
||||
|
||||
|
||||
@ -36,7 +36,7 @@ type (
|
||||
var cacheVidAllocator *memcache.MemCache
|
||||
|
||||
func init() {
|
||||
mc, err := memcache.NewMemCache(context.Background(), 1<<15)
|
||||
mc, err := memcache.NewMemCache(1 << 15)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@ -15,38 +15,33 @@
|
||||
package memcache
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
lru "github.com/hashicorp/golang-lru"
|
||||
)
|
||||
|
||||
// MemCache memory cache with lru-2q
|
||||
// ctx with logging
|
||||
type MemCache struct {
|
||||
ctx context.Context
|
||||
cache *lru.TwoQueueCache
|
||||
*lru.TwoQueueCache
|
||||
}
|
||||
|
||||
// NewMemCache new memory cache with capacity size
|
||||
func NewMemCache(ctx context.Context, size int) (*MemCache, error) {
|
||||
// NewMemCache new memory cache with capacity size.
|
||||
func NewMemCache(size int) (*MemCache, error) {
|
||||
cache, err := lru.New2Q(size)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &MemCache{ctx: ctx, cache: cache}, nil
|
||||
return &MemCache{cache}, nil
|
||||
}
|
||||
|
||||
// Get get by key, return nil if key doesn't exist or value is nil
|
||||
// Get by key, return nil if key doesn't exist or value is nil.
|
||||
func (mc *MemCache) Get(key interface{}) interface{} {
|
||||
value, ok := mc.cache.Get(key)
|
||||
value, ok := mc.TwoQueueCache.Get(key)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
// Set set to key with value, delete key if value is nil
|
||||
// Set key with value, delete key if value is nil.
|
||||
func (mc *MemCache) Set(key interface{}, value interface{}) {
|
||||
mc.cache.Add(key, value)
|
||||
mc.TwoQueueCache.Add(key, value)
|
||||
}
|
||||
|
||||
@ -15,7 +15,6 @@
|
||||
package memcache_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
@ -24,18 +23,18 @@ import (
|
||||
)
|
||||
|
||||
func TestNewMemCache(t *testing.T) {
|
||||
_, err := memcache.NewMemCache(context.TODO(), 1024)
|
||||
_, err := memcache.NewMemCache(1024)
|
||||
require.Nil(t, err)
|
||||
|
||||
// test 0 or -1
|
||||
_, err = memcache.NewMemCache(context.TODO(), -1)
|
||||
_, err = memcache.NewMemCache(-1)
|
||||
require.Error(t, err)
|
||||
_, err = memcache.NewMemCache(context.TODO(), 0)
|
||||
_, err = memcache.NewMemCache(0)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestMemCacheGetSet(t *testing.T) {
|
||||
mc, err := memcache.NewMemCache(context.TODO(), 4)
|
||||
mc, err := memcache.NewMemCache(4)
|
||||
require.Nil(t, err)
|
||||
|
||||
key, value := "foo", "bar"
|
||||
@ -47,8 +46,8 @@ func TestMemCacheGetSet(t *testing.T) {
|
||||
require.Equal(t, value, val)
|
||||
}
|
||||
|
||||
func TestMemCacheDel(t *testing.T) {
|
||||
mc, err := memcache.NewMemCache(context.TODO(), 4)
|
||||
func TestMemCacheSetNil(t *testing.T) {
|
||||
mc, err := memcache.NewMemCache(4)
|
||||
require.Nil(t, err)
|
||||
|
||||
key, value := "foo", "bar"
|
||||
@ -70,12 +69,12 @@ func TestMemCacheDel(t *testing.T) {
|
||||
|
||||
func BenchmarkNewMemCache(b *testing.B) {
|
||||
for ii := 0; ii < b.N; ii++ {
|
||||
memcache.NewMemCache(context.TODO(), 4)
|
||||
memcache.NewMemCache(4)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkMemCacheGet(b *testing.B) {
|
||||
mc, _ := memcache.NewMemCache(context.TODO(), 4)
|
||||
mc, _ := memcache.NewMemCache(4)
|
||||
key, value := "foo", "bar"
|
||||
mc.Set(key, value)
|
||||
for ii := 0; ii < b.N; ii++ {
|
||||
@ -84,7 +83,7 @@ func BenchmarkMemCacheGet(b *testing.B) {
|
||||
}
|
||||
|
||||
func BenchmarkMemCacheSet(b *testing.B) {
|
||||
mc, _ := memcache.NewMemCache(context.TODO(), 4)
|
||||
mc, _ := memcache.NewMemCache(4)
|
||||
key, value := "foo", "bar"
|
||||
for ii := 0; ii < b.N; ii++ {
|
||||
mc.Set(key, value)
|
||||
|
||||
@ -92,11 +92,11 @@ func New(clusterID proto.ClusterID, config ConfigCache, cmClient clustermgr.APIP
|
||||
defaulter.LessOrEqual(&config.DiskCapacity, _defaultCapacity)
|
||||
defaulter.LessOrEqual(&config.DiskExpirationS, _defaultExpirationS)
|
||||
|
||||
vc, err := memcache.NewMemCache(context.Background(), config.VolumeCapacity)
|
||||
vc, err := memcache.NewMemCache(config.VolumeCapacity)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dc, err := memcache.NewMemCache(context.Background(), config.DiskCapacity)
|
||||
dc, err := memcache.NewMemCache(config.DiskCapacity)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user