added delete on bst
This commit is contained in:
parent
c11107bf02
commit
9e28bbffcf
58
cache.go
58
cache.go
|
@ -66,23 +66,23 @@ func (c *cache) Set(k string, x interface{}, d time.Duration) {
|
|||
}
|
||||
|
||||
func (c *cache) set(k string, x interface{}, d time.Duration) {
|
||||
e := emptyTime
|
||||
item := Item{
|
||||
Object: x,
|
||||
Key : k,
|
||||
}
|
||||
if d == DefaultExpiration {
|
||||
d = c.defaultExpiration
|
||||
}
|
||||
if d > 0 {
|
||||
e = time.Now().Add(d)
|
||||
}
|
||||
item := Item{
|
||||
Object: x,
|
||||
Expiration: e,
|
||||
Key : k,
|
||||
}
|
||||
//if an item with the same key exists in the cache, remove it from the bst
|
||||
old, found := c.items[k]
|
||||
if found {
|
||||
c.sortedItems.Delete(old)
|
||||
c.sortedItems.InsertNoReplace(item)
|
||||
item.Expiration = time.Now().Add(d)
|
||||
//if an item with the same key exists in the cache, remove it from the bst
|
||||
old, found := c.items[k]
|
||||
if found {
|
||||
c.sortedItems.Delete(old)
|
||||
c.sortedItems.InsertNoReplace(item)
|
||||
}
|
||||
} else {
|
||||
item.Expiration = emptyTime
|
||||
}
|
||||
c.items[k] = item
|
||||
}
|
||||
|
@ -144,6 +144,9 @@ func (c *cache) Increment(k string, n int64) error {
|
|||
c.mu.Unlock()
|
||||
return fmt.Errorf("Item %s not found", k)
|
||||
}
|
||||
if v.Expiration != emptyTime {
|
||||
c.sortedItems.Delete(v)
|
||||
}
|
||||
switch v.Object.(type) {
|
||||
case int:
|
||||
v.Object = v.Object.(int) + int(n)
|
||||
|
@ -176,6 +179,9 @@ func (c *cache) Increment(k string, n int64) error {
|
|||
return fmt.Errorf("The value for %s is not an integer", k)
|
||||
}
|
||||
c.items[k] = v
|
||||
if v.Expiration != emptyTime {
|
||||
c.sortedItems.InsertNoReplace(v)
|
||||
}
|
||||
c.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
@ -875,26 +881,24 @@ func (c *cache) delete(k string) (interface{}, bool) {
|
|||
return nil, false
|
||||
}
|
||||
|
||||
type keyAndValue struct {
|
||||
key string
|
||||
value interface{}
|
||||
}
|
||||
|
||||
// Delete all expired items from the cache.
|
||||
func (c *cache) DeleteExpired() {
|
||||
var evictedItems []keyAndValue
|
||||
var evictedItems []Item
|
||||
c.mu.Lock()
|
||||
for k, v := range c.items {
|
||||
if v.Expired() {
|
||||
ov, evicted := c.delete(k)
|
||||
if evicted {
|
||||
evictedItems = append(evictedItems, keyAndValue{k, ov})
|
||||
}
|
||||
}
|
||||
c.sortedItems.DescendLessOrEqual(Item{Expiration: time.Now()}, func(i llrb.Item) bool {
|
||||
v := i.(Item)
|
||||
c.delete(v.Key)
|
||||
evictedItems = append(evictedItems, v)
|
||||
return true
|
||||
})
|
||||
for _, v := range evictedItems {
|
||||
c.sortedItems.Delete(v)
|
||||
}
|
||||
c.mu.Unlock()
|
||||
for _, v := range evictedItems {
|
||||
c.onEvicted(v.key, v.value)
|
||||
if c.onEvicted != nil {
|
||||
c.onEvicted(v.Key, v.Object)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1615,3 +1615,18 @@ func BenchmarkDeleteExpired(b *testing.B) {
|
|||
tc.DeleteExpired()
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkLargeCache(b *testing.B) {
|
||||
b.StopTimer()
|
||||
tc := New(200 * time.Millisecond, 50 * time.Millisecond)
|
||||
//tc.mu.Lock()
|
||||
for i := 0; i < 100000; i++ {
|
||||
tc.set(strconv.Itoa(i), "bar", DefaultExpiration)
|
||||
}
|
||||
//tc.mu.Unlock()
|
||||
tc.DeleteExpired()
|
||||
b.StartTimer()
|
||||
for i := 100000; i <100000 + b.N; i++ {
|
||||
tc.set(strconv.Itoa(i), "bar", DefaultExpiration)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue