2015-04-14 18:00:51 +00:00
|
|
|
package physical
|
|
|
|
|
2015-04-15 20:48:49 +00:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/golang-lru"
|
2016-08-26 14:27:06 +00:00
|
|
|
log "github.com/mgutz/logxi/v1"
|
2015-04-15 20:48:49 +00:00
|
|
|
)
|
2015-04-14 18:00:51 +00:00
|
|
|
|
|
|
|
const (
|
2015-04-14 18:03:18 +00:00
|
|
|
// DefaultCacheSize is used if no cache size is specified for NewCache
|
2016-08-26 14:27:06 +00:00
|
|
|
DefaultCacheSize = 1024 * 1024
|
2015-04-14 18:00:51 +00:00
|
|
|
)
|
|
|
|
|
2015-04-14 18:03:18 +00:00
|
|
|
// Cache is used to wrap an underlying physical backend
|
2015-04-14 18:00:51 +00:00
|
|
|
// and provide an LRU cache layer on top. Most of the reads done by
|
|
|
|
// Vault are for policy objects so there is a large read reduction
|
|
|
|
// by using a simple write-through cache.
|
2015-04-14 18:03:18 +00:00
|
|
|
type Cache struct {
|
2015-04-14 18:00:51 +00:00
|
|
|
backend Backend
|
2016-01-07 14:21:33 +00:00
|
|
|
lru *lru.TwoQueueCache
|
2015-04-14 18:00:51 +00:00
|
|
|
}
|
|
|
|
|
2015-04-14 18:03:18 +00:00
|
|
|
// NewCache returns a physical cache of the given size.
|
2015-04-14 18:00:51 +00:00
|
|
|
// If no size is provided, the default size is used.
|
2016-08-26 14:27:06 +00:00
|
|
|
func NewCache(b Backend, size int, logger log.Logger) *Cache {
|
2015-04-14 18:00:51 +00:00
|
|
|
if size <= 0 {
|
|
|
|
size = DefaultCacheSize
|
|
|
|
}
|
2016-08-26 14:27:06 +00:00
|
|
|
if logger.IsTrace() {
|
|
|
|
logger.Trace("physical/cache: creating LRU cache", "size", size)
|
|
|
|
}
|
2016-01-07 14:21:33 +00:00
|
|
|
cache, _ := lru.New2Q(size)
|
2015-04-14 18:03:18 +00:00
|
|
|
c := &Cache{
|
2015-04-14 18:00:51 +00:00
|
|
|
backend: b,
|
|
|
|
lru: cache,
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Purge is used to clear the cache
|
2015-04-14 18:03:18 +00:00
|
|
|
func (c *Cache) Purge() {
|
2015-04-14 18:00:51 +00:00
|
|
|
c.lru.Purge()
|
|
|
|
}
|
|
|
|
|
2015-04-14 18:03:18 +00:00
|
|
|
func (c *Cache) Put(entry *Entry) error {
|
2015-04-14 18:00:51 +00:00
|
|
|
err := c.backend.Put(entry)
|
|
|
|
c.lru.Add(entry.Key, entry)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-14 18:03:18 +00:00
|
|
|
func (c *Cache) Get(key string) (*Entry, error) {
|
2015-04-14 18:00:51 +00:00
|
|
|
// Check the LRU first
|
|
|
|
if raw, ok := c.lru.Get(key); ok {
|
|
|
|
if raw == nil {
|
|
|
|
return nil, nil
|
|
|
|
} else {
|
|
|
|
return raw.(*Entry), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read from the underlying backend
|
|
|
|
ent, err := c.backend.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-15 20:48:49 +00:00
|
|
|
// Cache the result. We do NOT cache negative results
|
|
|
|
// for keys in the 'core/' prefix otherwise we risk certain
|
|
|
|
// race conditions upstream. The primary issue is with the HA mode,
|
|
|
|
// we could potentially negatively cache the leader entry and cause
|
|
|
|
// leader discovery to fail.
|
|
|
|
if ent != nil || !strings.HasPrefix(key, "core/") {
|
|
|
|
c.lru.Add(key, ent)
|
|
|
|
}
|
2015-04-14 18:00:51 +00:00
|
|
|
return ent, err
|
|
|
|
}
|
|
|
|
|
2015-04-14 18:03:18 +00:00
|
|
|
func (c *Cache) Delete(key string) error {
|
2015-04-14 18:00:51 +00:00
|
|
|
err := c.backend.Delete(key)
|
|
|
|
c.lru.Remove(key)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-14 18:03:18 +00:00
|
|
|
func (c *Cache) List(prefix string) ([]string, error) {
|
2015-04-14 18:00:51 +00:00
|
|
|
// Always pass-through as this would be difficult to cache.
|
|
|
|
return c.backend.List(prefix)
|
|
|
|
}
|