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