Skip to content

Commit

Permalink
remove interface implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
knbr13 committed May 22, 2024
1 parent 31b7c7f commit ead89d1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 126 deletions.
14 changes: 7 additions & 7 deletions lru_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package incache

import (
"container/list"
"sync"
"time"
)

Expand All @@ -12,16 +13,15 @@ type lruItem[K comparable, V any] struct {
}

type LRUCache[K comparable, V any] struct {
baseCache
mu sync.RWMutex
size uint
m map[K]*list.Element // where the key-value pairs are stored
evictionList *list.List
}

func newLRU[K comparable, V any](cacheBuilder *CacheBuilder[K, V]) *LRUCache[K, V] {
func NewLRU[K comparable, V any](size uint) *LRUCache[K, V] {
return &LRUCache[K, V]{
baseCache: baseCache{
size: cacheBuilder.size,
},
size: size,
m: make(map[K]*list.Element),
evictionList: list.New(),
}
Expand Down Expand Up @@ -118,7 +118,7 @@ func (c *LRUCache[K, V]) delete(k K) {
c.evictionList.Remove(item)
}

func (src *LRUCache[K, V]) TransferTo(dst Cache[K, V]) {
func (src *LRUCache[K, V]) TransferTo(dst *LRUCache[K, V]) {
src.mu.Lock()
defer src.mu.Unlock()

Expand All @@ -130,7 +130,7 @@ func (src *LRUCache[K, V]) TransferTo(dst Cache[K, V]) {
}
}

func (src *LRUCache[K, V]) CopyTo(dst Cache[K, V]) {
func (src *LRUCache[K, V]) CopyTo(dst *LRUCache[K, V]) {
src.mu.Lock()
defer src.mu.Unlock()

Expand Down
64 changes: 16 additions & 48 deletions lru_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
)

func TestSet_LRU(t *testing.T) {
c := newLRU(&CacheBuilder[string, string]{
size: 10,
})
c := NewLRU[string, string](10)

c.Set("key1", "value1")
if c.m["key1"].Value.(*lruItem[string, string]).value != "value1" {
Expand All @@ -17,9 +15,7 @@ func TestSet_LRU(t *testing.T) {
}

func TestGet_LRU(t *testing.T) {
c := newLRU(&CacheBuilder[string, string]{
size: 10,
})
c := NewLRU[string, string](10)

c.Set("key1", "value1")
if v, ok := c.Get("key1"); !ok || v != "value1" {
Expand All @@ -28,9 +24,7 @@ func TestGet_LRU(t *testing.T) {
}

func TestGetAll_LRU(t *testing.T) {
c := newLRU(&CacheBuilder[string, string]{
size: 10,
})
c := NewLRU[string, string](10)

c.Set("key1", "value1")
c.Set("key2", "value2")
Expand All @@ -51,9 +45,7 @@ func TestGetAll_LRU(t *testing.T) {
}

func TestSetWithTimeout_LRU(t *testing.T) {
c := newLRU(&CacheBuilder[string, string]{
size: 10,
})
c := NewLRU[string, string](10)

c.SetWithTimeout("key1", "value1", time.Millisecond)

Expand All @@ -65,9 +57,7 @@ func TestSetWithTimeout_LRU(t *testing.T) {
}

func TestNotFoundSet_LRU(t *testing.T) {
c := newLRU(&CacheBuilder[string, string]{
size: 10,
})
c := NewLRU[string, string](10)

if !c.NotFoundSet("key1", "value1") {
t.Errorf("NotFoundSet failed")
Expand All @@ -79,9 +69,7 @@ func TestNotFoundSet_LRU(t *testing.T) {
}

func TestNotFoundSetWithTimeout_LRU(t *testing.T) {
c := newLRU(&CacheBuilder[string, string]{
size: 10,
})
c := NewLRU[string, string](10)

if !c.NotFoundSetWithTimeout("key1", "value1", 0) {
t.Errorf("NotFoundSetWithTimeout failed")
Expand All @@ -93,9 +81,7 @@ func TestNotFoundSetWithTimeout_LRU(t *testing.T) {
}

func TestDelete_LRU(t *testing.T) {
c := newLRU(&CacheBuilder[string, string]{
size: 10,
})
c := NewLRU[string, string](10)

c.Set("key1", "value1")

Expand All @@ -107,19 +93,15 @@ func TestDelete_LRU(t *testing.T) {
}

func TestTransferTo_LRU(t *testing.T) {
c := newLRU(&CacheBuilder[string, string]{
size: 10,
})
c := NewLRU[string, string](10)

c.Set("key1", "value1")
c.Set("key2", "value2")
c.Set("key3", "value3")
c.Set("key4", "value4")
c.Set("key5", "value5")

c2 := newLRU(&CacheBuilder[string, string]{
size: 10,
})
c2 := NewLRU[string, string](10)

c.TransferTo(c2)

Expand All @@ -133,19 +115,15 @@ func TestTransferTo_LRU(t *testing.T) {
}

func TestCopyTo_LRU(t *testing.T) {
c := newLRU(&CacheBuilder[string, string]{
size: 10,
})
c := NewLRU[string, string](10)

c.Set("key1", "value1")
c.Set("key2", "value2")
c.Set("key3", "value3")
c.Set("key4", "value4")
c.Set("key5", "value5")

c2 := newLRU(&CacheBuilder[string, string]{
size: 10,
})
c2 := NewLRU[string, string](10)

c.CopyTo(c2)

Expand All @@ -159,9 +137,7 @@ func TestCopyTo_LRU(t *testing.T) {
}

func TestKeys_LRU(t *testing.T) {
c := newLRU(&CacheBuilder[string, string]{
size: 10,
})
c := NewLRU[string, string](10)

c.Set("key1", "value1")
c.Set("key2", "value2")
Expand All @@ -177,9 +153,7 @@ func TestKeys_LRU(t *testing.T) {
}

func TestPurge_LRU(t *testing.T) {
c := newLRU(&CacheBuilder[string, string]{
size: 3,
})
c := NewLRU[string, string](10)

c.Set("key1", "value1")
c.Set("key2", "value2")
Expand All @@ -201,9 +175,7 @@ func TestPurge_LRU(t *testing.T) {
}

func TestCount_LRU(t *testing.T) {
c := newLRU(&CacheBuilder[string, string]{
size: 10,
})
c := NewLRU[string, string](10)

c.Set("key1", "value1")
c.Set("key2", "value2")
Expand All @@ -224,9 +196,7 @@ func TestCount_LRU(t *testing.T) {
}

func TestLen_LRU(t *testing.T) {
c := newLRU(&CacheBuilder[string, string]{
size: 10,
})
c := NewLRU[string, string](10)

c.Set("key1", "value1")
c.Set("key2", "value2")
Expand All @@ -247,9 +217,7 @@ func TestLen_LRU(t *testing.T) {
}

func TestEvict_LRU(t *testing.T) {
c := newLRU(&CacheBuilder[string, string]{
size: 5,
})
c := NewLRU[string, string](10)

c.Set("key1", "value1")
c.Set("key2", "value2")
Expand Down
16 changes: 8 additions & 8 deletions mcache.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package incache

import (
"sync"
"time"
)

type MCache[K comparable, V any] struct {
baseCache
mu sync.RWMutex
size uint
m map[K]valueWithTimeout[V] // where the key-value pairs are stored
stopCh chan struct{} // Channel to signal timeout goroutine to stop
timeInterval time.Duration // Time interval to sleep the goroutine that checks for expired keys
Expand All @@ -18,14 +20,12 @@ type valueWithTimeout[V any] struct {

// New creates a new cache instance with optional configuration provided by the specified options.
// The database starts a background goroutine to periodically check for expired keys based on the configured time interval.
func newManual[K comparable, V any](cacheBuilder *CacheBuilder[K, V]) *MCache[K, V] {
func NewManual[K comparable, V any](size uint, timeInterval time.Duration) *MCache[K, V] {
c := &MCache[K, V]{
m: make(map[K]valueWithTimeout[V]),
stopCh: make(chan struct{}),
timeInterval: cacheBuilder.tmIvl,
baseCache: baseCache{
size: cacheBuilder.size,
},
size: size,
timeInterval: timeInterval,
}
if c.timeInterval > 0 {
go c.expireKeys()
Expand Down Expand Up @@ -179,7 +179,7 @@ func (c *MCache[K, V]) Delete(k K) {
//
// The source cache and the destination cache are locked during the entire operation.
// The function is safe to call concurrently with other operations on any of the source cache or destination cache.
func (src *MCache[K, V]) TransferTo(dst Cache[K, V]) {
func (src *MCache[K, V]) TransferTo(dst *MCache[K, V]) {
all := src.GetAll()
src.mu.Lock()
src.m = make(map[K]valueWithTimeout[V])
Expand All @@ -194,7 +194,7 @@ func (src *MCache[K, V]) TransferTo(dst Cache[K, V]) {
//
// The source cache are the destination cache are locked during the entire operation.
// The function is safe to call concurrently with other operations on any of the source cache or Destination cache.
func (src *MCache[K, V]) CopyTo(dst Cache[K, V]) {
func (src *MCache[K, V]) CopyTo(dst *MCache[K, V]) {
all := src.GetAll()

for k, v := range all {
Expand Down
Loading

0 comments on commit ead89d1

Please sign in to comment.