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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-12-28 05:11:32 +01:00
|
|
|
func (b *layeredBucket) set(primary, secondary string, value interface{}, duration time.Duration) (*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()
|
2014-12-28 05:11:32 +01:00
|
|
|
item, existing := bkt.set(secondary, value, duration)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|