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
|
2014-11-21 08:39:25 +01:00
|
|
|
size int64
|
2014-11-14 01:56:24 +01:00
|
|
|
buckets []*bucket
|
2014-11-02 12:09:49 +01:00
|
|
|
bucketMask uint32
|
2014-02-28 13:10:42 +01:00
|
|
|
deletables chan *Item
|
|
|
|
promotables chan *Item
|
2013-10-19 02:56:28 +02:00
|
|
|
}
|
|
|
|
|
2014-11-14 01:56:24 +01:00
|
|
|
// Create a new cache with the specified configuration
|
|
|
|
// See ccache.Configure() for creating a configuration
|
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,
|
2014-11-02 12:09:49 +01:00
|
|
|
bucketMask: uint32(config.buckets) - 1,
|
2014-11-14 01:56:24 +01:00
|
|
|
buckets: make([]*bucket, config.buckets),
|
2014-02-28 13:10:42 +01:00
|
|
|
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-11-14 01:56:24 +01:00
|
|
|
c.buckets[i] = &bucket{
|
2014-02-28 13:10:42 +01:00
|
|
|
lookup: make(map[string]*Item),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
go c.worker()
|
|
|
|
return c
|
2013-10-19 02:56:28 +02:00
|
|
|
}
|
|
|
|
|
2014-11-14 01:56:24 +01:00
|
|
|
// Get an item from the cache. Returns nil if the item wasn't found.
|
|
|
|
// This can return an expired item. Use item.Expired() to see if the item
|
|
|
|
// is expired and item.TTL() to see how long until the item expires (which
|
|
|
|
// will be negative for an already expired item).
|
2014-10-25 12:15:47 +02:00
|
|
|
func (c *Cache) Get(key string) *Item {
|
2014-12-28 05:11:32 +01:00
|
|
|
item := c.bucket(key).get(key)
|
2014-10-25 12:15:47 +02:00
|
|
|
if item == nil {
|
|
|
|
return nil
|
2014-02-28 13:10:42 +01:00
|
|
|
}
|
2016-07-08 00:32:49 +02:00
|
|
|
if item.expires > time.Now().UnixNano() {
|
2014-12-28 05:11:32 +01:00
|
|
|
c.promote(item)
|
2014-10-25 12:15:47 +02:00
|
|
|
}
|
|
|
|
return item
|
2014-02-28 13:10:42 +01:00
|
|
|
}
|
|
|
|
|
2014-11-14 01:56:24 +01:00
|
|
|
// Used when the cache was created with the Track() configuration option.
|
|
|
|
// Avoid otherwise
|
2014-02-28 13:10:42 +01:00
|
|
|
func (c *Cache) TrackingGet(key string) TrackedItem {
|
2014-10-25 12:15:47 +02:00
|
|
|
item := c.Get(key)
|
2014-02-28 13:10:42 +01:00
|
|
|
if item == nil {
|
|
|
|
return NilTracked
|
|
|
|
}
|
|
|
|
item.track()
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
2014-11-14 01:56:24 +01:00
|
|
|
// Set the value in the cache for the specified duration
|
2013-10-30 13:18:51 +01:00
|
|
|
func (c *Cache) Set(key string, value interface{}, duration time.Duration) {
|
2015-01-07 02:09:39 +01:00
|
|
|
c.set(key, value, duration)
|
2013-10-19 02:56:28 +02:00
|
|
|
}
|
|
|
|
|
2014-11-14 01:56:24 +01:00
|
|
|
// Replace the value if it exists, does not set if it doesn't.
|
|
|
|
// Returns true if the item existed an was replaced, false otherwise.
|
2014-12-28 05:11:32 +01:00
|
|
|
// Replace does not reset item's TTL
|
2014-11-13 16:20:12 +01:00
|
|
|
func (c *Cache) Replace(key string, value interface{}) bool {
|
2014-12-28 05:11:32 +01:00
|
|
|
item := c.bucket(key).get(key)
|
|
|
|
if item == nil {
|
|
|
|
return false
|
2014-11-21 09:45:11 +01:00
|
|
|
}
|
2014-12-28 05:11:32 +01:00
|
|
|
c.Set(key, value, item.TTL())
|
|
|
|
return true
|
2014-11-13 16:20:12 +01:00
|
|
|
}
|
|
|
|
|
2016-02-03 16:07:59 +01:00
|
|
|
// Attempts to get the value from the cache and calles fetch on a miss (missing
|
|
|
|
// or stale item). If fetch returns an error, no value is cached and the error
|
|
|
|
// is returned back to the caller.
|
2015-01-07 02:12:17 +01:00
|
|
|
func (c *Cache) Fetch(key string, duration time.Duration, fetch func() (interface{}, error)) (*Item, error) {
|
2014-02-28 13:10:42 +01:00
|
|
|
item := c.Get(key)
|
2016-02-03 16:07:59 +01:00
|
|
|
if item != nil && !item.Expired() {
|
2014-02-28 13:10:42 +01:00
|
|
|
return item, nil
|
|
|
|
}
|
|
|
|
value, err := fetch()
|
2015-01-07 02:09:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-02-28 13:10:42 +01:00
|
|
|
}
|
2015-01-07 02:09:39 +01:00
|
|
|
return c.set(key, value, duration), nil
|
2013-10-30 13:24:43 +01:00
|
|
|
}
|
|
|
|
|
2014-11-14 01:56:24 +01:00
|
|
|
// Remove the item from the cache, return true if the item was present, false otherwise.
|
2014-10-27 02:30:48 +01:00
|
|
|
func (c *Cache) Delete(key string) bool {
|
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
|
2014-10-27 02:30:48 +01:00
|
|
|
return true
|
2014-02-28 13:10:42 +01:00
|
|
|
}
|
2014-10-27 02:30:48 +01:00
|
|
|
return false
|
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()
|
|
|
|
}
|
2014-11-20 01:03:59 +01:00
|
|
|
c.size = 0
|
2014-02-28 13:10:42 +01:00
|
|
|
c.list = list.New()
|
2013-10-31 04:45:22 +01:00
|
|
|
}
|
|
|
|
|
2015-07-23 16:24:50 +02:00
|
|
|
// Stops the background worker. Operations performed on the cache after Stop
|
|
|
|
// is called are likely to panic
|
|
|
|
func (c *Cache) Stop() {
|
|
|
|
close(c.promotables)
|
|
|
|
}
|
|
|
|
|
2014-11-14 01:56:24 +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
|
|
|
}
|
|
|
|
|
2015-01-07 02:09:39 +01:00
|
|
|
func (c *Cache) set(key string, value interface{}, duration time.Duration) *Item {
|
|
|
|
item, existing := c.bucket(key).set(key, value, duration)
|
|
|
|
if existing != nil {
|
|
|
|
c.deletables <- existing
|
|
|
|
}
|
|
|
|
c.promote(item)
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
2014-11-14 01:56:24 +01:00
|
|
|
func (c *Cache) bucket(key string) *bucket {
|
2014-02-28 13:10:42 +01:00
|
|
|
h := fnv.New32a()
|
|
|
|
h.Write([]byte(key))
|
2014-11-02 12:09:49 +01:00
|
|
|
return c.buckets[h.Sum32()&c.bucketMask]
|
2013-10-19 02:56:28 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
2015-07-23 16:24:50 +02:00
|
|
|
case item, ok := <-c.promotables:
|
|
|
|
if ok == false {
|
2015-07-26 05:05:48 +02:00
|
|
|
goto drain
|
2015-07-23 16:24:50 +02:00
|
|
|
}
|
2014-12-28 05:11:32 +01:00
|
|
|
if c.doPromote(item) && c.size > c.maxSize {
|
2014-02-28 13:10:42 +01:00
|
|
|
c.gc()
|
|
|
|
}
|
|
|
|
case item := <-c.deletables:
|
2015-07-26 05:05:48 +02:00
|
|
|
c.doDelete(item)
|
2014-02-28 13:10:42 +01:00
|
|
|
}
|
|
|
|
}
|
2015-07-26 05:05:48 +02:00
|
|
|
|
|
|
|
drain:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case item := <-c.deletables:
|
|
|
|
c.doDelete(item)
|
|
|
|
default:
|
|
|
|
close(c.deletables)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) doDelete(item *Item) {
|
|
|
|
if item.element == nil {
|
|
|
|
item.promotions = -2
|
|
|
|
} else {
|
|
|
|
c.size -= item.size
|
|
|
|
c.list.Remove(item.element)
|
|
|
|
}
|
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
|
2014-12-28 05:11:32 +01:00
|
|
|
if item.promotions == -2 {
|
2014-10-25 07:24:52 +02:00
|
|
|
return false
|
|
|
|
}
|
2014-02-28 13:10:42 +01:00
|
|
|
if item.element != nil { //not a new item
|
2014-12-28 05:11:32 +01:00
|
|
|
if item.shouldPromote(c.getsPerPromote) {
|
|
|
|
c.list.MoveToFront(item.element)
|
|
|
|
item.promotions = 0
|
|
|
|
}
|
2014-02-28 13:10:42 +01:00
|
|
|
return false
|
|
|
|
}
|
2014-12-28 05:11:32 +01:00
|
|
|
|
|
|
|
c.size += item.size
|
2014-02-28 13:10:42 +01:00
|
|
|
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)
|
2014-12-28 05:11:32 +01:00
|
|
|
c.size -= item.size
|
2014-02-28 13:10:42 +01:00
|
|
|
c.list.Remove(element)
|
2014-12-28 05:11:32 +01:00
|
|
|
item.promotions = -2
|
2014-02-28 13:10:42 +01:00
|
|
|
}
|
|
|
|
element = prev
|
|
|
|
}
|
2013-10-19 02:56:28 +02:00
|
|
|
}
|