ccache/item.go

108 lines
1.9 KiB
Go
Raw Normal View History

2013-10-19 02:56:28 +02:00
package ccache
import (
"container/list"
"sync/atomic"
"time"
2013-10-19 02:56:28 +02:00
)
type Sized interface {
Size() int64
}
type TrackedItem interface {
Value() interface{}
Release()
Expired() bool
TTL() time.Duration
Expires() time.Time
2014-10-27 02:27:26 +01:00
Extend(duration time.Duration)
}
type nilItem struct{}
func (n *nilItem) Value() interface{} { return nil }
func (n *nilItem) Release() {}
func (i *nilItem) Expired() bool {
return true
}
func (i *nilItem) TTL() time.Duration {
return time.Minute
}
func (i *nilItem) Expires() time.Time {
return time.Time{}
}
2014-10-27 02:27:26 +01:00
func (i *nilItem) Extend(duration time.Duration) {
}
var NilTracked = new(nilItem)
2013-10-19 02:56:28 +02:00
type Item struct {
2014-10-25 07:21:10 +02:00
key string
group string
promotions int32
refCount int32
expires int64
size int64
value interface{}
element *list.Element
2013-10-19 02:56:28 +02:00
}
func newItem(key string, value interface{}, expires int64, track bool) *Item {
size := int64(1)
if sized, ok := value.(Sized); ok {
size = sized.Size()
}
item := &Item{
key: key,
value: value,
promotions: 0,
size: size,
expires: expires,
}
if track {
item.refCount = 1
}
return item
}
func (i *Item) shouldPromote(getsPerPromote int32) bool {
i.promotions += 1
return i.promotions == getsPerPromote
}
func (i *Item) Value() interface{} {
return i.value
}
func (i *Item) track() {
atomic.AddInt32(&i.refCount, 1)
}
func (i *Item) Release() {
atomic.AddInt32(&i.refCount, -1)
2013-10-19 02:56:28 +02:00
}
func (i *Item) Expired() bool {
2014-10-27 02:27:26 +01:00
expires := atomic.LoadInt64(&i.expires)
return expires < time.Now().UnixNano()
}
func (i *Item) TTL() time.Duration {
2014-10-27 02:27:26 +01:00
expires := atomic.LoadInt64(&i.expires)
return time.Nanosecond * time.Duration(expires-time.Now().UnixNano())
}
func (i *Item) Expires() time.Time {
2014-10-27 02:27:26 +01:00
expires := atomic.LoadInt64(&i.expires)
return time.Unix(0, expires)
2014-10-27 02:27:26 +01:00
}
func (i *Item) Extend(duration time.Duration) {
atomic.StoreInt64(&i.expires, time.Now().Add(duration).UnixNano())
}