Merge 738ddeb7e2
into 46f4078530
This commit is contained in:
commit
32b9b48811
16
cache.go
16
cache.go
|
@ -115,6 +115,22 @@ func (c *cache) Replace(k string, x interface{}, d time.Duration) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update only if it is already existing, keep same ttl
|
||||||
|
// Set new expiry time which is now - its expiration
|
||||||
|
func (c *cache) Update(k string, x interface{}) error {
|
||||||
|
c.mu.Lock()
|
||||||
|
_, found := c.get(k)
|
||||||
|
if !found {
|
||||||
|
c.mu.Unlock()
|
||||||
|
return fmt.Errorf("Item %s doesn't exist", k)
|
||||||
|
}
|
||||||
|
newTtl := time.Unix(0, c.items[k].Expiration)
|
||||||
|
c.set(k, x, newTtl.Sub(time.Now()))
|
||||||
|
c.mu.Unlock()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Get an item from the cache. Returns the item or nil, and a bool indicating
|
// Get an item from the cache. Returns the item or nil, and a bool indicating
|
||||||
// whether the key was found.
|
// whether the key was found.
|
||||||
func (c *cache) Get(k string) (interface{}, bool) {
|
func (c *cache) Get(k string) (interface{}, bool) {
|
||||||
|
|
|
@ -1137,6 +1137,25 @@ func TestReplace(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUpdate(t *testing.T) {
|
||||||
|
tc := New(75*time.Millisecond, 1*time.Millisecond)
|
||||||
|
tc.Set("a", 1, DefaultExpiration)
|
||||||
|
|
||||||
|
<-time.After(50 * time.Millisecond)
|
||||||
|
_, found := tc.Get("a")
|
||||||
|
if !found {
|
||||||
|
t.Error("error in update; didnt find value that was expected")
|
||||||
|
}
|
||||||
|
|
||||||
|
tc.Update("a", 2)
|
||||||
|
<-time.After(26 * time.Millisecond)
|
||||||
|
|
||||||
|
a, found := tc.Get("a")
|
||||||
|
if found || a != nil {
|
||||||
|
t.Error("Getting A found value that shouldn't exist:", a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestDelete(t *testing.T) {
|
func TestDelete(t *testing.T) {
|
||||||
tc := New(DefaultExpiration, 0)
|
tc := New(DefaultExpiration, 0)
|
||||||
tc.Set("foo", "bar", DefaultExpiration)
|
tc.Set("foo", "bar", DefaultExpiration)
|
||||||
|
|
Loading…
Reference in New Issue