ccache/bucket.go

42 lines
711 B
Go
Raw Normal View History

2013-10-19 02:56:28 +02:00
package ccache
import (
"sync"
"time"
2013-10-19 02:56:28 +02:00
)
type bucket struct {
sync.RWMutex
lookup map[string]*Item
2013-10-19 02:56:28 +02:00
}
func (b *bucket) get(key string) *Item {
b.RLock()
defer b.RUnlock()
return b.lookup[key]
2013-10-19 02:56:28 +02:00
}
func (b *bucket) set(key string, value interface{}, duration time.Duration) (*Item, *Item) {
expires := time.Now().Add(duration).UnixNano()
item := newItem(key, value, expires)
2014-11-13 16:20:12 +01:00
b.Lock()
defer b.Unlock()
existing := b.lookup[key]
b.lookup[key] = item
return item, existing
2014-11-13 16:20:12 +01:00
}
func (b *bucket) delete(key string) *Item {
b.Lock()
defer b.Unlock()
item := b.lookup[key]
delete(b.lookup, key)
return item
2013-10-19 02:56:28 +02:00
}
func (b *bucket) clear() {
b.Lock()
defer b.Unlock()
b.lookup = make(map[string]*Item)
}