6df1e24ae3
1 - Previously, we determined if an item should be promoted in the main getter thread. This required that we protect the item.promotions variable, as both the getter and the worker were concurrently accessing it. This change pushes the conditional promotion to the worker (from the getter's point of view, items are always promoted). Since only the worker ever accesses .promotions, we no longer must protect access to it. 2 - The total size of the cache was being maintained by both the worker thread and the calling code. This required that we protect access to cache.size. Now, only the worker ever changes the size. While this simplifies much of the code, it means that we can't easily replace an item (replacement either via Set or Replace). A replcement now involves creating a new object and deleting the old one (using the existing deletables and promotable infrastructure). The only noticeable impact frmo this change is that, despite previous documentation, Replace WILL cause the item to be promoted (but it still only does so if it exists and it still doesn't extend the original TTL).
104 lines
1.8 KiB
Go
104 lines
1.8 KiB
Go
package ccache
|
|
|
|
import (
|
|
"container/list"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
type Sized interface {
|
|
Size() int64
|
|
}
|
|
|
|
type TrackedItem interface {
|
|
Value() interface{}
|
|
Release()
|
|
Expired() bool
|
|
TTL() time.Duration
|
|
Expires() time.Time
|
|
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{}
|
|
}
|
|
|
|
func (i *nilItem) Extend(duration time.Duration) {
|
|
}
|
|
|
|
var NilTracked = new(nilItem)
|
|
|
|
type Item struct {
|
|
key string
|
|
group string
|
|
promotions int32
|
|
refCount int32
|
|
expires int64
|
|
size int64
|
|
value interface{}
|
|
element *list.Element
|
|
}
|
|
|
|
func newItem(key string, value interface{}, expires int64) *Item {
|
|
size := int64(1)
|
|
if sized, ok := value.(Sized); ok {
|
|
size = sized.Size()
|
|
}
|
|
return &Item{
|
|
key: key,
|
|
value: value,
|
|
promotions: 0,
|
|
size: size,
|
|
expires: expires,
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func (i *Item) Expired() bool {
|
|
expires := atomic.LoadInt64(&i.expires)
|
|
return expires < time.Now().Unix()
|
|
}
|
|
|
|
func (i *Item) TTL() time.Duration {
|
|
expires := atomic.LoadInt64(&i.expires)
|
|
return time.Second * time.Duration(expires-time.Now().Unix())
|
|
}
|
|
|
|
func (i *Item) Expires() time.Time {
|
|
expires := atomic.LoadInt64(&i.expires)
|
|
return time.Unix(expires, 0)
|
|
}
|
|
|
|
func (i *Item) Extend(duration time.Duration) {
|
|
atomic.StoreInt64(&i.expires, time.Now().Add(duration).Unix())
|
|
}
|