frostfs-sdk-go/pool/cache.go
Denis Kirillov ec5c223f29 [#165] pool: make private inner structs
Signed-off-by: Denis Kirillov <denis@nspcc.ru>
2022-03-21 12:41:08 +03:00

63 lines
1.1 KiB
Go

package pool
import (
"strings"
"time"
lru "github.com/hashicorp/golang-lru"
"github.com/nspcc-dev/neofs-sdk-go/session"
)
type sessionCache struct {
cache *lru.Cache
}
type cacheValue struct {
atime time.Time
token *session.Token
}
func newCache() (*sessionCache, error) {
cache, err := lru.New(100)
if err != nil {
return nil, err
}
return &sessionCache{cache: cache}, nil
}
func (c *sessionCache) Get(key string) *session.Token {
valueRaw, ok := c.cache.Get(key)
if !ok {
return nil
}
value := valueRaw.(*cacheValue)
value.atime = time.Now()
return value.token
}
func (c *sessionCache) GetAccessTime(key string) (time.Time, bool) {
valueRaw, ok := c.cache.Peek(key)
if !ok {
return time.Time{}, false
}
return valueRaw.(*cacheValue).atime, true
}
func (c *sessionCache) Put(key string, token *session.Token) bool {
return c.cache.Add(key, &cacheValue{
atime: time.Now(),
token: token,
})
}
func (c *sessionCache) DeleteByPrefix(prefix string) {
for _, key := range c.cache.Keys() {
if strings.HasPrefix(key.(string), prefix) {
c.cache.Remove(key)
}
}
}