sm
/
cache
1
0
Fork 0

Merge latest master (GetWithExpiration) with LRU

This commit is contained in:
Patrick Mylund Nielsen 2017-04-18 20:01:30 -04:00
commit 795debb4ed
3 changed files with 149 additions and 1 deletions

View File

@ -7,3 +7,4 @@ Dustin Sallings <dustin@spy.net>
Jason Mooberry <jasonmoo@me.com>
Matthew Keller <m@cognusion.com>
Sergey Shepelev <temotor@gmail.com>
Alex Edwards <ajmedwards@gmail.com>

View File

@ -188,6 +188,8 @@ func (c *cache) Get(k string) (interface{}, bool) {
}
}
if c.maxItems > 0 {
item.Accessed = time.Now().UnixNano()
c.items[k] = item
c.mu.Unlock()
} else {
c.mu.RUnlock()
@ -215,6 +217,58 @@ func (c *cache) get(k string) (interface{}, bool) {
return item.Object, true
}
// GetWithExpiration returns an item and its expiration time from the cache.
// It returns the item or nil, the expiration time if one is set (if the item
// never expires a zero value for time.Time is returned), and a bool indicating
// whether the key was found.
func (c *cache) GetWithExpiration(k string) (interface{}, time.Time, bool) {
if c.maxItems > 0 {
// LRU enabled; GetWithExpiration implies write
c.mu.Lock()
} else {
// LRU not enabled; GetWithExpiration is read-only
c.mu.RLock()
}
// "Inlining" of get and Expired
item, found := c.items[k]
if !found {
if c.maxItems > 0 {
c.mu.Unlock()
} else {
c.mu.RUnlock()
}
return nil, time.Time{}, false
}
if item.Expiration > 0 {
if time.Now().UnixNano() > item.Expiration {
if c.maxItems > 0 {
c.mu.Unlock()
} else {
c.mu.RUnlock()
}
return nil, time.Time{}, false
}
if c.maxItems > 0 {
item.Accessed = time.Now().UnixNano()
c.items[k] = item
c.mu.Unlock()
} else {
c.mu.RUnlock()
}
return item.Object, time.Unix(0, item.Expiration), true
}
if c.maxItems > 0 {
item.Accessed = time.Now().UnixNano()
c.items[k] = item
c.mu.Unlock()
} else {
c.mu.RUnlock()
}
// If expiration <= 0 (i.e. no expiration time set) then return the item
// and a zeroed time.Time
return item.Object, time.Time{}, true
}
// Increment an item of type int, int8, int16, int32, int64, uintptr, uint,
// uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the
// item's value is not an integer, if it was not found, or if it is not

View File

@ -1512,7 +1512,7 @@ func BenchmarkRWMutexMapGet(b *testing.B) {
func BenchmarkRWMutexInterfaceMapGetStruct(b *testing.B) {
b.StopTimer()
s := struct{name string}{name: "foo"}
s := struct{ name string }{name: "foo"}
m := map[interface{}]string{
s: "bar",
}
@ -1729,3 +1729,96 @@ func BenchmarkDeleteExpiredLoop(b *testing.B) {
tc.DeleteExpired()
}
}
func TestGetWithExpiration(t *testing.T) {
tc := New(DefaultExpiration, 0)
a, expiration, found := tc.GetWithExpiration("a")
if found || a != nil || !expiration.IsZero() {
t.Error("Getting A found value that shouldn't exist:", a)
}
b, expiration, found := tc.GetWithExpiration("b")
if found || b != nil || !expiration.IsZero() {
t.Error("Getting B found value that shouldn't exist:", b)
}
c, expiration, found := tc.GetWithExpiration("c")
if found || c != nil || !expiration.IsZero() {
t.Error("Getting C found value that shouldn't exist:", c)
}
tc.Set("a", 1, DefaultExpiration)
tc.Set("b", "b", DefaultExpiration)
tc.Set("c", 3.5, DefaultExpiration)
tc.Set("d", 1, NoExpiration)
tc.Set("e", 1, 50*time.Millisecond)
x, expiration, found := tc.GetWithExpiration("a")
if !found {
t.Error("a was not found while getting a2")
}
if x == nil {
t.Error("x for a is nil")
} else if a2 := x.(int); a2+2 != 3 {
t.Error("a2 (which should be 1) plus 2 does not equal 3; value:", a2)
}
if !expiration.IsZero() {
t.Error("expiration for a is not a zeroed time")
}
x, expiration, found = tc.GetWithExpiration("b")
if !found {
t.Error("b was not found while getting b2")
}
if x == nil {
t.Error("x for b is nil")
} else if b2 := x.(string); b2+"B" != "bB" {
t.Error("b2 (which should be b) plus B does not equal bB; value:", b2)
}
if !expiration.IsZero() {
t.Error("expiration for b is not a zeroed time")
}
x, expiration, found = tc.GetWithExpiration("c")
if !found {
t.Error("c was not found while getting c2")
}
if x == nil {
t.Error("x for c is nil")
} else if c2 := x.(float64); c2+1.2 != 4.7 {
t.Error("c2 (which should be 3.5) plus 1.2 does not equal 4.7; value:", c2)
}
if !expiration.IsZero() {
t.Error("expiration for c is not a zeroed time")
}
x, expiration, found = tc.GetWithExpiration("d")
if !found {
t.Error("d was not found while getting d2")
}
if x == nil {
t.Error("x for d is nil")
} else if d2 := x.(int); d2+2 != 3 {
t.Error("d (which should be 1) plus 2 does not equal 3; value:", d2)
}
if !expiration.IsZero() {
t.Error("expiration for d is not a zeroed time")
}
x, expiration, found = tc.GetWithExpiration("e")
if !found {
t.Error("e was not found while getting e2")
}
if x == nil {
t.Error("x for e is nil")
} else if e2 := x.(int); e2+2 != 3 {
t.Error("e (which should be 1) plus 2 does not equal 3; value:", e2)
}
if expiration.UnixNano() != tc.items["e"].Expiration {
t.Error("expiration for e is not the correct time")
}
if expiration.UnixNano() < time.Now().UnixNano() {
t.Error("expiration for e is in the past")
}
}