s3browser-backend/internal/ttl_cache.go

44 lines
815 B
Go
Raw Normal View History

2021-08-01 11:00:47 +00:00
package s3browser
import (
"context"
"time"
"github.com/graph-gophers/dataloader"
gocache "github.com/patrickmn/go-cache"
)
type cache struct {
c *gocache.Cache
}
func newCache(ttl, cleanupInterval time.Duration) *cache {
return &cache{
c: gocache.New(ttl, cleanupInterval),
}
}
func (c *cache) Get(_ context.Context, key dataloader.Key) (dataloader.Thunk, bool) {
v, ok := c.c.Get(key.String())
if ok {
return v.(dataloader.Thunk), ok
}
return nil, ok
}
func (c *cache) Set(_ context.Context, key dataloader.Key, value dataloader.Thunk) {
c.c.Set(key.String(), value, 0)
}
func (c *cache) Delete(_ context.Context, key dataloader.Key) bool {
if _, found := c.c.Get(key.String()); found {
c.c.Delete(key.String())
return true
}
return false
}
func (c *cache) Clear() {
c.c.Flush()
}