[#185] Update SDK to support new tree/pool version
Signed-off-by: Marina Biryukova <m.biryukova@yadro.com>
This commit is contained in:
parent
e1b670a727
commit
1db62f9d95
15 changed files with 299 additions and 38 deletions
65
internal/cache/netmap.go
vendored
Normal file
65
internal/cache/netmap.go
vendored
Normal file
|
@ -0,0 +1,65 @@
|
|||
package cache
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/logs"
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
||||
"github.com/bluele/gcache"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type (
|
||||
// NetmapCache provides cache for netmap.
|
||||
NetmapCache struct {
|
||||
cache gcache.Cache
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// NetmapCacheConfig stores expiration params for cache.
|
||||
NetmapCacheConfig struct {
|
||||
Lifetime time.Duration
|
||||
Logger *zap.Logger
|
||||
}
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultNetmapCacheLifetime = time.Minute
|
||||
netmapCacheSize = 1
|
||||
netmapKey = "netmap"
|
||||
)
|
||||
|
||||
// DefaultNetmapConfig returns new default cache expiration values.
|
||||
func DefaultNetmapConfig(logger *zap.Logger) *NetmapCacheConfig {
|
||||
return &NetmapCacheConfig{
|
||||
Lifetime: DefaultNetmapCacheLifetime,
|
||||
Logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// NewNetmapCache creates an object of NetmapCache.
|
||||
func NewNetmapCache(config *NetmapCacheConfig) *NetmapCache {
|
||||
gc := gcache.New(netmapCacheSize).LRU().Expiration(config.Lifetime).Build()
|
||||
return &NetmapCache{cache: gc, logger: config.Logger}
|
||||
}
|
||||
|
||||
func (c *NetmapCache) Get() *netmap.NetMap {
|
||||
entry, err := c.cache.Get(netmapKey)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
result, ok := entry.(netmap.NetMap)
|
||||
if !ok {
|
||||
c.logger.Warn(logs.InvalidCacheEntryType, zap.String("actual", fmt.Sprintf("%T", entry)),
|
||||
zap.String("expected", fmt.Sprintf("%T", result)))
|
||||
return nil
|
||||
}
|
||||
|
||||
return &result
|
||||
}
|
||||
|
||||
func (c *NetmapCache) Put(nm netmap.NetMap) error {
|
||||
return c.cache.Set(netmapKey, nm)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue