2012-01-02 18:01:04 +08:00
|
|
|
package cache
|
|
|
|
|
2016-08-27 18:05:46 +08:00
|
|
|
// The package is used as a template, don't use it directly!
|
|
|
|
|
2012-01-16 02:16:10 +08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-08-27 18:05:46 +08:00
|
|
|
// Attr is cachmap attribute
|
|
|
|
type Attr_tpl struct {
|
|
|
|
// An (optional) function that is called with the key and value when an
|
|
|
|
// item is evicted from the cache. (Including when it is deleted manually, but
|
|
|
|
// not when it is overwritten.) Set to nil to disable.
|
|
|
|
OnEvicted func(k string, v ValueType_tpl)
|
|
|
|
|
2016-08-29 18:58:22 +08:00
|
|
|
DefaultCleanupInterval time.Duration // Default clean interval, this is a time interval to cleanup expired items
|
|
|
|
DefaultExpiration time.Duration // Default expiration duration
|
|
|
|
Size int64 // Initial size of map
|
2016-08-27 18:05:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Item struct
|
2016-09-01 14:03:22 +08:00
|
|
|
type Item_tpl struct {
|
2016-08-27 18:05:46 +08:00
|
|
|
Object ValueType_tpl
|
2015-12-01 04:02:02 +08:00
|
|
|
Expiration int64
|
2012-01-29 10:16:59 +08:00
|
|
|
}
|
|
|
|
|
2016-08-29 18:58:22 +08:00
|
|
|
// Expired returns true if the item has expired.
|
2016-09-01 14:03:22 +08:00
|
|
|
func (item Item_tpl) Expired() bool {
|
2016-08-27 18:05:46 +08:00
|
|
|
return item.Expiration != 0 && time.Now().UnixNano() > item.Expiration
|
2012-01-29 10:16:59 +08:00
|
|
|
}
|
|
|
|
|
2016-08-27 18:05:46 +08:00
|
|
|
// Cache struct
|
|
|
|
type Cache_tpl struct {
|
2016-09-01 14:03:22 +08:00
|
|
|
*cache_tpl
|
2012-06-22 10:56:12 +08:00
|
|
|
// If this is confusing, see the comment at the bottom of New()
|
2012-01-02 18:01:04 +08:00
|
|
|
}
|
|
|
|
|
2016-09-01 14:03:22 +08:00
|
|
|
type cache_tpl struct {
|
2012-07-05 02:34:37 +08:00
|
|
|
defaultExpiration time.Duration
|
2016-09-01 14:03:22 +08:00
|
|
|
items map[string]Item_tpl
|
2015-11-28 02:03:24 +08:00
|
|
|
mu sync.RWMutex
|
2016-08-27 18:05:46 +08:00
|
|
|
onEvicted func(string, ValueType_tpl)
|
2016-09-01 14:03:22 +08:00
|
|
|
stop chan bool
|
2012-01-02 18:01:04 +08:00
|
|
|
}
|
|
|
|
|
2014-12-22 14:46:22 +08:00
|
|
|
// Add an item to the cache, replacing any existing item. If the duration is 0
|
|
|
|
// (DefaultExpiration), the cache's default expiration time is used. If it is -1
|
|
|
|
// (NoExpiration), the item never expires.
|
2016-09-01 14:03:22 +08:00
|
|
|
func (c *cache_tpl) Set(k string, x ValueType_tpl, d time.Duration) {
|
2015-12-02 00:08:43 +08:00
|
|
|
// "Inlining" of set
|
|
|
|
var e int64
|
|
|
|
if d == DefaultExpiration {
|
|
|
|
d = c.defaultExpiration
|
|
|
|
}
|
|
|
|
if d > 0 {
|
|
|
|
e = time.Now().Add(d).UnixNano()
|
|
|
|
}
|
2015-11-28 02:03:24 +08:00
|
|
|
c.mu.Lock()
|
2016-09-01 14:03:22 +08:00
|
|
|
c.items[k] = Item_tpl{
|
2015-12-02 00:08:43 +08:00
|
|
|
Object: x,
|
|
|
|
Expiration: e,
|
|
|
|
}
|
2012-06-22 10:51:34 +08:00
|
|
|
// TODO: Calls to mu.Unlock are currently not deferred because defer
|
|
|
|
// adds ~200 ns (as of go1.)
|
2015-11-28 02:03:24 +08:00
|
|
|
c.mu.Unlock()
|
2012-01-04 15:54:01 +08:00
|
|
|
}
|
|
|
|
|
2016-09-01 14:03:22 +08:00
|
|
|
func (c *cache_tpl) set(k string, x ValueType_tpl, d time.Duration) {
|
2015-12-01 04:02:02 +08:00
|
|
|
var e int64
|
2014-12-22 14:46:22 +08:00
|
|
|
if d == DefaultExpiration {
|
2012-07-05 02:34:37 +08:00
|
|
|
d = c.defaultExpiration
|
2012-01-02 18:01:04 +08:00
|
|
|
}
|
2012-01-03 00:13:29 +08:00
|
|
|
if d > 0 {
|
2015-12-01 04:02:02 +08:00
|
|
|
e = time.Now().Add(d).UnixNano()
|
2012-01-02 18:01:04 +08:00
|
|
|
}
|
2016-09-01 14:03:22 +08:00
|
|
|
c.items[k] = Item_tpl{
|
2012-01-02 21:11:17 +08:00
|
|
|
Object: x,
|
2012-01-02 18:01:04 +08:00
|
|
|
Expiration: e,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-22 11:24:48 +08:00
|
|
|
// Add an item to the cache only if an item doesn't already exist for the given
|
|
|
|
// key, or if the existing item has expired. Returns an error otherwise.
|
2016-09-01 14:03:22 +08:00
|
|
|
func (c *cache_tpl) Add(k string, x ValueType_tpl, d time.Duration) error {
|
2015-11-28 02:03:24 +08:00
|
|
|
c.mu.Lock()
|
2012-01-04 15:54:01 +08:00
|
|
|
_, found := c.get(k)
|
2012-01-02 21:04:47 +08:00
|
|
|
if found {
|
2015-11-28 02:03:24 +08:00
|
|
|
c.mu.Unlock()
|
2013-04-19 02:24:30 +08:00
|
|
|
return fmt.Errorf("Item %s already exists", k)
|
2012-01-02 21:04:47 +08:00
|
|
|
}
|
2012-01-04 15:54:01 +08:00
|
|
|
c.set(k, x, d)
|
2015-11-28 02:03:24 +08:00
|
|
|
c.mu.Unlock()
|
2012-01-02 21:04:47 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-01 08:11:57 +08:00
|
|
|
// Set a new value for the cache key only if it already exists, and the existing
|
|
|
|
// item hasn't expired. Returns an error otherwise.
|
2016-09-01 14:03:22 +08:00
|
|
|
func (c *cache_tpl) Replace(k string, x ValueType_tpl, d time.Duration) error {
|
2015-11-28 02:03:24 +08:00
|
|
|
c.mu.Lock()
|
2012-01-04 15:54:01 +08:00
|
|
|
_, found := c.get(k)
|
2012-01-02 21:04:47 +08:00
|
|
|
if !found {
|
2015-11-28 02:03:24 +08:00
|
|
|
c.mu.Unlock()
|
2013-04-19 02:24:30 +08:00
|
|
|
return fmt.Errorf("Item %s doesn't exist", k)
|
2012-01-02 21:04:47 +08:00
|
|
|
}
|
2012-01-04 15:54:01 +08:00
|
|
|
c.set(k, x, d)
|
2015-11-28 02:03:24 +08:00
|
|
|
c.mu.Unlock()
|
2012-01-02 21:04:47 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-06-22 11:24:48 +08:00
|
|
|
// Get an item from the cache. Returns the item or nil, and a bool indicating
|
|
|
|
// whether the key was found.
|
2016-09-01 14:03:22 +08:00
|
|
|
func (c *cache_tpl) Get(k string) (ValueType_tpl, bool) {
|
2015-11-28 02:03:24 +08:00
|
|
|
c.mu.RLock()
|
2015-12-01 04:02:02 +08:00
|
|
|
// "Inlining" of get and Expired
|
2015-12-01 02:50:17 +08:00
|
|
|
item, found := c.items[k]
|
2016-08-27 18:05:46 +08:00
|
|
|
// TODO: inline time.Now implementation
|
|
|
|
if !found || item.Expiration > 0 && time.Now().UnixNano() > item.Expiration {
|
2015-12-01 02:50:17 +08:00
|
|
|
c.mu.RUnlock()
|
2016-08-27 18:05:46 +08:00
|
|
|
return ValueType_tpl(0), false
|
2015-12-01 03:47:22 +08:00
|
|
|
}
|
2015-11-28 02:03:24 +08:00
|
|
|
c.mu.RUnlock()
|
2016-08-27 18:05:46 +08:00
|
|
|
return item.Object, true
|
2012-01-04 15:54:01 +08:00
|
|
|
}
|
|
|
|
|
2016-09-01 14:03:22 +08:00
|
|
|
func (c *cache_tpl) get(k string) (*ValueType_tpl, bool) {
|
2012-07-05 02:34:37 +08:00
|
|
|
item, found := c.items[k]
|
2016-08-27 18:05:46 +08:00
|
|
|
if !found || item.Expiration > 0 && time.Now().UnixNano() > item.Expiration {
|
2012-01-02 18:01:04 +08:00
|
|
|
return nil, false
|
|
|
|
}
|
2016-08-11 11:58:16 +08:00
|
|
|
return &item.Object, true
|
2012-01-02 18:01:04 +08:00
|
|
|
}
|
|
|
|
|
2016-08-29 18:58:22 +08:00
|
|
|
// MARK_Numberic_tpl_begin
|
|
|
|
|
2012-06-22 10:56:12 +08:00
|
|
|
// Increment an item of type int, int8, int16, int32, int64, uintptr, uint,
|
2012-08-17 19:39:02 +08:00
|
|
|
// uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the
|
2012-06-22 10:56:12 +08:00
|
|
|
// item's value is not an integer, if it was not found, or if it is not
|
2013-04-19 02:24:30 +08:00
|
|
|
// possible to increment it by n. To retrieve the incremented value, use one
|
|
|
|
// of the specialized methods, e.g. IncrementInt64.
|
2016-09-01 14:03:22 +08:00
|
|
|
func (c *cache_tpl) Increment(k string, n ValueType_tpl) error {
|
2016-08-29 18:58:22 +08:00
|
|
|
c.mu.Lock()
|
|
|
|
v, found := c.items[k]
|
|
|
|
if !found || v.Expired() {
|
|
|
|
c.mu.Unlock()
|
|
|
|
return fmt.Errorf("Item %s not found", k)
|
|
|
|
}
|
|
|
|
v.Object += n
|
|
|
|
c.items[k] = v
|
|
|
|
c.mu.Unlock()
|
2013-04-19 02:24:30 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-06-22 10:56:12 +08:00
|
|
|
// Decrement an item of type int, int8, int16, int32, int64, uintptr, uint,
|
|
|
|
// uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the
|
|
|
|
// item's value is not an integer, if it was not found, or if it is not
|
2013-04-19 02:24:30 +08:00
|
|
|
// possible to decrement it by n. To retrieve the decremented value, use one
|
|
|
|
// of the specialized methods, e.g. DecrementInt64.
|
2016-09-01 14:03:22 +08:00
|
|
|
func (c *cache_tpl) Decrement(k string, n ValueType_tpl) error {
|
2012-08-17 19:39:02 +08:00
|
|
|
// TODO: Implement Increment and Decrement more cleanly.
|
|
|
|
// (Cannot do Increment(k, n*-1) for uints.)
|
2016-08-29 18:58:22 +08:00
|
|
|
c.mu.Lock()
|
|
|
|
v, found := c.items[k]
|
|
|
|
if !found || v.Expired() {
|
|
|
|
c.mu.Unlock()
|
|
|
|
return fmt.Errorf("Item not found")
|
|
|
|
}
|
|
|
|
v.Object -= n
|
|
|
|
c.items[k] = v
|
|
|
|
c.mu.Unlock()
|
2012-08-17 19:39:02 +08:00
|
|
|
return nil
|
2012-01-02 20:52:43 +08:00
|
|
|
}
|
|
|
|
|
2016-08-29 18:58:22 +08:00
|
|
|
// MARK_Numberic_tpl_end
|
|
|
|
|
2012-06-22 11:24:48 +08:00
|
|
|
// Delete an item from the cache. Does nothing if the key is not in the cache.
|
2016-09-01 14:03:22 +08:00
|
|
|
func (c *cache_tpl) Delete(k string) {
|
2016-08-27 18:05:46 +08:00
|
|
|
// fast path
|
|
|
|
if c.onEvicted == nil {
|
|
|
|
c.mu.Lock()
|
|
|
|
c.deleteFast(k)
|
|
|
|
c.mu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// slow path
|
2015-11-28 02:03:24 +08:00
|
|
|
c.mu.Lock()
|
2015-11-28 11:00:08 +08:00
|
|
|
v, evicted := c.delete(k)
|
2015-11-28 02:03:24 +08:00
|
|
|
c.mu.Unlock()
|
2015-11-28 11:00:08 +08:00
|
|
|
if evicted {
|
|
|
|
c.onEvicted(k, v)
|
|
|
|
}
|
2012-01-04 15:54:01 +08:00
|
|
|
}
|
|
|
|
|
2016-09-01 14:03:22 +08:00
|
|
|
func (c *cache_tpl) delete(k string) (ValueType_tpl, bool) {
|
2016-08-27 18:05:46 +08:00
|
|
|
if v, found := c.items[k]; found {
|
|
|
|
delete(c.items, k)
|
|
|
|
return v.Object, true
|
2015-11-28 11:00:08 +08:00
|
|
|
}
|
2016-08-27 18:05:46 +08:00
|
|
|
//TODO: zeroValue
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
2016-09-01 14:03:22 +08:00
|
|
|
func (c *cache_tpl) deleteFast(k string) {
|
2012-07-05 02:34:37 +08:00
|
|
|
delete(c.items, k)
|
2015-11-28 11:00:08 +08:00
|
|
|
}
|
|
|
|
|
2012-06-22 11:24:48 +08:00
|
|
|
// Delete all expired items from the cache.
|
2016-09-01 14:03:22 +08:00
|
|
|
func (c *cache_tpl) DeleteExpired() {
|
|
|
|
var evictedItems []struct {
|
|
|
|
key string
|
|
|
|
value ValueType_tpl
|
|
|
|
}
|
2015-12-01 04:12:19 +08:00
|
|
|
now := time.Now().UnixNano()
|
2016-08-27 18:05:46 +08:00
|
|
|
// fast path
|
|
|
|
if c.onEvicted == nil {
|
|
|
|
c.mu.Lock()
|
|
|
|
for k, v := range c.items {
|
|
|
|
// "Inlining" of expired
|
|
|
|
if v.Expiration > 0 && now > v.Expiration {
|
|
|
|
c.deleteFast(k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.mu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// slow path
|
2015-11-28 02:03:24 +08:00
|
|
|
c.mu.Lock()
|
2012-07-05 02:34:37 +08:00
|
|
|
for k, v := range c.items {
|
2015-12-01 03:12:45 +08:00
|
|
|
// "Inlining" of expired
|
2015-12-01 04:02:02 +08:00
|
|
|
if v.Expiration > 0 && now > v.Expiration {
|
2015-11-28 11:00:08 +08:00
|
|
|
ov, evicted := c.delete(k)
|
|
|
|
if evicted {
|
2016-09-01 14:03:22 +08:00
|
|
|
evictedItems = append(evictedItems, struct {
|
|
|
|
key string
|
|
|
|
value ValueType_tpl
|
|
|
|
}{k, ov})
|
2015-11-28 11:00:08 +08:00
|
|
|
}
|
2012-01-02 18:01:04 +08:00
|
|
|
}
|
|
|
|
}
|
2015-11-28 02:03:24 +08:00
|
|
|
c.mu.Unlock()
|
2015-11-28 11:00:08 +08:00
|
|
|
for _, v := range evictedItems {
|
|
|
|
c.onEvicted(v.key, v.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-19 02:32:01 +08:00
|
|
|
// Returns the number of items in the cache. This may include items that have
|
2013-08-09 02:54:03 +08:00
|
|
|
// expired, but have not yet been cleaned up. Equivalent to len(c.Items()).
|
2016-09-01 14:03:22 +08:00
|
|
|
func (c *cache_tpl) ItemCount() int {
|
2015-11-28 02:03:24 +08:00
|
|
|
c.mu.RLock()
|
2013-04-19 02:32:01 +08:00
|
|
|
n := len(c.items)
|
2015-11-28 02:03:24 +08:00
|
|
|
c.mu.RUnlock()
|
2013-04-19 02:32:01 +08:00
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2012-06-22 11:24:48 +08:00
|
|
|
// Delete all items from the cache.
|
2016-09-01 14:03:22 +08:00
|
|
|
func (c *cache_tpl) Flush() {
|
2015-11-28 02:03:24 +08:00
|
|
|
c.mu.Lock()
|
2016-09-01 14:03:22 +08:00
|
|
|
c.items = map[string]Item_tpl{}
|
2015-11-28 02:03:24 +08:00
|
|
|
c.mu.Unlock()
|
2012-01-02 18:01:04 +08:00
|
|
|
}
|
|
|
|
|
2016-09-01 14:03:22 +08:00
|
|
|
// _ *cache_tpl is used as a namespace, so that "stopJanitor" will not conflicts for the name.
|
|
|
|
func (_ *cache_tpl) stopJanitor(c *Cache_tpl) {
|
|
|
|
c.stop <- true
|
2012-01-02 18:01:04 +08:00
|
|
|
}
|
|
|
|
|
2016-09-01 14:03:22 +08:00
|
|
|
func (c *cache_tpl) runJanitor(ci time.Duration) {
|
|
|
|
c.stop = make(chan bool)
|
|
|
|
go func() {
|
|
|
|
ticker := time.NewTicker(ci)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
c.DeleteExpired()
|
|
|
|
case <-c.stop:
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
}
|
2012-01-02 18:01:04 +08:00
|
|
|
}
|
2016-09-01 14:03:22 +08:00
|
|
|
}()
|
2012-01-02 18:01:04 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-01 14:03:22 +08:00
|
|
|
// New Returns a new cache with a given default expiration duration and
|
|
|
|
// cleanup interval. If the expiration duration is less than one
|
|
|
|
// (or NoExpiration), the items in the cache never expire (by default),
|
|
|
|
// and must be deleted manually. If the cleanup interval is less than one,
|
|
|
|
// expired items are not deleted from the cache before calling c.DeleteExpired().
|
|
|
|
//
|
|
|
|
func New_tpl(attr Attr_tpl) *Cache_tpl {
|
|
|
|
items := make(map[string]Item_tpl, attr.Size)
|
|
|
|
if attr.DefaultExpiration == 0 {
|
|
|
|
attr.DefaultExpiration = -1
|
2012-01-02 18:01:04 +08:00
|
|
|
}
|
2016-09-01 14:03:22 +08:00
|
|
|
c := &cache_tpl{
|
|
|
|
defaultExpiration: attr.DefaultExpiration,
|
|
|
|
items: items,
|
2012-01-02 18:01:04 +08:00
|
|
|
}
|
2016-09-01 14:03:22 +08:00
|
|
|
c.onEvicted = attr.OnEvicted
|
2012-06-22 10:50:10 +08:00
|
|
|
// This trick ensures that the janitor goroutine (which--granted it
|
|
|
|
// was enabled--is running DeleteExpired on c forever) does not keep
|
|
|
|
// the returned C object from being garbage collected. When it is
|
|
|
|
// garbage collected, the finalizer stops the janitor goroutine, after
|
|
|
|
// which c can be collected.
|
2016-08-27 18:05:46 +08:00
|
|
|
C := &Cache_tpl{c}
|
2016-09-01 14:03:22 +08:00
|
|
|
if attr.DefaultCleanupInterval > 0 {
|
|
|
|
c.runJanitor(attr.DefaultCleanupInterval)
|
|
|
|
runtime.SetFinalizer(C, c.stopJanitor)
|
2012-01-02 18:01:04 +08:00
|
|
|
}
|
|
|
|
return C
|
|
|
|
}
|