ccache/bucket.go
Karl Seguin 751266c34a Remove Value interface, cache now works against interface{} with the
expiry specified on a Set.

Get no longer returns expired items

Items can now be deleted
2013-10-30 20:18:51 +08:00

48 lines
852 B
Go
Executable file

package ccache
import (
"sync"
"time"
)
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 interface{}, duration time.Duration) *Item {
expires := time.Now().Add(duration)
b.Lock()
defer b.Unlock()
if existing, exists := b.lookup[key]; exists {
existing.Lock()
existing.value = value
existing.expires = expires
existing.Unlock()
return existing
}
item := newItem(key, value, expires)
b.lookup[key] = item
return item
}
func (b *Bucket) delete(key string) {
b.Lock()
defer b.Unlock()
delete(b.lookup, key)
}
func (b *Bucket) getAndDelete(key string) *Item{
b.Lock()
defer b.Unlock()
item := b.lookup[key]
delete(b.lookup, key)
return item
}