ccache/bucket.go
2013-10-19 20:36:33 +08:00

37 lines
602 B
Go
Executable file

package ccache
import (
"sync"
)
type Bucket struct {
sync.RWMutex
lookup map[string]*Item
}
func (b *Bucket) get(key string) *Item {
b.RLock()
defer b.RUnlock()
return b.lookup[key]
}
func (b *Bucket) set(key string, value Value) *Item {
b.Lock()
defer b.Unlock()
if existing, exists := b.lookup[key]; exists {
existing.Lock()
existing.value = value
existing.Unlock()
return existing
}
item := &Item{key: key, value: value}
b.lookup[key] = item
return item
}
func (b *Bucket) remove(key string) {
b.Lock()
defer b.Unlock()
delete(b.lookup, key)
}