2021-08-18 13:48:58 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/bluele/gcache"
|
2021-08-19 06:55:22 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
2021-08-18 13:48:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// SystemCache provides interface for lru cache for objects.
|
2021-08-19 06:55:22 +00:00
|
|
|
// This cache contains "system" objects (bucket versioning settings, tagging object etc.).
|
|
|
|
// Key is bucketName+systemFileName.
|
2021-08-18 13:48:58 +00:00
|
|
|
SystemCache interface {
|
|
|
|
Get(key string) *object.Object
|
|
|
|
Put(key string, obj *object.Object) error
|
|
|
|
Delete(key string) bool
|
|
|
|
}
|
|
|
|
|
2021-08-19 06:55:22 +00:00
|
|
|
// SysCache contains cache with objects and lifetime of cache entries.
|
|
|
|
SysCache struct {
|
2021-08-18 13:48:58 +00:00
|
|
|
cache gcache.Cache
|
|
|
|
lifetime time.Duration
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewSystemCache creates an object of SystemCache.
|
2021-08-19 06:55:22 +00:00
|
|
|
func NewSystemCache(cacheSize int, lifetime time.Duration) *SysCache {
|
2021-08-18 13:48:58 +00:00
|
|
|
gc := gcache.New(cacheSize).LRU().Build()
|
|
|
|
|
2021-08-19 06:55:22 +00:00
|
|
|
return &SysCache{cache: gc, lifetime: lifetime}
|
2021-08-18 13:48:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns cached object.
|
2021-08-19 06:55:22 +00:00
|
|
|
func (o *SysCache) Get(key string) *object.Object {
|
2021-08-18 13:48:58 +00:00
|
|
|
entry, err := o.cache.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
result, ok := entry.(*object.Object)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put puts an object to cache.
|
2021-08-19 06:55:22 +00:00
|
|
|
func (o *SysCache) Put(key string, obj *object.Object) error {
|
2021-08-18 13:48:58 +00:00
|
|
|
return o.cache.SetWithExpire(key, obj, o.lifetime)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes an object from cache.
|
2021-08-19 06:55:22 +00:00
|
|
|
func (o *SysCache) Delete(key string) bool {
|
2021-08-18 13:48:58 +00:00
|
|
|
return o.cache.Remove(key)
|
|
|
|
}
|