2014-02-28 13:10:42 +01:00
|
|
|
// An LRU cached aimed at high concurrency
|
2013-10-19 02:56:28 +02:00
|
|
|
package ccache
|
|
|
|
|
|
|
|
import (
|
2014-02-28 13:10:42 +01:00
|
|
|
"container/list"
|
|
|
|
"hash/fnv"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
2013-10-19 02:56:28 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Cache struct {
|
2014-02-28 13:10:42 +01:00
|
|
|
*Configuration
|
|
|
|
list *list.List
|
|
|
|
buckets []*Bucket
|
|
|
|
bucketCount uint32
|
|
|
|
deletables chan *Item
|
|
|
|
promotables chan *Item
|
2013-10-19 02:56:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(config *Configuration) *Cache {
|
2014-02-28 13:10:42 +01:00
|
|
|
c := &Cache{
|
|
|
|
list: list.New(),
|
|
|
|
Configuration: config,
|
|
|
|
bucketCount: uint32(config.buckets),
|
|
|
|
buckets: make([]*Bucket, config.buckets),
|
|
|
|
deletables: make(chan *Item, config.deleteBuffer),
|
|
|
|
promotables: make(chan *Item, config.promoteBuffer),
|
|
|
|
}
|
2014-10-25 02:46:18 +02:00
|
|
|
for i := 0; i < int(config.buckets); i++ {
|
2014-02-28 13:10:42 +01:00
|
|
|
c.buckets[i] = &Bucket{
|
|
|
|
lookup: make(map[string]*Item),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
go c.worker()
|
|
|
|
return c
|
2013-10-19 02:56:28 +02:00
|
|
|
}
|
|
|
|
|
2013-10-30 13:18:51 +01:00
|
|
|
func (c *Cache) Get(key string) interface{} {
|
2014-02-28 13:10:42 +01:00
|
|
|
if item := c.get(key); item != nil {
|
|
|
|
return item.value
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) TrackingGet(key string) TrackedItem {
|
|
|
|
item := c.get(key)
|
|
|
|
if item == nil {
|
|
|
|
return NilTracked
|
|
|
|
}
|
|
|
|
item.track()
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) get(key string) *Item {
|
|
|
|
bucket := c.bucket(key)
|
|
|
|
item := bucket.get(key)
|
|
|
|
if item == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2014-10-25 03:35:52 +02:00
|
|
|
if item.expires < time.Now().Unix() {
|
2014-02-28 13:10:42 +01:00
|
|
|
c.deleteItem(bucket, item)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c.conditionalPromote(item)
|
|
|
|
return item
|
2013-10-19 02:56:28 +02:00
|
|
|
}
|
|
|
|
|
2013-10-30 13:18:51 +01:00
|
|
|
func (c *Cache) Set(key string, value interface{}, duration time.Duration) {
|
2014-02-28 13:10:42 +01:00
|
|
|
item, new := c.bucket(key).set(key, value, duration)
|
|
|
|
if new {
|
|
|
|
c.promote(item)
|
|
|
|
} else {
|
|
|
|
c.conditionalPromote(item)
|
|
|
|
}
|
2013-10-19 02:56:28 +02:00
|
|
|
}
|
|
|
|
|
2013-10-31 04:45:22 +01:00
|
|
|
func (c *Cache) Fetch(key string, duration time.Duration, fetch func() (interface{}, error)) (interface{}, error) {
|
2014-02-28 13:10:42 +01:00
|
|
|
item := c.Get(key)
|
|
|
|
if item != nil {
|
|
|
|
return item, nil
|
|
|
|
}
|
|
|
|
value, err := fetch()
|
|
|
|
if err == nil {
|
|
|
|
c.Set(key, value, duration)
|
|
|
|
}
|
|
|
|
return value, err
|
2013-10-30 13:24:43 +01:00
|
|
|
}
|
|
|
|
|
2013-10-30 13:18:51 +01:00
|
|
|
func (c *Cache) Delete(key string) {
|
2014-10-25 07:19:14 +02:00
|
|
|
item := c.bucket(key).delete(key)
|
2014-02-28 13:10:42 +01:00
|
|
|
if item != nil {
|
|
|
|
c.deletables <- item
|
|
|
|
}
|
2013-10-30 13:18:51 +01:00
|
|
|
}
|
|
|
|
|
2013-10-31 04:45:22 +01:00
|
|
|
//this isn't thread safe. It's meant to be called from non-concurrent tests
|
|
|
|
func (c *Cache) Clear() {
|
2014-02-28 13:10:42 +01:00
|
|
|
for _, bucket := range c.buckets {
|
|
|
|
bucket.clear()
|
|
|
|
}
|
|
|
|
c.list = list.New()
|
2013-10-31 04:45:22 +01:00
|
|
|
}
|
|
|
|
|
2013-10-30 13:18:51 +01:00
|
|
|
func (c *Cache) deleteItem(bucket *Bucket, item *Item) {
|
2014-10-25 07:19:14 +02:00
|
|
|
bucket.delete(item.key) //stop other GETs from getting it
|
2014-02-28 13:10:42 +01:00
|
|
|
c.deletables <- item
|
2013-10-30 13:18:51 +01:00
|
|
|
}
|
|
|
|
|
2013-10-19 02:56:28 +02:00
|
|
|
func (c *Cache) bucket(key string) *Bucket {
|
2014-02-28 13:10:42 +01:00
|
|
|
h := fnv.New32a()
|
|
|
|
h.Write([]byte(key))
|
2014-10-25 02:46:18 +02:00
|
|
|
return c.buckets[h.Sum32()%c.bucketCount]
|
2013-10-19 02:56:28 +02:00
|
|
|
}
|
|
|
|
|
2013-11-13 06:46:41 +01:00
|
|
|
func (c *Cache) conditionalPromote(item *Item) {
|
2014-02-28 13:10:42 +01:00
|
|
|
if item.shouldPromote(c.getsPerPromote) == false {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.promote(item)
|
2013-11-13 06:46:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) promote(item *Item) {
|
2014-02-28 13:10:42 +01:00
|
|
|
c.promotables <- item
|
2013-10-19 02:56:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) worker() {
|
2014-02-28 13:10:42 +01:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case item := <-c.promotables:
|
2014-10-25 02:46:18 +02:00
|
|
|
if c.doPromote(item) && uint64(c.list.Len()) > c.maxItems {
|
2014-02-28 13:10:42 +01:00
|
|
|
c.gc()
|
|
|
|
}
|
|
|
|
case item := <-c.deletables:
|
2014-10-25 07:24:52 +02:00
|
|
|
if item.element == nil {
|
|
|
|
item.promotions = -2
|
|
|
|
} else {
|
|
|
|
c.list.Remove(item.element)
|
|
|
|
}
|
2014-02-28 13:10:42 +01:00
|
|
|
}
|
|
|
|
}
|
2013-10-19 02:56:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) doPromote(item *Item) bool {
|
2014-10-25 07:24:52 +02:00
|
|
|
//already deleted
|
|
|
|
if item.promotions == -2 {
|
|
|
|
return false
|
|
|
|
}
|
2014-02-28 13:10:42 +01:00
|
|
|
item.promotions = 0
|
|
|
|
if item.element != nil { //not a new item
|
|
|
|
c.list.MoveToFront(item.element)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
item.element = c.list.PushFront(item)
|
|
|
|
return true
|
2013-10-19 02:56:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) gc() {
|
2014-02-28 13:10:42 +01:00
|
|
|
element := c.list.Back()
|
|
|
|
for i := 0; i < c.itemsToPrune; i++ {
|
|
|
|
if element == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
prev := element.Prev()
|
|
|
|
item := element.Value.(*Item)
|
|
|
|
if c.tracking == false || atomic.LoadInt32(&item.refCount) == 0 {
|
|
|
|
c.bucket(item.key).delete(item.key)
|
|
|
|
c.list.Remove(element)
|
|
|
|
}
|
|
|
|
element = prev
|
|
|
|
}
|
2013-10-19 02:56:28 +02:00
|
|
|
}
|