|
|
@ -2,6 +2,7 @@ package cache
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"reflect"
|
|
|
|
"runtime"
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
"sync"
|
|
|
@ -91,7 +92,7 @@ type Cache struct {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type cache struct {
|
|
|
|
type cache struct {
|
|
|
|
DefaultExpiration time.Duration
|
|
|
|
DefaultExpiration int64
|
|
|
|
Items map[string]*Item
|
|
|
|
Items map[string]*Item
|
|
|
|
mu *sync.Mutex
|
|
|
|
mu *sync.Mutex
|
|
|
|
janitor *janitor
|
|
|
|
janitor *janitor
|
|
|
@ -99,26 +100,26 @@ type cache struct {
|
|
|
|
|
|
|
|
|
|
|
|
type Item struct {
|
|
|
|
type Item struct {
|
|
|
|
Object interface{}
|
|
|
|
Object interface{}
|
|
|
|
Expiration *time.Time
|
|
|
|
Expiration *int64
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type janitor struct {
|
|
|
|
type janitor struct {
|
|
|
|
Interval time.Duration
|
|
|
|
Interval int64
|
|
|
|
stop chan bool
|
|
|
|
stop chan bool
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Adds an item to the cache, replacing any existing item. If the duration is 0, the
|
|
|
|
// Adds an item to the cache, replacing any existing item. If the duration is 0, the
|
|
|
|
// cache's default expiration time is used. If it is -1, the item never expires.
|
|
|
|
// cache's default expiration time is used. If it is -1, the item never expires.
|
|
|
|
func (c *cache) Set(k string, x interface{}, d time.Duration) {
|
|
|
|
func (c *cache) Set(k string, x interface{}, d int64) {
|
|
|
|
c.mu.Lock()
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
var e *time.Time
|
|
|
|
var e *int64
|
|
|
|
if d == 0 {
|
|
|
|
if d == 0 {
|
|
|
|
d = c.DefaultExpiration
|
|
|
|
d = c.DefaultExpiration
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if d > 0 {
|
|
|
|
if d > 0 {
|
|
|
|
t := time.Now().Add(d)
|
|
|
|
t := time.Nanoseconds() + d
|
|
|
|
e = &t
|
|
|
|
e = &t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.Items[k] = &Item{
|
|
|
|
c.Items[k] = &Item{
|
|
|
@ -130,8 +131,8 @@ func (c *cache) Set(k string, x interface{}, d time.Duration) {
|
|
|
|
// TODO: Add and Replace aren't completely atomic
|
|
|
|
// TODO: Add and Replace aren't completely atomic
|
|
|
|
|
|
|
|
|
|
|
|
// Adds an item to the cache only if an item doesn't already exist for the given key,
|
|
|
|
// Adds 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 if not.
|
|
|
|
// or if the existing item has expired. Returns an os.Error if not.
|
|
|
|
func (c *cache) Add(k string, x interface{}, d time.Duration) error {
|
|
|
|
func (c *cache) Add(k string, x interface{}, d int64) os.Error {
|
|
|
|
_, found := c.Get(k)
|
|
|
|
_, found := c.Get(k)
|
|
|
|
if found {
|
|
|
|
if found {
|
|
|
|
return fmt.Errorf("Item %s already exists", k)
|
|
|
|
return fmt.Errorf("Item %s already exists", k)
|
|
|
@ -140,9 +141,9 @@ func (c *cache) Add(k string, x interface{}, d time.Duration) error {
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Sets a new value for the cache item only if it already exists. Returns an error if
|
|
|
|
// Sets a new value for the cache item only if it already exists. Returns an os.Error if
|
|
|
|
// it does not.
|
|
|
|
// it does not.
|
|
|
|
func (c *cache) Replace(k string, x interface{}, d time.Duration) error {
|
|
|
|
func (c *cache) Replace(k string, x interface{}, d int64) os.Error {
|
|
|
|
_, found := c.Get(k)
|
|
|
|
_, found := c.Get(k)
|
|
|
|
if !found {
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("Item %s doesn't exist", k)
|
|
|
|
return fmt.Errorf("Item %s doesn't exist", k)
|
|
|
@ -162,17 +163,17 @@ func (c *cache) Get(k string) (interface{}, bool) {
|
|
|
|
return nil, false
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if item.Expired() {
|
|
|
|
if item.Expired() {
|
|
|
|
delete(c.Items, k)
|
|
|
|
c.Items[k] = nil, false
|
|
|
|
return nil, false
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return item.Object, true
|
|
|
|
return item.Object, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Increment an item of type int, int8, int16, int32, int64, uintptr, uint, uint8,
|
|
|
|
// Increment an item of type int, int8, int16, int32, int64, uintptr, uint, uint8,
|
|
|
|
// uint32, uint64, float32 or float64 by n. Returns an error if the item's value is
|
|
|
|
// uint32, uint64, float32 or float64 by n. Returns an os.Error if the item's value is
|
|
|
|
// not an integer, if it was not found, or if it is not possible to increment it by
|
|
|
|
// not an integer, if it was not found, or if it is not possible to increment it by
|
|
|
|
// n. Passing a negative number will cause the item to be decremented.
|
|
|
|
// n. Passing a negative number will cause the item to be decremented.
|
|
|
|
func (c *cache) IncrementFloat(k string, n float64) error {
|
|
|
|
func (c *cache) IncrementFloat(k string, n float64) os.Error {
|
|
|
|
c.mu.Lock()
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
|
|
|
@ -216,18 +217,18 @@ func (c *cache) IncrementFloat(k string, n float64) error {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Increment an item of type int, int8, int16, int32, int64, uintptr, uint, uint8,
|
|
|
|
// Increment 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
|
|
|
|
// uint32, or uint64, float32 or float64 by n. Returns an os.Error if the item's value
|
|
|
|
// is not an integer, if it was not found, or if it is not possible to increment it
|
|
|
|
// is not an integer, if it was not found, or if it is not possible to increment it
|
|
|
|
// by n. Passing a negative number will cause the item to be decremented.
|
|
|
|
// by n. Passing a negative number will cause the item to be decremented.
|
|
|
|
func (c *cache) Increment(k string, n int64) error {
|
|
|
|
func (c *cache) Increment(k string, n int64) os.Error {
|
|
|
|
return c.IncrementFloat(k, float64(n))
|
|
|
|
return c.IncrementFloat(k, float64(n))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Decrement an item of type int, int8, int16, int32, int64, uintptr, uint, uint8,
|
|
|
|
// 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
|
|
|
|
// uint32, or uint64, float32 or float64 by n. Returns an os.Error if the item's value
|
|
|
|
// is not an integer, if it was not found, or if it is not possible to decrement it
|
|
|
|
// is not an integer, if it was not found, or if it is not possible to decrement it
|
|
|
|
// by n.
|
|
|
|
// by n.
|
|
|
|
func (c *cache) Decrement(k string, n int64) error {
|
|
|
|
func (c *cache) Decrement(k string, n int64) os.Error {
|
|
|
|
return c.Increment(k, n*-1)
|
|
|
|
return c.Increment(k, n*-1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -236,7 +237,7 @@ func (c *cache) Delete(k string) {
|
|
|
|
c.mu.Lock()
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
delete(c.Items, k)
|
|
|
|
c.Items[k] = nil, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Deletes all expired items from the cache.
|
|
|
|
// Deletes all expired items from the cache.
|
|
|
@ -246,7 +247,7 @@ func (c *cache) DeleteExpired() {
|
|
|
|
|
|
|
|
|
|
|
|
for k, v := range c.Items {
|
|
|
|
for k, v := range c.Items {
|
|
|
|
if v.Expired() {
|
|
|
|
if v.Expired() {
|
|
|
|
delete(c.Items, k)
|
|
|
|
c.Items[k] = nil, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -264,7 +265,7 @@ func (i *Item) Expired() bool {
|
|
|
|
if i.Expiration == nil {
|
|
|
|
if i.Expiration == nil {
|
|
|
|
return false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i.Expiration.Before(time.Now())
|
|
|
|
return *i.Expiration < time.Nanoseconds()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (j *janitor) Run(c *cache) {
|
|
|
|
func (j *janitor) Run(c *cache) {
|
|
|
@ -293,7 +294,7 @@ func stopJanitor(c *Cache) {
|
|
|
|
// expire and must be deleted manually. If the cleanup interval is less than one,
|
|
|
|
// expire and must be deleted manually. If the cleanup interval is less than one,
|
|
|
|
// expired items are not deleted from the cache before their next lookup or before
|
|
|
|
// expired items are not deleted from the cache before their next lookup or before
|
|
|
|
// calling DeleteExpired.
|
|
|
|
// calling DeleteExpired.
|
|
|
|
func New(de, ci time.Duration) *Cache {
|
|
|
|
func New(de, ci int64) *Cache {
|
|
|
|
if de == 0 {
|
|
|
|
if de == 0 {
|
|
|
|
de = -1
|
|
|
|
de = -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|