grafana/pkg/services/secrets/manager/cache.go
Joan López de la Franca Beltran 2081f37e95
Encryption: Make DEKs cache TTL & cleanup interval configurable (#46042)
* Make DEKs cache TTL & cleanup interval configurable

* Improve 'data_keys_cache_ttl' setting description

* Fix test
2022-03-16 20:05:13 +01:00

72 lines
1.1 KiB
Go

package manager
import (
"sync"
"time"
)
var (
now = time.Now
)
type dataKeyCacheEntry struct {
dataKey []byte
expiration time.Time
}
func (e dataKeyCacheEntry) expired() bool {
return e.expiration.Before(now())
}
type dataKeyCache struct {
sync.RWMutex
entries map[string]dataKeyCacheEntry
cacheTTL time.Duration
}
func newDataKeyCache(ttl time.Duration) *dataKeyCache {
return &dataKeyCache{
entries: make(map[string]dataKeyCacheEntry),
cacheTTL: ttl,
}
}
func (c *dataKeyCache) get(id string) ([]byte, bool) {
c.RLock()
defer c.RUnlock()
entry, exists := c.entries[id]
if !exists || entry.expired() {
return nil, false
}
return entry.dataKey, true
}
func (c *dataKeyCache) add(id string, dataKey []byte) {
c.Lock()
defer c.Unlock()
c.entries[id] = dataKeyCacheEntry{
dataKey: dataKey,
expiration: now().Add(c.cacheTTL),
}
}
func (c *dataKeyCache) removeExpired() {
c.Lock()
defer c.Unlock()
for id, entry := range c.entries {
if entry.expired() {
delete(c.entries, id)
}
}
}
func (c *dataKeyCache) flush() {
c.Lock()
c.entries = make(map[string]dataKeyCacheEntry)
c.Unlock()
}