2021-08-18 13:48:58 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2022-06-06 08:01:12 +00:00
|
|
|
"fmt"
|
2021-08-18 13:48:58 +00:00
|
|
|
"time"
|
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/data"
|
2023-08-23 11:07:52 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
2021-08-18 13:48:58 +00:00
|
|
|
"github.com/bluele/gcache"
|
2022-06-06 08:01:12 +00:00
|
|
|
"go.uber.org/zap"
|
2021-08-18 13:48:58 +00:00
|
|
|
)
|
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
// SystemCache provides lru cache for objects.
|
|
|
|
// This cache contains "system" objects (bucket versioning settings, tagging object etc.).
|
2022-09-07 06:59:24 +00:00
|
|
|
// Key is bucketName+systemFilePath.
|
2021-09-10 06:56:56 +00:00
|
|
|
type SystemCache struct {
|
2022-06-06 08:01:12 +00:00
|
|
|
cache gcache.Cache
|
|
|
|
logger *zap.Logger
|
2021-09-10 06:56:56 +00:00
|
|
|
}
|
2021-08-18 13:48:58 +00:00
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
const (
|
|
|
|
// DefaultSystemCacheSize is a default maximum number of entries in cache.
|
|
|
|
DefaultSystemCacheSize = 1e4
|
2022-04-13 16:56:58 +00:00
|
|
|
// DefaultSystemCacheLifetime is a default lifetime of entries in cache.
|
2021-09-10 06:56:56 +00:00
|
|
|
DefaultSystemCacheLifetime = 5 * time.Minute
|
2021-08-18 13:48:58 +00:00
|
|
|
)
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// DefaultSystemConfig returns new default cache expiration values.
|
2022-06-06 08:01:12 +00:00
|
|
|
func DefaultSystemConfig(logger *zap.Logger) *Config {
|
|
|
|
return &Config{
|
|
|
|
Size: DefaultSystemCacheSize,
|
|
|
|
Lifetime: DefaultSystemCacheLifetime,
|
|
|
|
Logger: logger,
|
|
|
|
}
|
2021-09-10 06:56:56 +00:00
|
|
|
}
|
2021-08-18 13:48:58 +00:00
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
// NewSystemCache creates an object of SystemCache.
|
|
|
|
func NewSystemCache(config *Config) *SystemCache {
|
|
|
|
gc := gcache.New(config.Size).LRU().Expiration(config.Lifetime).Build()
|
2022-06-06 08:01:12 +00:00
|
|
|
return &SystemCache{cache: gc, logger: config.Logger}
|
2021-08-18 13:48:58 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// GetObject returns a cached object.
|
2021-10-13 18:50:02 +00:00
|
|
|
func (o *SystemCache) GetObject(key string) *data.ObjectInfo {
|
2021-08-18 13:48:58 +00:00
|
|
|
entry, err := o.cache.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-13 18:50:02 +00:00
|
|
|
result, ok := entry.(*data.ObjectInfo)
|
2021-08-18 13:48:58 +00:00
|
|
|
if !ok {
|
2023-08-23 11:07:52 +00:00
|
|
|
o.logger.Warn(logs.InvalidCacheEntryType, zap.String("actual", fmt.Sprintf("%T", entry)),
|
2022-06-06 12:03:51 +00:00
|
|
|
zap.String("expected", fmt.Sprintf("%T", result)))
|
2021-08-18 13:48:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-05-26 13:11:14 +00:00
|
|
|
// GetLockInfo returns a cached object.
|
|
|
|
func (o *SystemCache) GetLockInfo(key string) *data.LockInfo {
|
|
|
|
entry, err := o.cache.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
result, ok := entry.(*data.LockInfo)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-10-13 18:50:02 +00:00
|
|
|
func (o *SystemCache) GetCORS(key string) *data.CORSConfiguration {
|
|
|
|
entry, err := o.cache.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
result, ok := entry.(*data.CORSConfiguration)
|
|
|
|
if !ok {
|
2023-08-23 11:07:52 +00:00
|
|
|
o.logger.Warn(logs.InvalidCacheEntryType, zap.String("actual", fmt.Sprintf("%T", entry)),
|
2022-06-06 12:03:51 +00:00
|
|
|
zap.String("expected", fmt.Sprintf("%T", result)))
|
2021-10-13 18:50:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-02-28 08:02:05 +00:00
|
|
|
func (o *SystemCache) GetSettings(key string) *data.BucketSettings {
|
|
|
|
entry, err := o.cache.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
result, ok := entry.(*data.BucketSettings)
|
|
|
|
if !ok {
|
2023-08-23 11:07:52 +00:00
|
|
|
o.logger.Warn(logs.InvalidCacheEntryType, zap.String("actual", fmt.Sprintf("%T", entry)),
|
2022-06-06 12:03:51 +00:00
|
|
|
zap.String("expected", fmt.Sprintf("%T", result)))
|
2022-02-28 08:02:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-05-31 09:13:55 +00:00
|
|
|
// GetTagging returns tags of a bucket or an object.
|
2022-05-25 01:58:25 +00:00
|
|
|
func (o *SystemCache) GetTagging(key string) map[string]string {
|
2022-05-24 06:58:33 +00:00
|
|
|
entry, err := o.cache.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
result, ok := entry.(map[string]string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-10-13 18:50:02 +00:00
|
|
|
// PutObject puts an object to cache.
|
|
|
|
func (o *SystemCache) PutObject(key string, obj *data.ObjectInfo) error {
|
|
|
|
return o.cache.Set(key, obj)
|
|
|
|
}
|
|
|
|
|
2022-05-26 13:11:14 +00:00
|
|
|
// PutLockInfo puts an object to cache.
|
|
|
|
func (o *SystemCache) PutLockInfo(key string, lockInfo *data.LockInfo) error {
|
|
|
|
return o.cache.Set(key, lockInfo)
|
|
|
|
}
|
|
|
|
|
2021-10-13 18:50:02 +00:00
|
|
|
func (o *SystemCache) PutCORS(key string, obj *data.CORSConfiguration) error {
|
2021-09-10 06:56:56 +00:00
|
|
|
return o.cache.Set(key, obj)
|
2021-08-18 13:48:58 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 08:02:05 +00:00
|
|
|
func (o *SystemCache) PutSettings(key string, settings *data.BucketSettings) error {
|
|
|
|
return o.cache.Set(key, settings)
|
|
|
|
}
|
|
|
|
|
2022-05-31 09:13:55 +00:00
|
|
|
// PutTagging puts tags of a bucket or an object.
|
2022-05-25 01:58:25 +00:00
|
|
|
func (o *SystemCache) PutTagging(key string, tagSet map[string]string) error {
|
2022-05-24 06:58:33 +00:00
|
|
|
return o.cache.Set(key, tagSet)
|
|
|
|
}
|
|
|
|
|
2021-08-18 13:48:58 +00:00
|
|
|
// Delete deletes an object from cache.
|
2021-09-10 06:56:56 +00:00
|
|
|
func (o *SystemCache) Delete(key string) bool {
|
2021-08-18 13:48:58 +00:00
|
|
|
return o.cache.Remove(key)
|
|
|
|
}
|