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
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
6f4e38d129
Коммит
09b49c26dd
45
utils/lru.go
45
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()
|
||||
|
||||
Ссылка в новой задаче
Block a user