Skip to content

Use W-TinyLFU algorithm to enhance the LRU cache #287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/hotspot/cache/concurrent_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package cache

import "github.com/alibaba/sentinel-golang/core/hotspot/cache/stats"

// ConcurrentCounterCache cache the hotspot parameter
type ConcurrentCounterCache interface {
// Add add a value to the cache,
Expand Down Expand Up @@ -43,4 +45,7 @@ type ConcurrentCounterCache interface {

// Purge clears all cache entries.
Purge()

// Stats copies cache stats.
Stats() (*stats.CacheStats, error)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,33 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package cache
package lru

import (
"sync"

"github.com/alibaba/sentinel-golang/core/hotspot/cache"
"github.com/alibaba/sentinel-golang/core/hotspot/cache/stats"
)

// LruCacheMap use LRU strategy to cache the most frequently accessed hotspot parameter
type LruCacheMap struct {
// Not thread safe
lru *LRU
lock *sync.RWMutex
lru *LRU
sync.RWMutex
}

func (c *LruCacheMap) Add(key interface{}, value *int64) {
c.lock.Lock()
defer c.lock.Unlock()
c.Lock()
defer c.Unlock()

c.lru.Add(key, value)
return
}

func (c *LruCacheMap) AddIfAbsent(key interface{}, value *int64) (priorValue *int64) {
c.lock.Lock()
defer c.lock.Unlock()
c.Lock()
defer c.Unlock()
val := c.lru.AddIfAbsent(key, value)
if val == nil {
return nil
Expand All @@ -45,8 +48,8 @@ func (c *LruCacheMap) AddIfAbsent(key interface{}, value *int64) (priorValue *in
}

func (c *LruCacheMap) Get(key interface{}) (value *int64, isFound bool) {
c.lock.Lock()
defer c.lock.Unlock()
c.Lock()
defer c.Unlock()

val, found := c.lru.Get(key)
if found {
Expand All @@ -56,47 +59,52 @@ func (c *LruCacheMap) Get(key interface{}) (value *int64, isFound bool) {
}

func (c *LruCacheMap) Remove(key interface{}) (isFound bool) {
c.lock.Lock()
defer c.lock.Unlock()
c.Lock()
defer c.Unlock()

return c.lru.Remove(key)
}

func (c *LruCacheMap) Contains(key interface{}) (ok bool) {
c.lock.RLock()
defer c.lock.RUnlock()
c.RLock()
defer c.RUnlock()

return c.lru.Contains(key)
}

func (c *LruCacheMap) Keys() []interface{} {
c.lock.RLock()
defer c.lock.RUnlock()
c.RLock()
defer c.RUnlock()

return c.lru.Keys()
}

func (c *LruCacheMap) Len() int {
c.lock.RLock()
defer c.lock.RUnlock()
c.RLock()
defer c.RUnlock()

return c.lru.Len()
}

func (c *LruCacheMap) Purge() {
c.lock.Lock()
defer c.lock.Unlock()
c.Lock()
defer c.Unlock()

c.lru.Purge()
}

func NewLRUCacheMap(size int) ConcurrentCounterCache {
lru, err := NewLRU(size, nil)
func (c *LruCacheMap) Stats() (*stats.CacheStats, error) {
c.RUnlock()
defer c.RUnlock()
return c.lru.Stats()
}

func NewLRUCacheMap(size int, isRecordingStats bool) cache.ConcurrentCounterCache {
lru, err := NewLRU(size, nil, isRecordingStats)
if err != nil {
return nil
}
return &LruCacheMap{
lru: lru,
lock: new(sync.RWMutex),
lru: lru,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package cache
package lru

import (
"strconv"
Expand All @@ -22,7 +22,7 @@ import (
const CacheSize = 50000

func Benchmark_LRU_AddIfAbsent(b *testing.B) {
c := NewLRUCacheMap(CacheSize)
c := NewLRUCacheMap(CacheSize, false)
for a := 1; a <= CacheSize; a++ {
val := new(int64)
*val = int64(a)
Expand All @@ -42,7 +42,7 @@ func Benchmark_LRU_AddIfAbsent(b *testing.B) {
}

func Benchmark_LRU_Add(b *testing.B) {
c := NewLRUCacheMap(CacheSize)
c := NewLRUCacheMap(CacheSize, false)
for a := 1; a <= CacheSize; a++ {
val := new(int64)
*val = int64(a)
Expand All @@ -59,7 +59,7 @@ func Benchmark_LRU_Add(b *testing.B) {
}

func Benchmark_LRU_Get(b *testing.B) {
c := NewLRUCacheMap(CacheSize)
c := NewLRUCacheMap(CacheSize, false)
for a := 1; a <= CacheSize; a++ {
val := new(int64)
*val = int64(a)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package cache
package lru

import (
"strconv"
Expand All @@ -23,7 +23,7 @@ import (

func Test_concurrentLruCounterCacheMap_Add_Get(t *testing.T) {
t.Run("Test_concurrentLruCounterCacheMap_Add_Get", func(t *testing.T) {
c := NewLRUCacheMap(100)
c := NewLRUCacheMap(100, false)
for i := 1; i <= 100; i++ {
val := int64(i)
c.Add(strconv.Itoa(i), &val)
Expand All @@ -36,7 +36,7 @@ func Test_concurrentLruCounterCacheMap_Add_Get(t *testing.T) {

func Test_concurrentLruCounterCacheMap_AddIfAbsent(t *testing.T) {
t.Run("Test_concurrentLruCounterCacheMap_AddIfAbsent", func(t *testing.T) {
c := NewLRUCacheMap(100)
c := NewLRUCacheMap(100, false)
for i := 1; i <= 99; i++ {
val := int64(i)
c.Add(strconv.Itoa(i), &val)
Expand All @@ -52,7 +52,7 @@ func Test_concurrentLruCounterCacheMap_AddIfAbsent(t *testing.T) {

func Test_concurrentLruCounterCacheMap_Contains(t *testing.T) {
t.Run("Test_concurrentLruCounterCacheMap_Contains", func(t *testing.T) {
c := NewLRUCacheMap(100)
c := NewLRUCacheMap(100, false)
for i := 1; i <= 100; i++ {
val := int64(i)
c.Add(strconv.Itoa(i), &val)
Expand All @@ -69,7 +69,7 @@ func Test_concurrentLruCounterCacheMap_Contains(t *testing.T) {

func Test_concurrentLruCounterCacheMap_Keys(t *testing.T) {
t.Run("Test_concurrentLruCounterCacheMap_Add", func(t *testing.T) {
c := NewLRUCacheMap(100)
c := NewLRUCacheMap(100, false)
for i := 1; i <= 100; i++ {
val := int64(i)
c.Add(strconv.Itoa(i), &val)
Expand All @@ -82,7 +82,7 @@ func Test_concurrentLruCounterCacheMap_Keys(t *testing.T) {

func Test_concurrentLruCounterCacheMap_Purge(t *testing.T) {
t.Run("Test_concurrentLruCounterCacheMap_Add", func(t *testing.T) {
c := NewLRUCacheMap(100)
c := NewLRUCacheMap(100, false)
for i := 1; i <= 100; i++ {
val := int64(i)
c.Add(strconv.Itoa(i), &val)
Expand All @@ -95,7 +95,7 @@ func Test_concurrentLruCounterCacheMap_Purge(t *testing.T) {

func Test_concurrentLruCounterCacheMap_Remove(t *testing.T) {
t.Run("Test_concurrentLruCounterCacheMap_Add", func(t *testing.T) {
c := NewLRUCacheMap(100)
c := NewLRUCacheMap(100, false)
for i := 1; i <= 100; i++ {
val := int64(i)
c.Add(strconv.Itoa(i), &val)
Expand Down
34 changes: 32 additions & 2 deletions core/hotspot/cache/lru.go → core/hotspot/cache/lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package cache
package lru

import (
"container/list"

"github.com/alibaba/sentinel-golang/core/hotspot/cache/stats"
"github.com/pkg/errors"
)

Expand All @@ -29,6 +30,7 @@ type LRU struct {
evictList *list.List
items map[interface{}]*list.Element
onEvict EvictCallback
stats *stats.CacheStats
}

// entry is used to hold a value in the evictList
Expand All @@ -38,15 +40,20 @@ type entry struct {
}

// NewLRU constructs an LRU of the given size
func NewLRU(size int, onEvict EvictCallback) (*LRU, error) {
func NewLRU(size int, onEvict EvictCallback, isRecordingStats bool) (*LRU, error) {
if size <= 0 {
return nil, errors.New("must provide a positive size")
}
var statsCache *stats.CacheStats
if isRecordingStats {
statsCache = stats.NewCacheStats()
}
c := &LRU{
size: size,
evictList: list.New(),
items: make(map[interface{}]*list.Element, 64),
onEvict: onEvict,
stats: statsCache,
}
return c, nil
}
Expand Down Expand Up @@ -78,6 +85,9 @@ func (c *LRU) Add(key, value interface{}) {
evict := c.evictList.Len() > c.size
// Verify size not exceeded
if evict {
if c.stats != nil {
c.stats.RecordEviction()
}
c.removeOldest()
}
return
Expand All @@ -100,6 +110,9 @@ func (c *LRU) AddIfAbsent(key interface{}, value interface{}) (priorValue interf
evict := c.evictList.Len() > c.size
// Verify size not exceeded
if evict {
if c.stats != nil {
c.stats.RecordEviction()
}
c.removeOldest()
}
return nil
Expand All @@ -110,10 +123,19 @@ func (c *LRU) Get(key interface{}) (value interface{}, isFound bool) {
if ent, ok := c.items[key]; ok {
c.evictList.MoveToFront(ent)
if ent.Value.(*entry) == nil {
if c.stats != nil {
c.stats.RecordMisses()
}
return nil, false
}
if c.stats != nil {
c.stats.RecordHits()
}
return ent.Value.(*entry).value, true
}
if c.stats != nil {
c.stats.RecordMisses()
}
return
}

Expand Down Expand Up @@ -217,3 +239,11 @@ func (c *LRU) removeElement(e *list.Element) {
c.onEvict(kv.key, kv.value)
}
}

// Stats copies cache stats.
func (c LRU) Stats() (*stats.CacheStats, error) {
if c.stats == nil {
return nil, errors.New("RecordingStats Must be enabled")
}
return c.stats.Snapshot(), nil
}
Loading