-
-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
374 changed files
with
198,730 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package main | ||
|
||
import ( | ||
"math" | ||
"sync" | ||
) | ||
|
||
type cacheEntry struct { | ||
entry []byte | ||
hits int | ||
} | ||
|
||
type SimpleCache struct { | ||
maxItems int | ||
items map[string]cacheEntry | ||
lock sync.Mutex | ||
} | ||
|
||
func NewSimpleCache(maxItems int) *SimpleCache { | ||
cache := SimpleCache{ | ||
maxItems: maxItems, | ||
items: map[string]cacheEntry{}, | ||
lock: sync.Mutex{}, | ||
} | ||
|
||
return &cache | ||
} | ||
|
||
func (cache *SimpleCache) Add(cacheKey string, entry []byte) { | ||
cache.expireItems() | ||
|
||
cache.lock.Lock() | ||
defer cache.lock.Unlock() | ||
|
||
cache.items[cacheKey] = cacheEntry{ | ||
entry: entry, | ||
hits: 1, | ||
} | ||
} | ||
|
||
func (cache *SimpleCache) Get(cacheKey string) ([]byte, bool) { | ||
cache.lock.Lock() | ||
defer cache.lock.Unlock() | ||
|
||
item, ok := cache.items[cacheKey] | ||
|
||
if ok { | ||
if item.hits < 100 { | ||
item.hits++ | ||
} | ||
cache.items[cacheKey] = item | ||
return item.entry, true | ||
} | ||
|
||
return nil, false | ||
} | ||
|
||
// ExpireItems is called before any insert operation because we need to ensure we have less than | ||
// the total number of items | ||
func (cache *SimpleCache) expireItems() { | ||
cache.lock.Lock() | ||
defer cache.lock.Unlock() | ||
|
||
count := 10 | ||
if len(cache.items) >= cache.maxItems { | ||
lfuKey := "" | ||
lfuLowestCount := math.MaxInt64 | ||
|
||
for k, v := range cache.items { | ||
v.hits-- | ||
cache.items[k] = v | ||
if v.hits < lfuLowestCount { | ||
lfuKey = k | ||
lfuLowestCount = v.hits | ||
} | ||
|
||
// we only want to process X random elements so we don't spin forever | ||
count-- | ||
if count <= 0 { | ||
break | ||
} | ||
} | ||
|
||
delete(cache.items, lfuKey) | ||
} | ||
} |
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,52 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestSimpleCache_Add(t *testing.T) { | ||
simpleCache := NewSimpleCache(5) | ||
|
||
for i := 0; i < 5; i++ { | ||
simpleCache.Add(fmt.Sprintf("%d", i), []byte{}) | ||
} | ||
|
||
for i := 0; i < 4; i++ { | ||
for j := 0; j < 5; j++ { | ||
simpleCache.Get(fmt.Sprintf("%d", i)) | ||
} | ||
} | ||
|
||
simpleCache.Add("10", []byte{}) | ||
} | ||
|
||
func TestSimpleCache_Multiple(t *testing.T) { | ||
simpleCache := NewSimpleCache(10) | ||
|
||
for i := 0; i < 500; i++ { | ||
simpleCache.Add(fmt.Sprintf("%d", i), []byte{}) | ||
} | ||
|
||
simpleCache.Add("10", []byte{}) | ||
|
||
if len(simpleCache.items) != 10 { | ||
t.Errorf("expected 10 items got %v", len(simpleCache.items)) | ||
} | ||
} | ||
|
||
func TestSimpleCache_MultipleLarge(t *testing.T) { | ||
simpleCache := NewSimpleCache(10) | ||
|
||
for i := 0; i < 5000; i++ { | ||
simpleCache.Add(fmt.Sprintf("%d", i), []byte{}) | ||
simpleCache.Add("10", []byte{}) | ||
simpleCache.Get(fmt.Sprintf("%d", i)) | ||
simpleCache.Get("10") | ||
simpleCache.Get("10") | ||
} | ||
|
||
if len(simpleCache.items) != 9 { | ||
t.Errorf("expected 9 items got %v", len(simpleCache.items)) | ||
} | ||
} |
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
Oops, something went wrong.