forked from TrueCloudLab/frostfs-s3-gw
[#236] api: Refactor caches: ObjectsList, Objects
Move ObjectsList from layer to cache package Rename object_cache.go to objects.go Signed-off-by: Angira Kekteeva <kira@nspcc.ru>
This commit is contained in:
parent
239742f413
commit
1bc2e51cbc
11 changed files with 245 additions and 210 deletions
62
api/cache/objects.go
vendored
Normal file
62
api/cache/objects.go
vendored
Normal file
|
@ -0,0 +1,62 @@
|
|||
package cache
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/bluele/gcache"
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
||||
)
|
||||
|
||||
// ObjectsCache provides interface for lru cache for objects.
|
||||
type ObjectsCache interface {
|
||||
Get(address *object.Address) *object.Object
|
||||
Put(obj object.Object) error
|
||||
Delete(address *object.Address) bool
|
||||
}
|
||||
|
||||
const (
|
||||
// DefaultObjectsCacheLifetime is a default lifetime of entries in objects' cache.
|
||||
DefaultObjectsCacheLifetime = time.Minute * 5
|
||||
// DefaultObjectsCacheSize is a default maximum number of entries in objects' in cache.
|
||||
DefaultObjectsCacheSize = 1e6
|
||||
)
|
||||
|
||||
type (
|
||||
// ObjectHeadersCache contains cache with objects and lifetime of cache entries.
|
||||
ObjectHeadersCache struct {
|
||||
cache gcache.Cache
|
||||
lifetime time.Duration
|
||||
}
|
||||
)
|
||||
|
||||
// New creates an object of ObjectHeadersCache.
|
||||
func New(cacheSize int, lifetime time.Duration) *ObjectHeadersCache {
|
||||
gc := gcache.New(cacheSize).LRU().Build()
|
||||
|
||||
return &ObjectHeadersCache{cache: gc, lifetime: lifetime}
|
||||
}
|
||||
|
||||
// Get returns cached object.
|
||||
func (o *ObjectHeadersCache) Get(address *object.Address) *object.Object {
|
||||
entry, err := o.cache.Get(address.String())
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
result, ok := entry.(object.Object)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &result
|
||||
}
|
||||
|
||||
// Put puts an object to cache.
|
||||
func (o *ObjectHeadersCache) Put(obj object.Object) error {
|
||||
return o.cache.SetWithExpire(obj.ContainerID().String()+"/"+obj.ID().String(), obj, o.lifetime)
|
||||
}
|
||||
|
||||
// Delete deletes an object from cache.
|
||||
func (o *ObjectHeadersCache) Delete(address *object.Address) bool {
|
||||
return o.cache.Remove(address.String())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue