[#236] cache: Refactor ListObjectsCache

Replaced map in ListObjectsCache by gcache.
Now ListObjectsCache keeps only objectIDs and
requests ObjectInfo from cache or NeoFS.
Refactored ListObjectsCache keys: removed delimiter and method fields.
Now ListObjectsCache keeps cache with all objects versions.

Signed-off-by: Angira Kekteeva <kira@nspcc.ru>
This commit is contained in:
Angira Kekteeva 2021-09-01 19:10:31 +03:00 committed by Alex Vanin
parent 1bc2e51cbc
commit 1ece42b23f
8 changed files with 171 additions and 149 deletions

View file

@ -3,16 +3,15 @@ package cache
import (
"crypto/rand"
"crypto/sha256"
"sort"
"testing"
"time"
"github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-s3-gw/api"
"github.com/stretchr/testify/require"
)
const testingCacheLifetime = 5 * time.Second
const testingCacheSize = 10
func randID(t *testing.T) *object.ID {
id := object.NewID()
@ -31,83 +30,82 @@ func randSHA256Checksum(t *testing.T) (cs [sha256.Size]byte) {
func TestObjectsListCache(t *testing.T) {
var (
cacheSize = 10
objects []*api.ObjectInfo
ids []*object.ID
userKey = "key"
)
for i := 0; i < cacheSize; i++ {
id := randID(t)
objects = append(objects, &api.ObjectInfo{ID: id, Name: id.String()})
ids = append(ids, id)
}
sort.Slice(objects, func(i, j int) bool {
return objects[i].Name < objects[j].Name
})
t.Run("lifetime", func(t *testing.T) {
var (
cache = NewObjectsListCache(testingCacheLifetime)
cacheKey = ObjectsListKey{Key: userKey}
cache = NewObjectsListCache(testingCacheSize, testingCacheLifetime)
cacheKey = ObjectsListKey{cid: userKey}
)
cache.Put(cacheKey, objects)
err := cache.Put(cacheKey, ids)
require.NoError(t, err)
condition := func() bool {
return cache.Get(cacheKey) == nil
}
require.Never(t, condition, cache.cacheLifetime, time.Second)
require.Never(t, condition, cache.lifetime, time.Second)
require.Eventually(t, condition, time.Second, 10*time.Millisecond)
})
t.Run("get cache with empty delimiter, empty prefix", func(t *testing.T) {
t.Run("get cache with empty prefix", func(t *testing.T) {
var (
cache = NewObjectsListCache(testingCacheLifetime)
cacheKey = ObjectsListKey{Key: userKey}
cache = NewObjectsListCache(testingCacheSize, testingCacheLifetime)
cacheKey = ObjectsListKey{cid: userKey}
)
cache.Put(cacheKey, objects)
err := cache.Put(cacheKey, ids)
require.NoError(t, err)
actual := cache.Get(cacheKey)
require.Equal(t, len(objects), len(actual))
for i := range objects {
require.Equal(t, objects[i], actual[i])
require.Equal(t, len(ids), len(actual))
for i := range ids {
require.Equal(t, ids[i], actual[i])
}
})
t.Run("get cache with delimiter and prefix", func(t *testing.T) {
t.Run("get cache with prefix", func(t *testing.T) {
cacheKey := ObjectsListKey{
Key: userKey,
Delimiter: "/",
Prefix: "dir",
cid: userKey,
prefix: "dir",
}
cache := NewObjectsListCache(testingCacheLifetime)
cache.Put(cacheKey, objects)
cache := NewObjectsListCache(testingCacheSize, testingCacheLifetime)
err := cache.Put(cacheKey, ids)
require.NoError(t, err)
actual := cache.Get(cacheKey)
require.Equal(t, len(objects), len(actual))
for i := range objects {
require.Equal(t, objects[i], actual[i])
require.Equal(t, len(ids), len(actual))
for i := range ids {
require.Equal(t, ids[i], actual[i])
}
})
t.Run("get cache with other delimiter and prefix", func(t *testing.T) {
t.Run("get cache with other prefix", func(t *testing.T) {
var (
cacheKey = ObjectsListKey{
Key: userKey,
Delimiter: "/",
Prefix: "dir",
cid: userKey,
prefix: "dir",
}
newKey = ObjectsListKey{
Key: "key",
Delimiter: "*",
Prefix: "obj",
cid: "key",
prefix: "obj",
}
)
cache := NewObjectsListCache(testingCacheLifetime)
cache.Put(cacheKey, objects)
cache := NewObjectsListCache(testingCacheSize, testingCacheLifetime)
err := cache.Put(cacheKey, ids)
require.NoError(t, err)
actual := cache.Get(newKey)
require.Nil(t, actual)
@ -116,15 +114,16 @@ func TestObjectsListCache(t *testing.T) {
t.Run("get cache with non-existing key", func(t *testing.T) {
var (
cacheKey = ObjectsListKey{
Key: userKey,
cid: userKey,
}
newKey = ObjectsListKey{
Key: "asdf",
cid: "asdf",
}
)
cache := NewObjectsListCache(testingCacheLifetime)
cache.Put(cacheKey, objects)
cache := NewObjectsListCache(testingCacheSize, testingCacheLifetime)
err := cache.Put(cacheKey, ids)
require.NoError(t, err)
actual := cache.Get(newKey)
require.Nil(t, actual)