2013-10-19 02:56:28 +02:00
|
|
|
package ccache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
"runtime"
|
|
|
|
"hash/fnv"
|
|
|
|
"container/list"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Cache struct {
|
|
|
|
*Configuration
|
|
|
|
list *list.List
|
|
|
|
buckets []*Bucket
|
|
|
|
bucketCount uint32
|
2013-10-30 13:18:51 +01:00
|
|
|
deletables chan *Item
|
2013-10-19 02:56:28 +02:00
|
|
|
promotables chan *Item
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(config *Configuration) *Cache {
|
|
|
|
c := &Cache{
|
2013-10-31 04:45:22 +01:00
|
|
|
list: list.New(),
|
2013-10-19 02:56:28 +02:00
|
|
|
Configuration: config,
|
|
|
|
bucketCount: uint32(config.buckets),
|
|
|
|
buckets: make([]*Bucket, config.buckets),
|
2013-10-30 13:18:51 +01:00
|
|
|
deletables: make(chan *Item, config.deleteBuffer),
|
2013-10-19 02:56:28 +02:00
|
|
|
promotables: make(chan *Item, config.promoteBuffer),
|
|
|
|
}
|
|
|
|
for i := 0; i < config.buckets; i++ {
|
|
|
|
c.buckets[i] = &Bucket{
|
|
|
|
lookup: make(map[string]*Item),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
go c.worker()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2013-10-30 13:18:51 +01:00
|
|
|
func (c *Cache) Get(key string) interface{} {
|
|
|
|
bucket := c.bucket(key)
|
|
|
|
item := bucket.get(key)
|
2013-10-19 02:56:28 +02:00
|
|
|
if item == nil { return nil }
|
2013-10-30 13:18:51 +01:00
|
|
|
if item.expires.Before(time.Now()) {
|
|
|
|
c.deleteItem(bucket, item)
|
|
|
|
return nil
|
|
|
|
}
|
2013-11-13 06:46:41 +01:00
|
|
|
c.conditionalPromote(item)
|
2013-10-19 02:56:28 +02:00
|
|
|
return item.value
|
|
|
|
}
|
|
|
|
|
2013-10-30 13:18:51 +01:00
|
|
|
func (c *Cache) Set(key string, value interface{}, duration time.Duration) {
|
2013-11-13 06:46:41 +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) {
|
2013-10-30 13:24:43 +01:00
|
|
|
item := c.Get(key)
|
2013-10-31 04:45:22 +01:00
|
|
|
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) {
|
|
|
|
item := c.bucket(key).getAndDelete(key)
|
|
|
|
if item != nil {
|
|
|
|
c.deletables <- item
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
for _, bucket := range c.buckets {
|
|
|
|
bucket.clear()
|
|
|
|
}
|
|
|
|
c.list = list.New()
|
|
|
|
}
|
|
|
|
|
2013-10-30 13:18:51 +01:00
|
|
|
func (c *Cache) deleteItem(bucket *Bucket, item *Item) {
|
|
|
|
bucket.delete(item.key) //stop othe GETs from getting it
|
|
|
|
c.deletables <- item
|
|
|
|
}
|
|
|
|
|
2013-10-19 02:56:28 +02:00
|
|
|
func (c *Cache) bucket(key string) *Bucket {
|
|
|
|
h := fnv.New32a()
|
|
|
|
h.Write([]byte(key))
|
|
|
|
index := h.Sum32() % c.bucketCount
|
|
|
|
return c.buckets[index]
|
|
|
|
}
|
|
|
|
|
2013-11-13 06:46:41 +01:00
|
|
|
func (c *Cache) conditionalPromote(item *Item) {
|
2013-10-30 13:18:51 +01:00
|
|
|
if item.shouldPromote(c.getsPerPromote) == false { return }
|
2013-11-13 06:46:41 +01:00
|
|
|
c.promote(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) promote(item *Item) {
|
2013-10-19 02:56:28 +02:00
|
|
|
c.promotables <- item
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) worker() {
|
|
|
|
ms := new(runtime.MemStats)
|
|
|
|
for {
|
2013-10-30 13:18:51 +01:00
|
|
|
select {
|
|
|
|
case item := <- c.promotables:
|
|
|
|
wasNew := c.doPromote(item)
|
|
|
|
if wasNew == false { continue }
|
|
|
|
runtime.ReadMemStats(ms)
|
|
|
|
if ms.HeapAlloc > c.size { c.gc() }
|
|
|
|
case item := <- c.deletables:
|
|
|
|
c.list.Remove(item.element)
|
2013-10-19 02:56:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) doPromote(item *Item) bool {
|
|
|
|
item.Lock()
|
|
|
|
defer item.Unlock()
|
2013-10-21 13:37:35 +02:00
|
|
|
item.promotions = 0
|
2013-10-19 02:56:28 +02:00
|
|
|
if item.element != nil { //not a new item
|
|
|
|
c.list.MoveToFront(item.element)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
item.element = c.list.PushFront(item)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) gc() {
|
|
|
|
for i := 0; i < c.itemsToPrune; i++ {
|
|
|
|
element := c.list.Back()
|
|
|
|
if element == nil { return }
|
|
|
|
item := element.Value.(*Item)
|
2013-10-30 13:18:51 +01:00
|
|
|
c.bucket(item.key).delete(item.key)
|
2013-10-19 02:56:28 +02:00
|
|
|
c.list.Remove(element)
|
|
|
|
}
|
|
|
|
}
|