summaryrefslogtreecommitdiffstats
path: root/utils/lru.go
diff options
context:
space:
mode:
authorChristopher Speller <crspeller@gmail.com>2017-07-31 08:15:23 -0700
committerGitHub <noreply@github.com>2017-07-31 08:15:23 -0700
commit09b49c26ddfdb20ced61e7dfd4192e750ce40449 (patch)
tree1288d069cc8a199b8eb3b858935dffd377ee3d2d /utils/lru.go
parent6f4e38d129ffaf469d40fc8596d3957ee94d21e9 (diff)
downloadchat-09b49c26ddfdb20ced61e7dfd4192e750ce40449.tar.gz
chat-09b49c26ddfdb20ced61e7dfd4192e750ce40449.tar.bz2
chat-09b49c26ddfdb20ced61e7dfd4192e750ce40449.zip
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
Diffstat (limited to 'utils/lru.go')
-rw-r--r--utils/lru.go45
1 files changed, 40 insertions, 5 deletions
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()