[#488] Change cache key types

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2022-06-28 15:56:41 +03:00 committed by Alex Vanin
parent e104855633
commit 206a7aa395
5 changed files with 24 additions and 27 deletions

View file

@ -50,7 +50,7 @@ func NewAccessBoxCache(config *Config) *AccessBoxCache {
// Get returns a cached object.
func (o *AccessBoxCache) Get(address oid.Address) *accessbox.Box {
entry, err := o.cache.Get(address.EncodeToString())
entry, err := o.cache.Get(address)
if err != nil {
return nil
}
@ -67,5 +67,5 @@ func (o *AccessBoxCache) Get(address oid.Address) *accessbox.Box {
// Put stores an object to cache.
func (o *AccessBoxCache) Put(address oid.Address, box *accessbox.Box) error {
return o.cache.Set(address.EncodeToString(), box)
return o.cache.Set(address, box)
}

View file

@ -26,7 +26,7 @@ func TestAccessBoxCacheType(t *testing.T) {
require.Equal(t, box, val)
require.Equal(t, 0, observedLog.Len())
err = cache.cache.Set(addr.EncodeToString(), "tmp")
err = cache.cache.Set(addr, "tmp")
require.NoError(t, err)
assertInvalidCacheEntry(t, cache.Get(addr), observedLog)
}
@ -82,7 +82,7 @@ func TestObjectCacheType(t *testing.T) {
require.Equal(t, objInfo, val)
require.Equal(t, 0, observedLog.Len())
err = cache.cache.Set(addr.EncodeToString(), "tmp")
err = cache.cache.Set(addr, "tmp")
require.NoError(t, err)
assertInvalidCacheEntry(t, cache.GetObject(addr), observedLog)
}
@ -92,7 +92,7 @@ func TestObjectsListCacheType(t *testing.T) {
cache := NewObjectsListCache(DefaultObjectsListConfig(logger))
cnrID := cidtest.ID()
key := ObjectsListKey{cid: cnrID.EncodeToString(), prefix: "obj"}
key := ObjectsListKey{cid: cnrID, prefix: "obj"}
objIDs := []oid.ID{oidtest.ID()}
err := cache.Put(key, objIDs)

View file

@ -40,7 +40,7 @@ func New(config *Config) *ObjectsCache {
// GetObject returns a cached object info.
func (o *ObjectsCache) GetObject(address oid.Address) *data.ObjectInfo {
entry, err := o.cache.Get(address.EncodeToString())
entry, err := o.cache.Get(address)
if err != nil {
return nil
}
@ -57,12 +57,10 @@ func (o *ObjectsCache) GetObject(address oid.Address) *data.ObjectInfo {
// PutObject puts an object info to cache.
func (o *ObjectsCache) PutObject(obj *data.ObjectInfo) error {
cnrID := obj.CID.EncodeToString()
objID := obj.ID.EncodeToString()
return o.cache.Set(cnrID+"/"+objID, obj)
return o.cache.Set(obj.Address(), obj)
}
// Delete deletes an object from cache.
func (o *ObjectsCache) Delete(address oid.Address) bool {
return o.cache.Remove(address.EncodeToString())
return o.cache.Remove(address)
}

View file

@ -34,7 +34,7 @@ type (
// ObjectsListKey is a key to find a ObjectsListCache's entry.
ObjectsListKey struct {
cid string
cid cid.ID
prefix string
latestOnly bool
}
@ -106,7 +106,6 @@ func (l *ObjectsListCache) PutVersions(key ObjectsListKey, versions []*data.Node
// CleanCacheEntriesContainingObject deletes entries containing specified object.
func (l *ObjectsListCache) CleanCacheEntriesContainingObject(objectName string, cnr cid.ID) {
cidStr := cnr.EncodeToString()
keys := l.cache.Keys(true)
for _, key := range keys {
k, ok := key.(ObjectsListKey)
@ -115,7 +114,7 @@ func (l *ObjectsListCache) CleanCacheEntriesContainingObject(objectName string,
zap.String("expected", fmt.Sprintf("%T", k)))
continue
}
if cidStr == k.cid && strings.HasPrefix(objectName, k.prefix) {
if cnr.Equals(k.cid) && strings.HasPrefix(objectName, k.prefix) {
l.cache.Remove(k)
}
}
@ -124,7 +123,7 @@ func (l *ObjectsListCache) CleanCacheEntriesContainingObject(objectName string,
// CreateObjectsListCacheKey returns ObjectsListKey with the given CID, prefix and latestOnly flag.
func CreateObjectsListCacheKey(cnr cid.ID, prefix string, latestOnly bool) ObjectsListKey {
p := ObjectsListKey{
cid: cnr.EncodeToString(),
cid: cnr,
prefix: prefix,
latestOnly: latestOnly,
}

View file

@ -4,7 +4,7 @@ import (
"testing"
"time"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
oidtest "github.com/nspcc-dev/neofs-sdk-go/object/id/test"
"github.com/stretchr/testify/require"
@ -24,9 +24,9 @@ func getTestObjectsListConfig() *Config {
func TestObjectsListCache(t *testing.T) {
var (
listSize = 10
ids []oid.ID
userKey = "key"
listSize = 10
ids []oid.ID
cidKey, cidKey2 = cidtest.ID(), cidtest.ID()
)
for i := 0; i < listSize; i++ {
@ -37,7 +37,7 @@ func TestObjectsListCache(t *testing.T) {
var (
config = getTestObjectsListConfig()
cache = NewObjectsListCache(config)
cacheKey = ObjectsListKey{cid: userKey}
cacheKey = ObjectsListKey{cid: cidKey}
)
err := cache.Put(cacheKey, ids)
@ -54,7 +54,7 @@ func TestObjectsListCache(t *testing.T) {
t.Run("get cache with empty prefix", func(t *testing.T) {
var (
cache = NewObjectsListCache(getTestObjectsListConfig())
cacheKey = ObjectsListKey{cid: userKey}
cacheKey = ObjectsListKey{cid: cidKey}
)
err := cache.Put(cacheKey, ids)
require.NoError(t, err)
@ -69,7 +69,7 @@ func TestObjectsListCache(t *testing.T) {
t.Run("get cache with prefix", func(t *testing.T) {
cacheKey := ObjectsListKey{
cid: userKey,
cid: cidKey,
prefix: "dir",
}
@ -88,12 +88,12 @@ func TestObjectsListCache(t *testing.T) {
t.Run("get cache with other prefix", func(t *testing.T) {
var (
cacheKey = ObjectsListKey{
cid: userKey,
cid: cidKey,
prefix: "dir",
}
newKey = ObjectsListKey{
cid: "key",
cid: cidKey,
prefix: "obj",
}
)
@ -109,10 +109,10 @@ func TestObjectsListCache(t *testing.T) {
t.Run("get cache with non-existing key", func(t *testing.T) {
var (
cacheKey = ObjectsListKey{
cid: userKey,
cid: cidKey,
}
newKey = ObjectsListKey{
cid: "asdf",
cid: cidKey2,
}
)
@ -127,13 +127,13 @@ func TestObjectsListCache(t *testing.T) {
func TestCleanCacheEntriesChangedWithPutObject(t *testing.T) {
var (
id cid.ID
id = cidtest.ID()
oids = []oid.ID{oidtest.ID()}
keys []ObjectsListKey
)
for _, p := range []string{"", "dir/", "dir/lol/"} {
keys = append(keys, ObjectsListKey{cid: id.EncodeToString(), prefix: p})
keys = append(keys, ObjectsListKey{cid: id, prefix: p})
}
t.Run("put object to the root of the bucket", func(t *testing.T) {