From 09b49c26ddfdb20ced61e7dfd4192e750ce40449 Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Mon, 31 Jul 2017 08:15:23 -0700 Subject: PLT-5308 Caching layer part 2 (#6973) * Adding Reaction store cache layer example * Implementing reaction store in new caching system. * Redis for reaction store * Adding redis library * Adding invalidation for DeleteAllWithEmojiName and other minor enhancements --- utils/lru.go | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) (limited to 'utils/lru.go') diff --git a/utils/lru.go b/utils/lru.go index 41ba72d71..576331563 100644 --- a/utils/lru.go +++ b/utils/lru.go @@ -14,13 +14,28 @@ import ( "time" ) +// Caching Interface +type ObjectCache interface { + AddWithExpiresInSecs(key, value interface{}, expireAtSecs int64) bool + AddWithDefaultExpires(key, value interface{}) bool + Purge() + Get(key interface{}) (value interface{}, ok bool) + Remove(key interface{}) + Len() int + Name() string + GetInvalidateClusterEvent() string +} + // Cache is a thread-safe fixed size LRU cache. type Cache struct { - size int - evictList *list.List - items map[interface{}]*list.Element - lock sync.RWMutex - onEvicted func(key interface{}, value interface{}) + size int + evictList *list.List + items map[interface{}]*list.Element + lock sync.RWMutex + onEvicted func(key interface{}, value interface{}) + name string + defaultExpiry int64 + invalidateClusterEvent string } // entry is used to hold a value in the evictList @@ -49,6 +64,14 @@ func NewLruWithEvict(size int, onEvicted func(key interface{}, value interface{} return c, nil } +func NewLruWithParams(size int, name string, defaultExpiry int64, invalidateClusterEvent string) *Cache { + lru := NewLru(size) + lru.name = name + lru.defaultExpiry = defaultExpiry + lru.invalidateClusterEvent = invalidateClusterEvent + return lru +} + // Purge is used to completely clear the cache func (c *Cache) Purge() { c.lock.Lock() @@ -68,6 +91,10 @@ func (c *Cache) Add(key, value interface{}) bool { return c.AddWithExpiresInSecs(key, value, 0) } +func (c *Cache) AddWithDefaultExpires(key, value interface{}) bool { + return c.AddWithExpiresInSecs(key, value, c.defaultExpiry) +} + // Add adds a value to the cache. Returns true if an eviction occurred. func (c *Cache) AddWithExpiresInSecs(key, value interface{}, expireAtSecs int64) bool { c.lock.Lock() @@ -159,6 +186,14 @@ func (c *Cache) Len() int { return c.evictList.Len() } +func (c *Cache) Name() string { + return c.name +} + +func (c *Cache) GetInvalidateClusterEvent() string { + return c.invalidateClusterEvent +} + // removeOldest removes the oldest item from the cache. func (c *Cache) removeOldest() { ent := c.evictList.Back() -- cgit v1.2.3-1-g7c22