sm
/
cache
1
0
Fork 0

Injected LRU capabilities that simply:

* ensures that when an item is added, if the number of unexpired items
>= maximum number of items specified, the oldest (based on ACCESS) is
expired
* when an item is "gotten", or "set", the access time is updated

The existing janitor system is unchanged, and used to actually delete
the items.

The only change from a consumer standpoint, is one addition parameter at
the end of a "New" call, that specifies the maximum number of items to
allow, or 0, if you want to disable the LRU at all.
This commit is contained in:
Matt Keller 2015-02-27 15:57:16 -05:00
parent 03284ca422
commit f66ee0bbc6
3 changed files with 283 additions and 89 deletions

View File

@ -6,3 +6,4 @@ code was contributed.)
Dustin Sallings <dustin@spy.net>
Jason Mooberry <jasonmoo@me.com>
Sergey Shepelev <temotor@gmail.com>
Matthew Keller <m@cognusion.com>

215
cache.go
View File

@ -13,6 +13,8 @@ import (
type Item struct {
Object interface{}
Expiration *time.Time
Ctime *time.Time
Atime *time.Time
}
// Returns true if the item has expired.
@ -23,6 +25,21 @@ func (item *Item) Expired() bool {
return item.Expiration.Before(time.Now())
}
// Return the Atime
func (item *Item) LastAccessed() *time.Time {
return item.Atime
}
func (item *Item) Accessed() {
now := time.Now()
item.Atime = &now
}
// Return the Ctime
func (item *Item) Created() *time.Time {
return item.Ctime
}
const (
// For use with functions that take an expiration time.
NoExpiration time.Duration = -1
@ -42,6 +59,7 @@ type cache struct {
defaultExpiration time.Duration
items map[string]*Item
janitor *janitor
maxItems int
}
// Add an item to the cache, replacing any existing item. If the duration is 0
@ -57,16 +75,40 @@ func (c *cache) Set(k string, x interface{}, d time.Duration) {
func (c *cache) set(k string, x interface{}, d time.Duration) {
var e *time.Time
t := time.Now()
at := &t
ct := &t
if d == DefaultExpiration {
d = c.defaultExpiration
}
if d > 0 {
t := time.Now().Add(d)
t = time.Now().Add(d)
e = &t
}
c.items[k] = &Item{
Object: x,
Expiration: e,
if c.maxItems > 0 {
// We don't want to hit the expiration if we're updating.
_, found := c.get(k)
if ! found {
// Create, means maybe expire
c.ExpireLRU()
} else {
// Update, means don't expire, or change the Ctime
ct = c.items[k].Created()
}
c.items[k] = &Item{
Object: x,
Expiration: e,
Ctime: ct,
Atime: at,
}
} else {
// Not using maxItems
c.items[k] = &Item{
Object: x,
Expiration: e,
}
}
}
@ -112,6 +154,9 @@ func (c *cache) get(k string) (interface{}, bool) {
if !found || item.Expired() {
return nil, false
}
if c.maxItems > 0 { item.Accessed() }
return item.Object, true
}
@ -127,6 +172,9 @@ func (c *cache) Increment(k string, n int64) error {
c.Unlock()
return fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
switch v.Object.(type) {
case int:
v.Object = v.Object.(int) + int(n)
@ -174,6 +222,9 @@ func (c *cache) IncrementFloat(k string, n float64) error {
c.Unlock()
return fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
switch v.Object.(type) {
case float32:
v.Object = v.Object.(float32) + float32(n)
@ -197,6 +248,9 @@ func (c *cache) IncrementInt(k string, n int) (int, error) {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
rv, ok := v.Object.(int)
if !ok {
c.Unlock()
@ -218,6 +272,9 @@ func (c *cache) IncrementInt8(k string, n int8) (int8, error) {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
rv, ok := v.Object.(int8)
if !ok {
c.Unlock()
@ -239,6 +296,9 @@ func (c *cache) IncrementInt16(k string, n int16) (int16, error) {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
rv, ok := v.Object.(int16)
if !ok {
c.Unlock()
@ -260,6 +320,9 @@ func (c *cache) IncrementInt32(k string, n int32) (int32, error) {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
rv, ok := v.Object.(int32)
if !ok {
c.Unlock()
@ -281,6 +344,9 @@ func (c *cache) IncrementInt64(k string, n int64) (int64, error) {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
rv, ok := v.Object.(int64)
if !ok {
c.Unlock()
@ -302,6 +368,9 @@ func (c *cache) IncrementUint(k string, n uint) (uint, error) {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
rv, ok := v.Object.(uint)
if !ok {
c.Unlock()
@ -323,6 +392,9 @@ func (c *cache) IncrementUintptr(k string, n uintptr) (uintptr, error) {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
rv, ok := v.Object.(uintptr)
if !ok {
c.Unlock()
@ -344,6 +416,9 @@ func (c *cache) IncrementUint8(k string, n uint8) (uint8, error) {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
rv, ok := v.Object.(uint8)
if !ok {
c.Unlock()
@ -365,6 +440,9 @@ func (c *cache) IncrementUint16(k string, n uint16) (uint16, error) {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
rv, ok := v.Object.(uint16)
if !ok {
c.Unlock()
@ -386,6 +464,9 @@ func (c *cache) IncrementUint32(k string, n uint32) (uint32, error) {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
rv, ok := v.Object.(uint32)
if !ok {
c.Unlock()
@ -407,6 +488,9 @@ func (c *cache) IncrementUint64(k string, n uint64) (uint64, error) {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
rv, ok := v.Object.(uint64)
if !ok {
c.Unlock()
@ -428,6 +512,9 @@ func (c *cache) IncrementFloat32(k string, n float32) (float32, error) {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
rv, ok := v.Object.(float32)
if !ok {
c.Unlock()
@ -449,6 +536,9 @@ func (c *cache) IncrementFloat64(k string, n float64) (float64, error) {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
rv, ok := v.Object.(float64)
if !ok {
c.Unlock()
@ -474,6 +564,9 @@ func (c *cache) Decrement(k string, n int64) error {
c.Unlock()
return fmt.Errorf("Item not found")
}
if c.maxItems > 0 { v.Accessed() }
switch v.Object.(type) {
case int:
v.Object = v.Object.(int) - int(n)
@ -521,6 +614,9 @@ func (c *cache) DecrementFloat(k string, n float64) error {
c.Unlock()
return fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
switch v.Object.(type) {
case float32:
v.Object = v.Object.(float32) - float32(n)
@ -544,6 +640,9 @@ func (c *cache) DecrementInt(k string, n int) (int, error) {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
rv, ok := v.Object.(int)
if !ok {
c.Unlock()
@ -565,6 +664,9 @@ func (c *cache) DecrementInt8(k string, n int8) (int8, error) {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
rv, ok := v.Object.(int8)
if !ok {
c.Unlock()
@ -586,6 +688,9 @@ func (c *cache) DecrementInt16(k string, n int16) (int16, error) {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
rv, ok := v.Object.(int16)
if !ok {
c.Unlock()
@ -607,6 +712,9 @@ func (c *cache) DecrementInt32(k string, n int32) (int32, error) {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
rv, ok := v.Object.(int32)
if !ok {
c.Unlock()
@ -628,6 +736,9 @@ func (c *cache) DecrementInt64(k string, n int64) (int64, error) {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
rv, ok := v.Object.(int64)
if !ok {
c.Unlock()
@ -649,6 +760,9 @@ func (c *cache) DecrementUint(k string, n uint) (uint, error) {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
rv, ok := v.Object.(uint)
if !ok {
c.Unlock()
@ -670,6 +784,9 @@ func (c *cache) DecrementUintptr(k string, n uintptr) (uintptr, error) {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
rv, ok := v.Object.(uintptr)
if !ok {
c.Unlock()
@ -691,6 +808,9 @@ func (c *cache) DecrementUint8(k string, n uint8) (uint8, error) {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
rv, ok := v.Object.(uint8)
if !ok {
c.Unlock()
@ -712,6 +832,9 @@ func (c *cache) DecrementUint16(k string, n uint16) (uint16, error) {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
rv, ok := v.Object.(uint16)
if !ok {
c.Unlock()
@ -733,6 +856,9 @@ func (c *cache) DecrementUint32(k string, n uint32) (uint32, error) {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
rv, ok := v.Object.(uint32)
if !ok {
c.Unlock()
@ -754,6 +880,9 @@ func (c *cache) DecrementUint64(k string, n uint64) (uint64, error) {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
rv, ok := v.Object.(uint64)
if !ok {
c.Unlock()
@ -775,6 +904,9 @@ func (c *cache) DecrementFloat32(k string, n float32) (float32, error) {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
rv, ok := v.Object.(float32)
if !ok {
c.Unlock()
@ -796,6 +928,9 @@ func (c *cache) DecrementFloat64(k string, n float64) (float64, error) {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
rv, ok := v.Object.(float64)
if !ok {
c.Unlock()
@ -927,6 +1062,19 @@ func (c *cache) ItemCount() int {
return n
}
// Returns the number of items in the cache *that have not expired*
func (c *cache) itemCount() int {
n := 0
now := time.Now()
for _, v := range c.items {
if v.Expiration == nil || ! v.Expiration.Before(now) {
n++
}
}
return n
}
// Delete all items from the cache.
func (c *cache) Flush() {
c.Lock()
@ -934,6 +1082,50 @@ func (c *cache) Flush() {
c.Unlock()
}
// Find oldest N items, and Expire them.
// Returns the number "expired".
// BIG HAIRY ALERT: This function assumes it is called
// from inside a c.Lock() .. c.Unlock() block.
func (c *cache) ExpireLRU() {
// We need the number of UNexpired Items
itemCount := c.itemCount()
if itemCount >= c.maxItems {
// Cache the current time.
// We do this "early" so that when we set an Expiration,
// it is definitely "do" when the janitor ticks
notw := time.Now()
var lastTime int64 = 0
var lastName string = ""
for k, v := range c.items {
if v.Expired() == false {
// unexpired item
atime := v.LastAccessed().UnixNano()
if lastTime == 0 {
// We need to expire something, so let's start with the first item
lastTime = atime
lastName = k
} else if atime < lastTime {
// We found a least-recently-used item
lastTime = atime
lastName = k
}
}
}
if lastTime > 0 {
// We expire the item, but making it look .Expired(),
// so the janitor will clean it up for us
c.items[lastName].Expiration = &notw
}
}
}
type janitor struct {
Interval time.Duration
stop chan bool
@ -964,19 +1156,20 @@ func runJanitor(c *cache, ci time.Duration) {
go j.Run(c)
}
func newCache(de time.Duration, m map[string]*Item) *cache {
func newCache(de time.Duration, m map[string]*Item, mi int) *cache {
if de == 0 {
de = -1
}
c := &cache{
defaultExpiration: de,
maxItems: mi,
items: m,
}
return c
}
func newCacheWithJanitor(de time.Duration, ci time.Duration, m map[string]*Item) *Cache {
c := newCache(de, m)
func newCacheWithJanitor(de time.Duration, ci time.Duration, m map[string]*Item, mi int) *Cache {
c := newCache(de, m, mi)
// 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
@ -995,9 +1188,9 @@ func newCacheWithJanitor(de time.Duration, ci time.Duration, m map[string]*Item)
// 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(defaultExpiration, cleanupInterval time.Duration) *Cache {
func New(defaultExpiration, cleanupInterval time.Duration, maxItems int) *Cache {
items := make(map[string]*Item)
return newCacheWithJanitor(defaultExpiration, cleanupInterval, items)
return newCacheWithJanitor(defaultExpiration, cleanupInterval, items, maxItems)
}
// Return a new cache with a given default expiration duration and cleanup
@ -1021,6 +1214,6 @@ func New(defaultExpiration, cleanupInterval time.Duration) *Cache {
// gob.Register() the individual types stored in the cache before encoding a
// map retrieved with c.Items(), and to register those same types before
// decoding a blob containing an items map.
func NewFrom(defaultExpiration, cleanupInterval time.Duration, items map[string]*Item) *Cache {
return newCacheWithJanitor(defaultExpiration, cleanupInterval, items)
func NewFrom(defaultExpiration, cleanupInterval time.Duration, items map[string]*Item, maxItems int) *Cache {
return newCacheWithJanitor(defaultExpiration, cleanupInterval, items, maxItems)
}

View File

@ -16,7 +16,7 @@ type TestStruct struct {
}
func TestCache(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
a, found := tc.Get("a")
if found || a != nil {
@ -71,7 +71,7 @@ func TestCache(t *testing.T) {
func TestCacheTimes(t *testing.T) {
var found bool
tc := New(50*time.Millisecond, 1*time.Millisecond)
tc := New(50*time.Millisecond, 1*time.Millisecond, 0)
tc.Set("a", 1, DefaultExpiration)
tc.Set("b", 2, NoExpiration)
tc.Set("c", 3, 20*time.Millisecond)
@ -117,7 +117,7 @@ func TestNewFrom(t *testing.T) {
Expiration: nil,
},
}
tc := NewFrom(DefaultExpiration, 0, m)
tc := NewFrom(DefaultExpiration, 0, m, 0)
a, found := tc.Get("a")
if !found {
t.Fatal("Did not find a")
@ -135,7 +135,7 @@ func TestNewFrom(t *testing.T) {
}
func TestStorePointerToStruct(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("foo", &TestStruct{Num: 1}, DefaultExpiration)
x, found := tc.Get("foo")
if !found {
@ -155,7 +155,7 @@ func TestStorePointerToStruct(t *testing.T) {
}
func TestIncrementWithInt(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("tint", 1, DefaultExpiration)
err := tc.Increment("tint", 2)
if err != nil {
@ -171,7 +171,7 @@ func TestIncrementWithInt(t *testing.T) {
}
func TestIncrementWithInt8(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("tint8", int8(1), DefaultExpiration)
err := tc.Increment("tint8", 2)
if err != nil {
@ -187,7 +187,7 @@ func TestIncrementWithInt8(t *testing.T) {
}
func TestIncrementWithInt16(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("tint16", int16(1), DefaultExpiration)
err := tc.Increment("tint16", 2)
if err != nil {
@ -203,7 +203,7 @@ func TestIncrementWithInt16(t *testing.T) {
}
func TestIncrementWithInt32(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("tint32", int32(1), DefaultExpiration)
err := tc.Increment("tint32", 2)
if err != nil {
@ -219,7 +219,7 @@ func TestIncrementWithInt32(t *testing.T) {
}
func TestIncrementWithInt64(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("tint64", int64(1), DefaultExpiration)
err := tc.Increment("tint64", 2)
if err != nil {
@ -235,7 +235,7 @@ func TestIncrementWithInt64(t *testing.T) {
}
func TestIncrementWithUint(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("tuint", uint(1), DefaultExpiration)
err := tc.Increment("tuint", 2)
if err != nil {
@ -251,7 +251,7 @@ func TestIncrementWithUint(t *testing.T) {
}
func TestIncrementWithUintptr(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("tuintptr", uintptr(1), DefaultExpiration)
err := tc.Increment("tuintptr", 2)
if err != nil {
@ -268,7 +268,7 @@ func TestIncrementWithUintptr(t *testing.T) {
}
func TestIncrementWithUint8(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("tuint8", uint8(1), DefaultExpiration)
err := tc.Increment("tuint8", 2)
if err != nil {
@ -284,7 +284,7 @@ func TestIncrementWithUint8(t *testing.T) {
}
func TestIncrementWithUint16(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("tuint16", uint16(1), DefaultExpiration)
err := tc.Increment("tuint16", 2)
if err != nil {
@ -301,7 +301,7 @@ func TestIncrementWithUint16(t *testing.T) {
}
func TestIncrementWithUint32(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("tuint32", uint32(1), DefaultExpiration)
err := tc.Increment("tuint32", 2)
if err != nil {
@ -317,7 +317,7 @@ func TestIncrementWithUint32(t *testing.T) {
}
func TestIncrementWithUint64(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("tuint64", uint64(1), DefaultExpiration)
err := tc.Increment("tuint64", 2)
if err != nil {
@ -334,7 +334,7 @@ func TestIncrementWithUint64(t *testing.T) {
}
func TestIncrementWithFloat32(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("float32", float32(1.5), DefaultExpiration)
err := tc.Increment("float32", 2)
if err != nil {
@ -350,7 +350,7 @@ func TestIncrementWithFloat32(t *testing.T) {
}
func TestIncrementWithFloat64(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("float64", float64(1.5), DefaultExpiration)
err := tc.Increment("float64", 2)
if err != nil {
@ -366,7 +366,7 @@ func TestIncrementWithFloat64(t *testing.T) {
}
func TestIncrementFloatWithFloat32(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("float32", float32(1.5), DefaultExpiration)
err := tc.IncrementFloat("float32", 2)
if err != nil {
@ -382,7 +382,7 @@ func TestIncrementFloatWithFloat32(t *testing.T) {
}
func TestIncrementFloatWithFloat64(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("float64", float64(1.5), DefaultExpiration)
err := tc.IncrementFloat("float64", 2)
if err != nil {
@ -398,7 +398,7 @@ func TestIncrementFloatWithFloat64(t *testing.T) {
}
func TestDecrementWithInt(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("int", int(5), DefaultExpiration)
err := tc.Decrement("int", 2)
if err != nil {
@ -414,7 +414,7 @@ func TestDecrementWithInt(t *testing.T) {
}
func TestDecrementWithInt8(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("int8", int8(5), DefaultExpiration)
err := tc.Decrement("int8", 2)
if err != nil {
@ -430,7 +430,7 @@ func TestDecrementWithInt8(t *testing.T) {
}
func TestDecrementWithInt16(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("int16", int16(5), DefaultExpiration)
err := tc.Decrement("int16", 2)
if err != nil {
@ -446,7 +446,7 @@ func TestDecrementWithInt16(t *testing.T) {
}
func TestDecrementWithInt32(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("int32", int32(5), DefaultExpiration)
err := tc.Decrement("int32", 2)
if err != nil {
@ -462,7 +462,7 @@ func TestDecrementWithInt32(t *testing.T) {
}
func TestDecrementWithInt64(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("int64", int64(5), DefaultExpiration)
err := tc.Decrement("int64", 2)
if err != nil {
@ -478,7 +478,7 @@ func TestDecrementWithInt64(t *testing.T) {
}
func TestDecrementWithUint(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("uint", uint(5), DefaultExpiration)
err := tc.Decrement("uint", 2)
if err != nil {
@ -494,7 +494,7 @@ func TestDecrementWithUint(t *testing.T) {
}
func TestDecrementWithUintptr(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("uintptr", uintptr(5), DefaultExpiration)
err := tc.Decrement("uintptr", 2)
if err != nil {
@ -510,7 +510,7 @@ func TestDecrementWithUintptr(t *testing.T) {
}
func TestDecrementWithUint8(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("uint8", uint8(5), DefaultExpiration)
err := tc.Decrement("uint8", 2)
if err != nil {
@ -526,7 +526,7 @@ func TestDecrementWithUint8(t *testing.T) {
}
func TestDecrementWithUint16(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("uint16", uint16(5), DefaultExpiration)
err := tc.Decrement("uint16", 2)
if err != nil {
@ -542,7 +542,7 @@ func TestDecrementWithUint16(t *testing.T) {
}
func TestDecrementWithUint32(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("uint32", uint32(5), DefaultExpiration)
err := tc.Decrement("uint32", 2)
if err != nil {
@ -558,7 +558,7 @@ func TestDecrementWithUint32(t *testing.T) {
}
func TestDecrementWithUint64(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("uint64", uint64(5), DefaultExpiration)
err := tc.Decrement("uint64", 2)
if err != nil {
@ -574,7 +574,7 @@ func TestDecrementWithUint64(t *testing.T) {
}
func TestDecrementWithFloat32(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("float32", float32(5.5), DefaultExpiration)
err := tc.Decrement("float32", 2)
if err != nil {
@ -590,7 +590,7 @@ func TestDecrementWithFloat32(t *testing.T) {
}
func TestDecrementWithFloat64(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("float64", float64(5.5), DefaultExpiration)
err := tc.Decrement("float64", 2)
if err != nil {
@ -606,7 +606,7 @@ func TestDecrementWithFloat64(t *testing.T) {
}
func TestDecrementFloatWithFloat32(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("float32", float32(5.5), DefaultExpiration)
err := tc.DecrementFloat("float32", 2)
if err != nil {
@ -622,7 +622,7 @@ func TestDecrementFloatWithFloat32(t *testing.T) {
}
func TestDecrementFloatWithFloat64(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("float64", float64(5.5), DefaultExpiration)
err := tc.DecrementFloat("float64", 2)
if err != nil {
@ -638,7 +638,7 @@ func TestDecrementFloatWithFloat64(t *testing.T) {
}
func TestIncrementInt(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("tint", 1, DefaultExpiration)
n, err := tc.IncrementInt("tint", 2)
if err != nil {
@ -657,7 +657,7 @@ func TestIncrementInt(t *testing.T) {
}
func TestIncrementInt8(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("tint8", int8(1), DefaultExpiration)
n, err := tc.IncrementInt8("tint8", 2)
if err != nil {
@ -676,7 +676,7 @@ func TestIncrementInt8(t *testing.T) {
}
func TestIncrementInt16(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("tint16", int16(1), DefaultExpiration)
n, err := tc.IncrementInt16("tint16", 2)
if err != nil {
@ -695,7 +695,7 @@ func TestIncrementInt16(t *testing.T) {
}
func TestIncrementInt32(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("tint32", int32(1), DefaultExpiration)
n, err := tc.IncrementInt32("tint32", 2)
if err != nil {
@ -714,7 +714,7 @@ func TestIncrementInt32(t *testing.T) {
}
func TestIncrementInt64(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("tint64", int64(1), DefaultExpiration)
n, err := tc.IncrementInt64("tint64", 2)
if err != nil {
@ -733,7 +733,7 @@ func TestIncrementInt64(t *testing.T) {
}
func TestIncrementUint(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("tuint", uint(1), DefaultExpiration)
n, err := tc.IncrementUint("tuint", 2)
if err != nil {
@ -752,7 +752,7 @@ func TestIncrementUint(t *testing.T) {
}
func TestIncrementUintptr(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("tuintptr", uintptr(1), DefaultExpiration)
n, err := tc.IncrementUintptr("tuintptr", 2)
if err != nil {
@ -771,7 +771,7 @@ func TestIncrementUintptr(t *testing.T) {
}
func TestIncrementUint8(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("tuint8", uint8(1), DefaultExpiration)
n, err := tc.IncrementUint8("tuint8", 2)
if err != nil {
@ -790,7 +790,7 @@ func TestIncrementUint8(t *testing.T) {
}
func TestIncrementUint16(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("tuint16", uint16(1), DefaultExpiration)
n, err := tc.IncrementUint16("tuint16", 2)
if err != nil {
@ -809,7 +809,7 @@ func TestIncrementUint16(t *testing.T) {
}
func TestIncrementUint32(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("tuint32", uint32(1), DefaultExpiration)
n, err := tc.IncrementUint32("tuint32", 2)
if err != nil {
@ -828,7 +828,7 @@ func TestIncrementUint32(t *testing.T) {
}
func TestIncrementUint64(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("tuint64", uint64(1), DefaultExpiration)
n, err := tc.IncrementUint64("tuint64", 2)
if err != nil {
@ -847,7 +847,7 @@ func TestIncrementUint64(t *testing.T) {
}
func TestIncrementFloat32(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("float32", float32(1.5), DefaultExpiration)
n, err := tc.IncrementFloat32("float32", 2)
if err != nil {
@ -866,7 +866,7 @@ func TestIncrementFloat32(t *testing.T) {
}
func TestIncrementFloat64(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("float64", float64(1.5), DefaultExpiration)
n, err := tc.IncrementFloat64("float64", 2)
if err != nil {
@ -885,7 +885,7 @@ func TestIncrementFloat64(t *testing.T) {
}
func TestDecrementInt8(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("int8", int8(5), DefaultExpiration)
n, err := tc.DecrementInt8("int8", 2)
if err != nil {
@ -904,7 +904,7 @@ func TestDecrementInt8(t *testing.T) {
}
func TestDecrementInt16(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("int16", int16(5), DefaultExpiration)
n, err := tc.DecrementInt16("int16", 2)
if err != nil {
@ -923,7 +923,7 @@ func TestDecrementInt16(t *testing.T) {
}
func TestDecrementInt32(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("int32", int32(5), DefaultExpiration)
n, err := tc.DecrementInt32("int32", 2)
if err != nil {
@ -942,7 +942,7 @@ func TestDecrementInt32(t *testing.T) {
}
func TestDecrementInt64(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("int64", int64(5), DefaultExpiration)
n, err := tc.DecrementInt64("int64", 2)
if err != nil {
@ -961,7 +961,7 @@ func TestDecrementInt64(t *testing.T) {
}
func TestDecrementUint(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("uint", uint(5), DefaultExpiration)
n, err := tc.DecrementUint("uint", 2)
if err != nil {
@ -980,7 +980,7 @@ func TestDecrementUint(t *testing.T) {
}
func TestDecrementUintptr(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("uintptr", uintptr(5), DefaultExpiration)
n, err := tc.DecrementUintptr("uintptr", 2)
if err != nil {
@ -999,7 +999,7 @@ func TestDecrementUintptr(t *testing.T) {
}
func TestDecrementUint8(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("uint8", uint8(5), DefaultExpiration)
n, err := tc.DecrementUint8("uint8", 2)
if err != nil {
@ -1018,7 +1018,7 @@ func TestDecrementUint8(t *testing.T) {
}
func TestDecrementUint16(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("uint16", uint16(5), DefaultExpiration)
n, err := tc.DecrementUint16("uint16", 2)
if err != nil {
@ -1037,7 +1037,7 @@ func TestDecrementUint16(t *testing.T) {
}
func TestDecrementUint32(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("uint32", uint32(5), DefaultExpiration)
n, err := tc.DecrementUint32("uint32", 2)
if err != nil {
@ -1056,7 +1056,7 @@ func TestDecrementUint32(t *testing.T) {
}
func TestDecrementUint64(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("uint64", uint64(5), DefaultExpiration)
n, err := tc.DecrementUint64("uint64", 2)
if err != nil {
@ -1075,7 +1075,7 @@ func TestDecrementUint64(t *testing.T) {
}
func TestDecrementFloat32(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("float32", float32(5), DefaultExpiration)
n, err := tc.DecrementFloat32("float32", 2)
if err != nil {
@ -1094,7 +1094,7 @@ func TestDecrementFloat32(t *testing.T) {
}
func TestDecrementFloat64(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("float64", float64(5), DefaultExpiration)
n, err := tc.DecrementFloat64("float64", 2)
if err != nil {
@ -1113,7 +1113,7 @@ func TestDecrementFloat64(t *testing.T) {
}
func TestAdd(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
err := tc.Add("foo", "bar", DefaultExpiration)
if err != nil {
t.Error("Couldn't add foo even though it shouldn't exist")
@ -1125,7 +1125,7 @@ func TestAdd(t *testing.T) {
}
func TestReplace(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
err := tc.Replace("foo", "bar", DefaultExpiration)
if err == nil {
t.Error("Replaced foo when it shouldn't exist")
@ -1138,7 +1138,7 @@ func TestReplace(t *testing.T) {
}
func TestDelete(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("foo", "bar", DefaultExpiration)
tc.Delete("foo")
x, found := tc.Get("foo")
@ -1151,7 +1151,7 @@ func TestDelete(t *testing.T) {
}
func TestItemCount(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("foo", "1", DefaultExpiration)
tc.Set("bar", "2", DefaultExpiration)
tc.Set("baz", "3", DefaultExpiration)
@ -1161,7 +1161,7 @@ func TestItemCount(t *testing.T) {
}
func TestFlush(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("foo", "bar", DefaultExpiration)
tc.Set("baz", "yes", DefaultExpiration)
tc.Flush()
@ -1182,7 +1182,7 @@ func TestFlush(t *testing.T) {
}
func TestIncrementOverflowInt(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("int8", int8(127), DefaultExpiration)
err := tc.Increment("int8", 1)
if err != nil {
@ -1197,7 +1197,7 @@ func TestIncrementOverflowInt(t *testing.T) {
}
func TestIncrementOverflowUint(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("uint8", uint8(255), DefaultExpiration)
err := tc.Increment("uint8", 1)
if err != nil {
@ -1211,7 +1211,7 @@ func TestIncrementOverflowUint(t *testing.T) {
}
func TestDecrementUnderflowUint(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("uint8", uint8(0), DefaultExpiration)
err := tc.Decrement("uint8", 1)
if err != nil {
@ -1225,7 +1225,7 @@ func TestDecrementUnderflowUint(t *testing.T) {
}
func TestCacheSerialization(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
testFillAndSerialize(t, tc)
// Check if gob.Register behaves properly even after multiple gob.Register
@ -1261,7 +1261,7 @@ func testFillAndSerialize(t *testing.T, tc *Cache) {
t.Fatal("Couldn't save cache to fp:", err)
}
oc := New(DefaultExpiration, 0)
oc := New(DefaultExpiration, 0, 0)
err = oc.Load(fp)
if err != nil {
t.Fatal("Couldn't load cache from fp:", err)
@ -1352,7 +1352,7 @@ func testFillAndSerialize(t *testing.T, tc *Cache) {
}
func TestFileSerialization(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Add("a", "a", DefaultExpiration)
tc.Add("b", "b", DefaultExpiration)
f, err := ioutil.TempFile("", "go-cache-cache.dat")
@ -1363,7 +1363,7 @@ func TestFileSerialization(t *testing.T) {
f.Close()
tc.SaveFile(fname)
oc := New(DefaultExpiration, 0)
oc := New(DefaultExpiration, 0, 0)
oc.Add("a", "aa", 0) // this should not be overwritten
err = oc.LoadFile(fname)
if err != nil {
@ -1391,7 +1391,7 @@ func TestFileSerialization(t *testing.T) {
}
func TestSerializeUnserializable(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
ch := make(chan bool, 1)
ch <- true
tc.Set("chan", ch, DefaultExpiration)
@ -1404,7 +1404,7 @@ func TestSerializeUnserializable(t *testing.T) {
func BenchmarkCacheGet(b *testing.B) {
b.StopTimer()
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("foo", "bar", DefaultExpiration)
b.StartTimer()
for i := 0; i < b.N; i++ {
@ -1428,7 +1428,7 @@ func BenchmarkRWMutexMapGet(b *testing.B) {
func BenchmarkCacheGetConcurrent(b *testing.B) {
b.StopTimer()
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
tc.Set("foo", "bar", DefaultExpiration)
wg := new(sync.WaitGroup)
workers := runtime.NumCPU()
@ -1476,7 +1476,7 @@ func BenchmarkCacheGetManyConcurrent(b *testing.B) {
// in sharded_test.go.
b.StopTimer()
n := 10000
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
keys := make([]string, n)
for i := 0; i < n; i++ {
k := "foo" + strconv.Itoa(n)
@ -1500,7 +1500,7 @@ func BenchmarkCacheGetManyConcurrent(b *testing.B) {
func BenchmarkCacheSet(b *testing.B) {
b.StopTimer()
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
b.StartTimer()
for i := 0; i < b.N; i++ {
tc.Set("foo", "bar", DefaultExpiration)
@ -1521,7 +1521,7 @@ func BenchmarkRWMutexMapSet(b *testing.B) {
func BenchmarkCacheSetDelete(b *testing.B) {
b.StopTimer()
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
b.StartTimer()
for i := 0; i < b.N; i++ {
tc.Set("foo", "bar", DefaultExpiration)
@ -1546,7 +1546,7 @@ func BenchmarkRWMutexMapSetDelete(b *testing.B) {
func BenchmarkCacheSetDeleteSingleLock(b *testing.B) {
b.StopTimer()
tc := New(DefaultExpiration, 0)
tc := New(DefaultExpiration, 0, 0)
b.StartTimer()
for i := 0; i < b.N; i++ {
tc.Lock()