fixed a bug in set
This commit is contained in:
parent
3fed5bb1fe
commit
4c8dd8263a
13
cache.go
13
cache.go
|
@ -19,7 +19,6 @@ type Item struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (item Item) Less(than llrb.Item) bool {
|
func (item Item) Less(than llrb.Item) bool {
|
||||||
//return item.Expiration.Before(than.(Item).Expiration)
|
|
||||||
return item.Expiration < than.(Item).Expiration
|
return item.Expiration < than.(Item).Expiration
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,10 +75,8 @@ func (c *cache) set(k string, x interface{}, d time.Duration) {
|
||||||
}
|
}
|
||||||
if d > 0 {
|
if d > 0 {
|
||||||
item.Expiration = time.Now().Add(d).UnixNano()
|
item.Expiration = time.Now().Add(d).UnixNano()
|
||||||
//if an item with the same key exists in the cache, remove it from the bst
|
_, found := c.items[k]
|
||||||
old, found := c.items[k]
|
if !found {
|
||||||
if found {
|
|
||||||
c.sortedItems.Delete(old)
|
|
||||||
c.sortedItems.InsertNoReplace(item)
|
c.sortedItems.InsertNoReplace(item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -162,9 +159,6 @@ func (c *cache) Increment(k string, n int64) error {
|
||||||
c.mu.Unlock()
|
c.mu.Unlock()
|
||||||
return fmt.Errorf("Item %s not found", k)
|
return fmt.Errorf("Item %s not found", k)
|
||||||
}
|
}
|
||||||
if v.Expiration != 0 {
|
|
||||||
c.sortedItems.Delete(v)
|
|
||||||
}
|
|
||||||
switch v.Object.(type) {
|
switch v.Object.(type) {
|
||||||
case int:
|
case int:
|
||||||
v.Object = v.Object.(int) + int(n)
|
v.Object = v.Object.(int) + int(n)
|
||||||
|
@ -197,9 +191,6 @@ func (c *cache) Increment(k string, n int64) error {
|
||||||
return fmt.Errorf("The value for %s is not an integer", k)
|
return fmt.Errorf("The value for %s is not an integer", k)
|
||||||
}
|
}
|
||||||
c.items[k] = v
|
c.items[k] = v
|
||||||
if v.Expiration != 0 {
|
|
||||||
c.sortedItems.InsertNoReplace(v)
|
|
||||||
}
|
|
||||||
c.mu.Unlock()
|
c.mu.Unlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue