2014-10-25 07:19:14 +02:00
|
|
|
package ccache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-11-14 01:56:24 +01:00
|
|
|
type layeredBucket struct {
|
2014-10-25 07:19:14 +02:00
|
|
|
sync.RWMutex
|
2014-11-14 01:56:24 +01:00
|
|
|
buckets map[string]*bucket
|
2014-10-25 07:19:14 +02:00
|
|
|
}
|
|
|
|
|
2019-01-26 06:33:50 +01:00
|
|
|
func (b *layeredBucket) itemCount() int {
|
|
|
|
count := 0
|
|
|
|
b.RLock()
|
|
|
|
defer b.RUnlock()
|
|
|
|
for _, b := range b.buckets {
|
|
|
|
count += b.itemCount()
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2014-11-14 01:56:24 +01:00
|
|
|
func (b *layeredBucket) get(primary, secondary string) *Item {
|
2016-11-01 17:01:39 +01:00
|
|
|
bucket := b.getSecondaryBucket(primary)
|
|
|
|
if bucket == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return bucket.get(secondary)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *layeredBucket) getSecondaryBucket(primary string) *bucket {
|
2014-10-25 07:19:14 +02:00
|
|
|
b.RLock()
|
|
|
|
bucket, exists := b.buckets[primary]
|
|
|
|
b.RUnlock()
|
|
|
|
if exists == false {
|
|
|
|
return nil
|
|
|
|
}
|
2016-11-01 17:01:39 +01:00
|
|
|
return bucket
|
2014-10-25 07:19:14 +02:00
|
|
|
}
|
|
|
|
|
2020-08-14 05:15:13 +02:00
|
|
|
func (b *layeredBucket) set(primary, secondary string, value interface{}, duration time.Duration, track bool) (*Item, *Item) {
|
2014-10-25 07:19:14 +02:00
|
|
|
b.Lock()
|
2014-11-14 01:56:24 +01:00
|
|
|
bkt, exists := b.buckets[primary]
|
2014-10-25 07:19:14 +02:00
|
|
|
if exists == false {
|
2014-11-14 01:56:24 +01:00
|
|
|
bkt = &bucket{lookup: make(map[string]*Item)}
|
|
|
|
b.buckets[primary] = bkt
|
2014-10-25 07:19:14 +02:00
|
|
|
}
|
|
|
|
b.Unlock()
|
2020-08-14 05:15:13 +02:00
|
|
|
item, existing := bkt.set(secondary, value, duration, track)
|
2014-12-28 05:11:32 +01:00
|
|
|
item.group = primary
|
|
|
|
return item, existing
|
2014-11-13 16:23:52 +01:00
|
|
|
}
|
|
|
|
|
2014-11-14 01:56:24 +01:00
|
|
|
func (b *layeredBucket) delete(primary, secondary string) *Item {
|
2014-10-25 07:19:14 +02:00
|
|
|
b.RLock()
|
|
|
|
bucket, exists := b.buckets[primary]
|
|
|
|
b.RUnlock()
|
|
|
|
if exists == false {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return bucket.delete(secondary)
|
|
|
|
}
|
|
|
|
|
2020-08-11 18:53:40 +02:00
|
|
|
func (b *layeredBucket) deletePrefix(primary, prefix string, deletables chan *Item) int {
|
|
|
|
b.RLock()
|
|
|
|
bucket, exists := b.buckets[primary]
|
|
|
|
b.RUnlock()
|
|
|
|
if exists == false {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return bucket.deletePrefix(prefix, deletables)
|
|
|
|
}
|
|
|
|
|
2020-08-13 16:10:22 +02:00
|
|
|
func (b *layeredBucket) deleteFunc(primary string, matches func(key string, item *Item) bool, deletables chan *Item) int {
|
2020-08-12 17:30:28 +02:00
|
|
|
b.RLock()
|
|
|
|
bucket, exists := b.buckets[primary]
|
|
|
|
b.RUnlock()
|
|
|
|
if exists == false {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return bucket.deleteFunc(matches, deletables)
|
|
|
|
}
|
|
|
|
|
2014-11-14 01:56:24 +01:00
|
|
|
func (b *layeredBucket) deleteAll(primary string, deletables chan *Item) bool {
|
2014-10-25 07:19:14 +02:00
|
|
|
b.RLock()
|
|
|
|
bucket, exists := b.buckets[primary]
|
|
|
|
b.RUnlock()
|
|
|
|
if exists == false {
|
2014-10-27 02:30:48 +01:00
|
|
|
return false
|
2014-10-25 07:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bucket.Lock()
|
|
|
|
defer bucket.Unlock()
|
|
|
|
|
|
|
|
if l := len(bucket.lookup); l == 0 {
|
2014-10-27 02:30:48 +01:00
|
|
|
return false
|
2014-10-25 07:19:14 +02:00
|
|
|
}
|
|
|
|
for key, item := range bucket.lookup {
|
|
|
|
delete(bucket.lookup, key)
|
|
|
|
deletables <- item
|
|
|
|
}
|
2014-10-27 02:30:48 +01:00
|
|
|
return true
|
2014-10-25 07:19:14 +02:00
|
|
|
}
|
|
|
|
|
2014-11-14 01:56:24 +01:00
|
|
|
func (b *layeredBucket) clear() {
|
2014-10-25 07:19:14 +02:00
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
|
|
|
for _, bucket := range b.buckets {
|
|
|
|
bucket.clear()
|
|
|
|
}
|
2014-11-14 01:56:24 +01:00
|
|
|
b.buckets = make(map[string]*bucket)
|
2014-10-25 07:19:14 +02:00
|
|
|
}
|