2021-08-27 22:20:40 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2021-09-01 16:10:31 +00:00
|
|
|
"fmt"
|
2021-09-01 16:10:52 +00:00
|
|
|
"strings"
|
2021-08-27 22:20:40 +00:00
|
|
|
"time"
|
|
|
|
|
2021-09-01 16:10:31 +00:00
|
|
|
"github.com/bluele/gcache"
|
2021-11-15 12:56:16 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2022-02-08 16:54:04 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2022-06-06 08:01:12 +00:00
|
|
|
"go.uber.org/zap"
|
2021-08-27 22:20:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
2022-04-13 16:56:58 +00:00
|
|
|
This is an implementation of cache which keeps unsorted lists of objects' IDs (all versions)
|
2021-09-01 22:50:01 +00:00
|
|
|
for a specified bucket and a prefix.
|
2021-08-27 22:20:40 +00:00
|
|
|
|
2021-09-01 22:50:01 +00:00
|
|
|
The cache contains gcache whose entries have a key: ObjectsListKey struct and a value: list of ids.
|
2022-04-13 16:56:58 +00:00
|
|
|
After putting a record, it lives for a while (default value is 60 seconds).
|
2021-08-27 22:20:40 +00:00
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
When we receive a request from a user, we try to find the suitable and non-expired cache entry, go through the list
|
2021-09-01 22:50:01 +00:00
|
|
|
and get ObjectInfos from common object cache or with a request to NeoFS.
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
When we put an object into a container, we invalidate entries with prefixes that are prefixes of the object's name.
|
2021-08-27 22:20:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
type (
|
2021-09-10 06:56:56 +00:00
|
|
|
// ObjectsListCache contains cache for ListObjects and ListObjectVersions.
|
|
|
|
ObjectsListCache struct {
|
2022-06-06 08:01:12 +00:00
|
|
|
cache gcache.Cache
|
|
|
|
logger *zap.Logger
|
2021-09-10 06:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ObjectsListKey is a key to find a ObjectsListCache's entry.
|
|
|
|
ObjectsListKey struct {
|
|
|
|
cid string
|
|
|
|
prefix string
|
2021-08-27 22:20:40 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-09-01 16:10:31 +00:00
|
|
|
// DefaultObjectsListCacheLifetime is a default lifetime of entries in cache of ListObjects.
|
|
|
|
DefaultObjectsListCacheLifetime = time.Second * 60
|
|
|
|
// DefaultObjectsListCacheSize is a default size of cache of ListObjects.
|
|
|
|
DefaultObjectsListCacheSize = 1e5
|
2021-08-27 22:20:40 +00:00
|
|
|
)
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// DefaultObjectsListConfig returns new default cache expiration values.
|
2022-06-06 08:01:12 +00:00
|
|
|
func DefaultObjectsListConfig(logger *zap.Logger) *Config {
|
|
|
|
return &Config{
|
|
|
|
Size: DefaultObjectsListCacheSize,
|
|
|
|
Lifetime: DefaultObjectsListCacheLifetime,
|
|
|
|
Logger: logger,
|
|
|
|
}
|
2021-09-10 06:56:56 +00:00
|
|
|
}
|
2021-08-27 22:20:40 +00:00
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// NewObjectsListCache is a constructor which creates an object of ListObjectsCache with the given lifetime of entries.
|
2021-09-10 06:56:56 +00:00
|
|
|
func NewObjectsListCache(config *Config) *ObjectsListCache {
|
|
|
|
gc := gcache.New(config.Size).LRU().Expiration(config.Lifetime).Build()
|
2022-06-06 08:01:12 +00:00
|
|
|
return &ObjectsListCache{cache: gc, logger: config.Logger}
|
2021-08-27 22:20:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// Get returns a list of ObjectInfo.
|
2022-02-08 16:54:04 +00:00
|
|
|
func (l *ObjectsListCache) Get(key ObjectsListKey) []oid.ID {
|
2021-09-01 16:10:31 +00:00
|
|
|
entry, err := l.cache.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
2021-08-27 22:20:40 +00:00
|
|
|
}
|
2021-09-01 16:10:31 +00:00
|
|
|
|
2022-02-08 16:54:04 +00:00
|
|
|
result, ok := entry.([]oid.ID)
|
2021-09-01 16:10:31 +00:00
|
|
|
if !ok {
|
2022-06-06 08:01:12 +00:00
|
|
|
l.logger.Warn("invalid cache entry type", zap.String("actual", fmt.Sprintf("%T", entry)),
|
|
|
|
zap.String("expected", "[]oid.ID"))
|
2021-09-01 16:10:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
2021-08-27 22:20:40 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 16:10:31 +00:00
|
|
|
// Put puts a list of objects to cache.
|
2022-02-08 16:54:04 +00:00
|
|
|
func (l *ObjectsListCache) Put(key ObjectsListKey, oids []oid.ID) error {
|
2021-09-01 16:10:31 +00:00
|
|
|
if len(oids) == 0 {
|
|
|
|
return fmt.Errorf("list is empty, cid: %s, prefix: %s", key.cid, key.prefix)
|
2021-08-27 22:20:40 +00:00
|
|
|
}
|
2021-09-01 16:10:31 +00:00
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
return l.cache.Set(key, oids)
|
2021-08-27 22:20:40 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 16:10:52 +00:00
|
|
|
// CleanCacheEntriesContainingObject deletes entries containing specified object.
|
2022-05-25 17:25:43 +00:00
|
|
|
func (l *ObjectsListCache) CleanCacheEntriesContainingObject(objectName string, cnr cid.ID) {
|
|
|
|
cidStr := cnr.EncodeToString()
|
2021-09-01 16:10:52 +00:00
|
|
|
keys := l.cache.Keys(true)
|
|
|
|
for _, key := range keys {
|
|
|
|
k, ok := key.(ObjectsListKey)
|
|
|
|
if !ok {
|
2022-06-06 08:01:12 +00:00
|
|
|
l.logger.Warn("invalid cache key type", zap.String("actual", fmt.Sprintf("%T", key)),
|
|
|
|
zap.String("expected", "ObjectsListKey"))
|
2021-09-01 16:10:52 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if cidStr == k.cid && strings.HasPrefix(objectName, k.prefix) {
|
|
|
|
l.cache.Remove(k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// CreateObjectsListCacheKey returns ObjectsListKey with the given CID and prefix.
|
2022-05-25 17:25:43 +00:00
|
|
|
func CreateObjectsListCacheKey(cnr cid.ID, prefix string) ObjectsListKey {
|
2021-08-27 22:20:40 +00:00
|
|
|
p := ObjectsListKey{
|
2022-05-25 17:25:43 +00:00
|
|
|
cid: cnr.EncodeToString(),
|
2021-09-01 16:10:31 +00:00
|
|
|
prefix: prefix,
|
2021-08-27 22:20:40 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 16:10:31 +00:00
|
|
|
return p
|
2021-08-27 22:20:40 +00:00
|
|
|
}
|