-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
If implemented, will provide user with a way to cache index files. This addresses issues where the index file is loaded and unmarshalled in conncurrent reconciliation resulting in a heavy memory footprint. The caching strategy used is cahe aside, and teh cache is a k/v store with expiration. The cache number of entries and ttl for entries are configurable. The cache is optional and is disable dby default Signed-off-by: Soule BA <soule@weave.works>
- Loading branch information
Showing
7 changed files
with
366 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
package cache | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
"sync" | ||
"time" | ||
) | ||
|
||
// NOTE: this is heavily based on patrickmn/go-cache: | ||
// https://github.com/patrickmn/go-cache | ||
|
||
// Cache is a thread-safe in-memory key/value store. | ||
type Cache struct { | ||
*cache | ||
} | ||
|
||
// Item is an item stored in the cache. | ||
type Item struct { | ||
Object interface{} | ||
Expiration int64 | ||
} | ||
|
||
type cache struct { | ||
// Items holds the elements in the cache. | ||
Items map[string]Item | ||
// Maximum number of items the cache can hold. | ||
MaxItems int | ||
mu sync.RWMutex | ||
janitor *janitor | ||
} | ||
|
||
// ItemCount returns the number of items in the cache. | ||
// This may include items that have expired, but have not yet been cleaned up. | ||
func (c *cache) ItemCount() int { | ||
c.mu.RLock() | ||
n := len(c.Items) | ||
c.mu.RUnlock() | ||
return n | ||
} | ||
|
||
func (c *cache) set(key string, value interface{}, expiration time.Duration) { | ||
var e int64 | ||
if expiration > 0 { | ||
e = time.Now().Add(expiration).UnixNano() | ||
} | ||
|
||
c.Items[key] = Item{ | ||
Object: value, | ||
Expiration: e, | ||
} | ||
} | ||
|
||
// Set adds an item to the cache, replacing any existing item. | ||
// If expiration is zero, the item never expires. | ||
// If the cache is full, Set will return an error. | ||
func (c *cache) Set(key string, value interface{}, expiration time.Duration) error { | ||
c.mu.Lock() | ||
_, found := c.Items[key] | ||
if found { | ||
c.set(key, value, expiration) | ||
c.mu.Unlock() | ||
return nil | ||
} | ||
|
||
if c.MaxItems > 0 && len(c.Items) < c.MaxItems { | ||
c.set(key, value, expiration) | ||
c.mu.Unlock() | ||
return nil | ||
} | ||
|
||
c.mu.Unlock() | ||
return fmt.Errorf("Cache is full") | ||
} | ||
|
||
func (c *cache) Add(key string, value interface{}, expiration time.Duration) error { | ||
c.mu.Lock() | ||
_, found := c.Items[key] | ||
if found { | ||
c.mu.Unlock() | ||
return fmt.Errorf("Item %s already exists", key) | ||
} | ||
|
||
if c.MaxItems > 0 && len(c.Items) < c.MaxItems { | ||
c.set(key, value, expiration) | ||
c.mu.Unlock() | ||
return nil | ||
} | ||
|
||
c.mu.Unlock() | ||
return fmt.Errorf("Cache is full") | ||
} | ||
|
||
func (c *cache) Get(key string) (interface{}, bool) { | ||
c.mu.RLock() | ||
item, found := c.Items[key] | ||
if !found { | ||
c.mu.RUnlock() | ||
return nil, false | ||
} | ||
if item.Expiration > 0 { | ||
if item.Expiration < time.Now().UnixNano() { | ||
c.mu.RUnlock() | ||
return nil, false | ||
} | ||
} | ||
c.mu.RUnlock() | ||
return item.Object, true | ||
} | ||
|
||
func (c *cache) Delete(key string) { | ||
c.mu.Lock() | ||
delete(c.Items, key) | ||
c.mu.Unlock() | ||
} | ||
|
||
func (c *cache) Clear() { | ||
c.mu.Lock() | ||
c.Items = make(map[string]Item) | ||
c.mu.Unlock() | ||
} | ||
|
||
func (c *cache) HasExpired(key string) bool { | ||
c.mu.RLock() | ||
item, ok := c.Items[key] | ||
if !ok { | ||
c.mu.RUnlock() | ||
return true | ||
} | ||
if item.Expiration > 0 { | ||
if item.Expiration < time.Now().UnixNano() { | ||
c.mu.RUnlock() | ||
return true | ||
} | ||
} | ||
c.mu.RUnlock() | ||
return false | ||
} | ||
|
||
func (c *cache) SetExpiration(key string, expiration time.Duration) { | ||
c.mu.Lock() | ||
item, ok := c.Items[key] | ||
if !ok { | ||
c.mu.Unlock() | ||
return | ||
} | ||
item.Expiration = time.Now().Add(expiration).UnixNano() | ||
c.mu.Unlock() | ||
} | ||
|
||
func (c *cache) GetExpiration(key string) time.Duration { | ||
c.mu.RLock() | ||
item, ok := c.Items[key] | ||
if !ok { | ||
c.mu.RUnlock() | ||
return 0 | ||
} | ||
if item.Expiration > 0 { | ||
if item.Expiration < time.Now().UnixNano() { | ||
c.mu.RUnlock() | ||
return 0 | ||
} | ||
} | ||
c.mu.RUnlock() | ||
return time.Duration(item.Expiration - time.Now().UnixNano()) | ||
} | ||
|
||
func (c *cache) DeleteExpired() { | ||
c.mu.Lock() | ||
for k, v := range c.Items { | ||
if v.Expiration > 0 && v.Expiration < time.Now().UnixNano() { | ||
delete(c.Items, k) | ||
} | ||
} | ||
c.mu.Unlock() | ||
} | ||
|
||
type janitor struct { | ||
Interval time.Duration | ||
stop chan bool | ||
} | ||
|
||
func (j *janitor) Run(c *cache) { | ||
ticker := time.NewTicker(j.Interval) | ||
for { | ||
select { | ||
case <-ticker.C: | ||
c.DeleteExpired() | ||
case <-j.stop: | ||
ticker.Stop() | ||
return | ||
} | ||
} | ||
} | ||
|
||
func stopJanitor(c *Cache) { | ||
c.janitor.stop <- true | ||
} | ||
|
||
func New(maxItems int, interval time.Duration) *Cache { | ||
c := &cache{ | ||
Items: make(map[string]Item), | ||
MaxItems: maxItems, | ||
janitor: &janitor{ | ||
Interval: interval, | ||
stop: make(chan bool), | ||
}, | ||
} | ||
|
||
C := &Cache{c} | ||
|
||
if interval > 0 { | ||
go c.janitor.Run(c) | ||
runtime.SetFinalizer(C, stopJanitor) | ||
} | ||
|
||
return C | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package cache | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestCache(t *testing.T) { | ||
g := NewWithT(t) | ||
// create a cache that can hold 2 items and have no cleanup | ||
cache := New(2, 0) | ||
|
||
// Get an Item from the cache | ||
if _, found := cache.Get("key1"); found { | ||
t.Error("Item should not be found") | ||
} | ||
|
||
// Add an item to the cache | ||
err := cache.Add("key1", "value1", 0) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
|
||
// Get the item from the cache | ||
item, found := cache.Get("key1") | ||
g.Expect(found).To(BeTrue()) | ||
g.Expect(item).To(Equal("value1")) | ||
|
||
// Add another item to the cache | ||
err = cache.Add("key2", "value2", 0) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
g.Expect(cache.ItemCount()).To(Equal(2)) | ||
|
||
// Get the item from the cache | ||
item, found = cache.Get("key2") | ||
g.Expect(found).To(BeTrue()) | ||
g.Expect(item).To(Equal("value2")) | ||
|
||
//Add an item to the cache | ||
err = cache.Add("key3", "value3", 0) | ||
g.Expect(err).To(HaveOccurred()) | ||
|
||
// Replace an item in the cache | ||
err = cache.Set("key2", "value3", 0) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
|
||
// Get the item from the cache | ||
item, found = cache.Get("key2") | ||
g.Expect(found).To(BeTrue()) | ||
g.Expect(item).To(Equal("value3")) | ||
|
||
// new cache with a cleanup interval of 1 second | ||
cache = New(2, 1*time.Second) | ||
|
||
// Add an item to the cache | ||
err = cache.Add("key1", "value1", 2*time.Second) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
|
||
// Get the item from the cache | ||
item, found = cache.Get("key1") | ||
g.Expect(found).To(BeTrue()) | ||
g.Expect(item).To(Equal("value1")) | ||
|
||
// wait for the item to expire | ||
time.Sleep(3 * time.Second) | ||
|
||
// Get the item from the cache | ||
item, found = cache.Get("key1") | ||
g.Expect(found).To(BeFalse()) | ||
g.Expect(item).To(BeNil()) | ||
} |
Oops, something went wrong.