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 {
|
2014-10-25 07:19:14 +02:00
|
|
|
b.RLock()
|
|
|
|
bucket, exists := b.buckets[primary]
|
|
|
|
b.RUnlock()
|
|
|
|
if exists == false {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return bucket.get(secondary)
|
|
|
|
}
|
|
|
|
|
2014-11-14 01:56:24 +01:00
|
|
|
func (b *layeredBucket) set(primary, secondary string, value interface{}, duration time.Duration) (*Item, bool) {
|
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-11-14 01:56:24 +01:00
|
|
|
item, new := bkt.set(secondary, value, duration)
|
2014-10-25 07:19:14 +02:00
|
|
|
if new {
|
|
|
|
item.group = primary
|
|
|
|
}
|
|
|
|
return item, new
|
|
|
|
}
|
|
|
|
|
2014-11-14 01:56:24 +01:00
|
|
|
func (b *layeredBucket) replace(primary, secondary string, value interface{}) bool {
|
2014-11-13 16:23:52 +01:00
|
|
|
b.Lock()
|
|
|
|
bucket, exists := b.buckets[primary]
|
|
|
|
b.Unlock()
|
|
|
|
if exists == false {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return bucket.replace(secondary, value)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|