Merge pull request #46 from alexedwards/master

Add GetWithExpiration
This commit is contained in:
Patrick Mylund Nielsen 2017-04-18 19:29:47 -04:00 committed by GitHub
commit 7ac151875f
3 changed files with 125 additions and 1 deletions

View File

@ -6,3 +6,4 @@ code was contributed.)
Dustin Sallings <dustin@spy.net> Dustin Sallings <dustin@spy.net>
Jason Mooberry <jasonmoo@me.com> Jason Mooberry <jasonmoo@me.com>
Sergey Shepelev <temotor@gmail.com> Sergey Shepelev <temotor@gmail.com>
Alex Edwards <ajmedwards@gmail.com>

View File

@ -135,6 +135,36 @@ func (c *cache) Get(k string) (interface{}, bool) {
return item.Object, true 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) {
c.mu.RLock()
// "Inlining" of get and Expired
item, found := c.items[k]
if !found {
c.mu.RUnlock()
return nil, time.Time{}, false
}
if item.Expiration > 0 {
if time.Now().UnixNano() > item.Expiration {
c.mu.RUnlock()
return nil, time.Time{}, false
}
// Return the item and the expiration time
c.mu.RUnlock()
return item.Object, time.Unix(0, item.Expiration), true
}
// If expiration <= 0 (i.e. no expiration time set) then return the item
// and a zeroed time.Time
c.mu.RUnlock()
return item.Object, time.Time{}, true
}
func (c *cache) get(k string) (interface{}, bool) { func (c *cache) get(k string) (interface{}, bool) {
item, found := c.items[k] item, found := c.items[k]
if !found { if !found {

View File

@ -1676,3 +1676,96 @@ func BenchmarkDeleteExpiredLoop(b *testing.B) {
tc.DeleteExpired() 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")
}
}