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"
|
|
|
|
|
|
|
|
"github.com/bluele/gcache"
|
2021-10-13 18:50:02 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/data"
|
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.).
|
|
|
|
// Key is bucketName+systemFileName.
|
|
|
|
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 {
|
2022-06-06 08:01:12 +00:00
|
|
|
o.logger.Warn("invalid cache entry type", 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
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-06-06 08:01:12 +00:00
|
|
|
o.logger.Warn("invalid cache entry type", 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 {
|
2022-06-06 08:01:12 +00:00
|
|
|
o.logger.Warn("invalid cache entry type", 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-02-17 18:58:51 +00:00
|
|
|
func (o *SystemCache) GetNotificationConfiguration(key string) *data.NotificationConfiguration {
|
|
|
|
entry, err := o.cache.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
result, ok := entry.(*data.NotificationConfiguration)
|
|
|
|
if !ok {
|
2022-06-06 08:01:12 +00:00
|
|
|
o.logger.Warn("invalid cache entry type", zap.String("actual", fmt.Sprintf("%T", entry)),
|
2022-06-06 12:03:51 +00:00
|
|
|
zap.String("expected", fmt.Sprintf("%T", result)))
|
2022-02-17 18:58:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-05-24 06:58:33 +00:00
|
|
|
func (o *SystemCache) GetObjectTagging(key string) map[string]string {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-17 18:58:51 +00:00
|
|
|
func (o *SystemCache) PutNotificationConfiguration(key string, obj *data.NotificationConfiguration) error {
|
|
|
|
return o.cache.Set(key, obj)
|
|
|
|
}
|
|
|
|
|
2022-05-24 06:58:33 +00:00
|
|
|
func (o *SystemCache) PutObjectTagging(key string, tagSet map[string]string) error {
|
|
|
|
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)
|
|
|
|
}
|