Skip to content

Commit

Permalink
add AddEx func
Browse files Browse the repository at this point in the history
  • Loading branch information
liqi committed Sep 11, 2018
1 parent 518996b commit 3c964cd
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
12 changes: 8 additions & 4 deletions 2q.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,21 +119,25 @@ func (c *TwoQueueCache) Get(key interface{}) (interface{}, bool) {
}

func (c *TwoQueueCache) Add(key, value interface{}) {
c.AddEx(key, value, 0)
}

func (c *TwoQueueCache) AddEx(key, value interface{}, expire time.Duration) {
c.lock.Lock()
defer c.lock.Unlock()

// Check if the value is frequently used already,
// and just update the value
if c.frequent.Contains(key) {
c.frequent.Add(key, value)
c.frequent.AddEx(key, value, expire)
return
}

// Check if the value is recently used, and promote
// the value into the frequent list
if c.recent.Contains(key) {
c.recent.Remove(key)
c.frequent.Add(key, value)
c.frequent.AddEx(key, value, expire)
return
}

Expand All @@ -142,13 +146,13 @@ func (c *TwoQueueCache) Add(key, value interface{}) {
if c.recentEvict.Contains(key) {
c.ensureSpace(true)
c.recentEvict.Remove(key)
c.frequent.Add(key, value)
c.frequent.AddEx(key, value, expire)
return
}

// Add to the recently seen list
c.ensureSpace(false)
c.recent.Add(key, value)
c.recent.AddEx(key, value, expire)
return
}

Expand Down
15 changes: 10 additions & 5 deletions arc.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,25 @@ func (c *ARCCache) Get(key interface{}) (interface{}, bool) {

// Add adds a value to the cache.
func (c *ARCCache) Add(key, value interface{}) {
c.AddEx(key, value, 0)
}

// AddEx adds a value to the cache.
func (c *ARCCache) AddEx(key, value interface{}, expire time.Duration) {
c.lock.Lock()
defer c.lock.Unlock()

// Check if the value is contained in T1 (recent), and potentially
// promote it to frequent T2
if c.t1.Contains(key) {
c.t1.Remove(key)
c.t2.Add(key, value)
c.t2.AddEx(key, value, expire)
return
}

// Check if the value is already in T2 (frequent) and update it
if c.t2.Contains(key) {
c.t2.Add(key, value)
c.t2.AddEx(key, value, expire)
return
}

Expand Down Expand Up @@ -131,7 +136,7 @@ func (c *ARCCache) Add(key, value interface{}) {
c.b1.Remove(key)

// Add the key to the frequently used list
c.t2.Add(key, value)
c.t2.AddEx(key, value, expire)
return
}

Expand Down Expand Up @@ -160,7 +165,7 @@ func (c *ARCCache) Add(key, value interface{}) {
c.b2.Remove(key)

// Add the key to the frequntly used list
c.t2.Add(key, value)
c.t2.AddEx(key, value, expire)
return
}

Expand All @@ -178,7 +183,7 @@ func (c *ARCCache) Add(key, value interface{}) {
}

// Add to the recently seen list
c.t1.Add(key, value)
c.t1.AddEx(key, value, expire)
return
}

Expand Down
7 changes: 7 additions & 0 deletions lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ func (c *Cache) Add(key, value interface{}) bool {
return c.lru.Add(key, value)
}

// AddEx adds a value to the cache. Returns true if an eviction occurred.
func (c *Cache) AddEx(key, value interface{}, expire time.Duration) bool {
c.lock.Lock()
defer c.lock.Unlock()
return c.lru.AddEx(key, value, expire)
}

// Get looks up a key's value from the cache.
func (c *Cache) Get(key interface{}) (interface{}, bool) {
c.lock.Lock()
Expand Down
10 changes: 9 additions & 1 deletion simplelru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,16 @@ func (c *LRU) Purge() {

// Add adds a value to the cache. Returns true if an eviction occurred.
func (c *LRU) Add(key, value interface{}) bool {
return c.AddEx(key, value, 0)
}

// AddEx adds a value to the cache with expire. Returns true if an eviction occurred.
func (c *LRU) AddEx(key, value interface{}, expire time.Duration) bool {
var ex *time.Time = nil
if c.expire != 0 {
if expire > 0 {
expire := time.Now().Add(expire)
ex = &expire
} else if c.expire > 0 {
expire := time.Now().Add(c.expire)
ex = &expire
}
Expand Down
10 changes: 10 additions & 0 deletions simplelru/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,14 @@ func TestLRU_Expire(t *testing.T) {
if l.Contains(1) {
t.Errorf("1 should not be contained")
}

l.AddEx(1, 1, 1*time.Second)

if !l.Contains(1) {
t.Errorf("1 should be contained")
}
time.Sleep(1 * time.Second)
if l.Contains(1) {
t.Errorf("1 should not be contained")
}
}

0 comments on commit 3c964cd

Please sign in to comment.