Use typed *Item in DeleteFunc
This commit is contained in:
parent
e9b7be5016
commit
a42bd4a9c8
6 changed files with 13 additions and 13 deletions
|
@ -54,7 +54,7 @@ func (b *bucket) delete(key string) *Item {
|
||||||
// the item from the map. I'm pretty sure this is 100% fine, but it is unique.
|
// the item from the map. I'm pretty sure this is 100% fine, but it is unique.
|
||||||
// (We do this so that the write to the channel is under the read lock and not the
|
// (We do this so that the write to the channel is under the read lock and not the
|
||||||
// write lock)
|
// write lock)
|
||||||
func (b *bucket) deleteFunc(matches func(key string, item interface{}) bool, deletables chan *Item) int {
|
func (b *bucket) deleteFunc(matches func(key string, item *Item) bool, deletables chan *Item) int {
|
||||||
lookup := b.lookup
|
lookup := b.lookup
|
||||||
items := make([]*Item, 0)
|
items := make([]*Item, 0)
|
||||||
|
|
||||||
|
@ -81,7 +81,7 @@ func (b *bucket) deleteFunc(matches func(key string, item interface{}) bool, del
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *bucket) deletePrefix(prefix string, deletables chan *Item) int {
|
func (b *bucket) deletePrefix(prefix string, deletables chan *Item) int {
|
||||||
return b.deleteFunc(func(key string, item interface{}) bool {
|
return b.deleteFunc(func(key string, item *Item) bool {
|
||||||
return strings.HasPrefix(key, prefix)
|
return strings.HasPrefix(key, prefix)
|
||||||
}, deletables)
|
}, deletables)
|
||||||
}
|
}
|
||||||
|
|
2
cache.go
2
cache.go
|
@ -64,7 +64,7 @@ func (c *Cache) DeletePrefix(prefix string) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deletes all items that the matches func evaluates to true.
|
// Deletes all items that the matches func evaluates to true.
|
||||||
func (c *Cache) DeleteFunc(matches func(key string, item interface{}) bool) int {
|
func (c *Cache) DeleteFunc(matches func(key string, item *Item) bool) int {
|
||||||
count := 0
|
count := 0
|
||||||
for _, b := range c.buckets {
|
for _, b := range c.buckets {
|
||||||
count += b.deleteFunc(matches, c.deletables)
|
count += b.deleteFunc(matches, c.deletables)
|
||||||
|
|
|
@ -63,17 +63,17 @@ func (_ CacheTests) DeletesAFunc() {
|
||||||
cache.Set("f", 6, time.Minute)
|
cache.Set("f", 6, time.Minute)
|
||||||
Expect(cache.ItemCount()).To.Equal(6)
|
Expect(cache.ItemCount()).To.Equal(6)
|
||||||
|
|
||||||
Expect(cache.DeleteFunc(func(key string, item interface{}) bool {
|
Expect(cache.DeleteFunc(func(key string, item *Item) bool {
|
||||||
return false
|
return false
|
||||||
})).To.Equal(0)
|
})).To.Equal(0)
|
||||||
Expect(cache.ItemCount()).To.Equal(6)
|
Expect(cache.ItemCount()).To.Equal(6)
|
||||||
|
|
||||||
Expect(cache.DeleteFunc(func(key string, item interface{}) bool {
|
Expect(cache.DeleteFunc(func(key string, item *Item) bool {
|
||||||
return item.(*Item).Value().(int) < 4
|
return item.Value().(int) < 4
|
||||||
})).To.Equal(3)
|
})).To.Equal(3)
|
||||||
Expect(cache.ItemCount()).To.Equal(3)
|
Expect(cache.ItemCount()).To.Equal(3)
|
||||||
|
|
||||||
Expect(cache.DeleteFunc(func(key string, item interface{}) bool {
|
Expect(cache.DeleteFunc(func(key string, item *Item) bool {
|
||||||
return key == "d"
|
return key == "d"
|
||||||
})).To.Equal(1)
|
})).To.Equal(1)
|
||||||
Expect(cache.ItemCount()).To.Equal(2)
|
Expect(cache.ItemCount()).To.Equal(2)
|
||||||
|
|
|
@ -71,7 +71,7 @@ func (b *layeredBucket) deletePrefix(primary, prefix string, deletables chan *It
|
||||||
return bucket.deletePrefix(prefix, deletables)
|
return bucket.deletePrefix(prefix, deletables)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *layeredBucket) deleteFunc(primary string, matches func(key string, item interface{}) bool, deletables chan *Item) int {
|
func (b *layeredBucket) deleteFunc(primary string, matches func(key string, item *Item) bool, deletables chan *Item) int {
|
||||||
b.RLock()
|
b.RLock()
|
||||||
bucket, exists := b.buckets[primary]
|
bucket, exists := b.buckets[primary]
|
||||||
b.RUnlock()
|
b.RUnlock()
|
||||||
|
|
|
@ -155,7 +155,7 @@ func (c *LayeredCache) DeletePrefix(primary, prefix string) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deletes all items that share the same primary key and where the matches func evaluates to true.
|
// Deletes all items that share the same primary key and where the matches func evaluates to true.
|
||||||
func (c *LayeredCache) DeleteFunc(primary string, matches func(key string, item interface{}) bool) int {
|
func (c *LayeredCache) DeleteFunc(primary string, matches func(key string, item *Item) bool) int {
|
||||||
return c.bucket(primary).deleteFunc(primary, matches, c.deletables)
|
return c.bucket(primary).deleteFunc(primary, matches, c.deletables)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -107,17 +107,17 @@ func (_ *LayeredCacheTests) DeletesAFunc() {
|
||||||
cache.Set("spice", "f", 6, time.Minute)
|
cache.Set("spice", "f", 6, time.Minute)
|
||||||
Expect(cache.ItemCount()).To.Equal(6)
|
Expect(cache.ItemCount()).To.Equal(6)
|
||||||
|
|
||||||
Expect(cache.DeleteFunc("spice", func(key string, item interface{}) bool {
|
Expect(cache.DeleteFunc("spice", func(key string, item *Item) bool {
|
||||||
return false
|
return false
|
||||||
})).To.Equal(0)
|
})).To.Equal(0)
|
||||||
Expect(cache.ItemCount()).To.Equal(6)
|
Expect(cache.ItemCount()).To.Equal(6)
|
||||||
|
|
||||||
Expect(cache.DeleteFunc("spice", func(key string, item interface{}) bool {
|
Expect(cache.DeleteFunc("spice", func(key string, item *Item) bool {
|
||||||
return item.(*Item).Value().(int) < 4
|
return item.Value().(int) < 4
|
||||||
})).To.Equal(2)
|
})).To.Equal(2)
|
||||||
Expect(cache.ItemCount()).To.Equal(4)
|
Expect(cache.ItemCount()).To.Equal(4)
|
||||||
|
|
||||||
Expect(cache.DeleteFunc("spice", func(key string, item interface{}) bool {
|
Expect(cache.DeleteFunc("spice", func(key string, item *Item) bool {
|
||||||
return key == "d"
|
return key == "d"
|
||||||
})).To.Equal(1)
|
})).To.Equal(1)
|
||||||
Expect(cache.ItemCount()).To.Equal(3)
|
Expect(cache.ItemCount()).To.Equal(3)
|
||||||
|
|
Loading…
Reference in a new issue