From 49090a885f950bfc59f07f343028172704ea275f Mon Sep 17 00:00:00 2001 From: Jin Date: Thu, 5 Mar 2015 18:30:37 +0100 Subject: [PATCH] adding extra features for the library Signed-off-by: Moath Almallahi --- README.md | 8 +++ cache.go | 98 +++++++++++++++++++------------ cache_test.go | 160 +++++++++++++++++++++++++------------------------- size.go | 75 +++++++++++++++++++++++ size_test.go | 27 +++++++++ 5 files changed, 250 insertions(+), 118 deletions(-) create mode 100644 size.go create mode 100644 size_test.go diff --git a/README.md b/README.md index 92e8a74..38347d9 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,14 @@ cache can be saved to and loaded from a file (using `c.Items()` to retrieve the items map to serialize, and `NewFrom()` to create a cache from a deserialized one) to recover from downtime quickly. (See the docs for `NewFrom()` for caveats.) +### Updates + +add the limitation of max capacity on the total memory consumption. + +- first version: If the cache is full, stop to set the new key. + +- second version: If the cache is full, run some replacement algorithm. + ### Installation `go get github.com/pmylund/go-cache` diff --git a/cache.go b/cache.go index 12f7f0b..9ee791a 100644 --- a/cache.go +++ b/cache.go @@ -10,6 +10,11 @@ import ( "time" ) +var ( + ErrKeyExists = fmt.Errorf("item already exists") + ErrCacheMiss = fmt.Errorf("item not found") +) + type Item struct { Object interface{} Expiration *time.Time @@ -42,6 +47,9 @@ type cache struct { defaultExpiration time.Duration items map[string]*Item janitor *janitor + + // capacity - max number of cached items + capacity int } // Add an item to the cache, replacing any existing item. If the duration is 0 @@ -64,6 +72,14 @@ func (c *cache) set(k string, x interface{}, d time.Duration) { t := time.Now().Add(d) e = &t } + + oldItem, _ := c.get(k) + + if oldItem == nil && len(c.items) >= c.capacity { + // the new key, but the capacity is reached + return + } + c.items[k] = &Item{ Object: x, Expiration: e, @@ -77,7 +93,7 @@ func (c *cache) Add(k string, x interface{}, d time.Duration) error { _, found := c.get(k) if found { c.Unlock() - return fmt.Errorf("Item %s already exists", k) + return ErrKeyExists } c.set(k, x, d) c.Unlock() @@ -109,9 +125,12 @@ func (c *cache) Get(k string) (interface{}, bool) { func (c *cache) get(k string) (interface{}, bool) { item, found := c.items[k] - if !found || item.Expired() { + if !found { return nil, false } + if item.Expired() { + return item.Object, false + } return item.Object, true } @@ -125,7 +144,7 @@ func (c *cache) Increment(k string, n int64) error { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return fmt.Errorf("Item %s not found", k) + return ErrCacheMiss } switch v.Object.(type) { case int: @@ -172,7 +191,7 @@ func (c *cache) IncrementFloat(k string, n float64) error { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return fmt.Errorf("Item %s not found", k) + return ErrCacheMiss } switch v.Object.(type) { case float32: @@ -195,7 +214,7 @@ func (c *cache) IncrementInt(k string, n int) (int, error) { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return 0, fmt.Errorf("Item %s not found", k) + return 0, ErrCacheMiss } rv, ok := v.Object.(int) if !ok { @@ -216,7 +235,7 @@ func (c *cache) IncrementInt8(k string, n int8) (int8, error) { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return 0, fmt.Errorf("Item %s not found", k) + return 0, ErrCacheMiss } rv, ok := v.Object.(int8) if !ok { @@ -237,7 +256,7 @@ func (c *cache) IncrementInt16(k string, n int16) (int16, error) { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return 0, fmt.Errorf("Item %s not found", k) + return 0, ErrCacheMiss } rv, ok := v.Object.(int16) if !ok { @@ -258,7 +277,7 @@ func (c *cache) IncrementInt32(k string, n int32) (int32, error) { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return 0, fmt.Errorf("Item %s not found", k) + return 0, ErrCacheMiss } rv, ok := v.Object.(int32) if !ok { @@ -279,7 +298,7 @@ func (c *cache) IncrementInt64(k string, n int64) (int64, error) { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return 0, fmt.Errorf("Item %s not found", k) + return 0, ErrCacheMiss } rv, ok := v.Object.(int64) if !ok { @@ -300,7 +319,7 @@ func (c *cache) IncrementUint(k string, n uint) (uint, error) { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return 0, fmt.Errorf("Item %s not found", k) + return 0, ErrCacheMiss } rv, ok := v.Object.(uint) if !ok { @@ -321,7 +340,7 @@ func (c *cache) IncrementUintptr(k string, n uintptr) (uintptr, error) { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return 0, fmt.Errorf("Item %s not found", k) + return 0, ErrCacheMiss } rv, ok := v.Object.(uintptr) if !ok { @@ -342,7 +361,7 @@ func (c *cache) IncrementUint8(k string, n uint8) (uint8, error) { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return 0, fmt.Errorf("Item %s not found", k) + return 0, ErrCacheMiss } rv, ok := v.Object.(uint8) if !ok { @@ -363,7 +382,7 @@ func (c *cache) IncrementUint16(k string, n uint16) (uint16, error) { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return 0, fmt.Errorf("Item %s not found", k) + return 0, ErrCacheMiss } rv, ok := v.Object.(uint16) if !ok { @@ -384,7 +403,7 @@ func (c *cache) IncrementUint32(k string, n uint32) (uint32, error) { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return 0, fmt.Errorf("Item %s not found", k) + return 0, ErrCacheMiss } rv, ok := v.Object.(uint32) if !ok { @@ -405,7 +424,7 @@ func (c *cache) IncrementUint64(k string, n uint64) (uint64, error) { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return 0, fmt.Errorf("Item %s not found", k) + return 0, ErrCacheMiss } rv, ok := v.Object.(uint64) if !ok { @@ -426,7 +445,7 @@ func (c *cache) IncrementFloat32(k string, n float32) (float32, error) { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return 0, fmt.Errorf("Item %s not found", k) + return 0, ErrCacheMiss } rv, ok := v.Object.(float32) if !ok { @@ -447,7 +466,7 @@ func (c *cache) IncrementFloat64(k string, n float64) (float64, error) { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return 0, fmt.Errorf("Item %s not found", k) + return 0, ErrCacheMiss } rv, ok := v.Object.(float64) if !ok { @@ -472,7 +491,7 @@ func (c *cache) Decrement(k string, n int64) error { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return fmt.Errorf("Item not found") + return ErrCacheMiss } switch v.Object.(type) { case int: @@ -519,7 +538,7 @@ func (c *cache) DecrementFloat(k string, n float64) error { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return fmt.Errorf("Item %s not found", k) + return ErrCacheMiss } switch v.Object.(type) { case float32: @@ -542,7 +561,7 @@ func (c *cache) DecrementInt(k string, n int) (int, error) { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return 0, fmt.Errorf("Item %s not found", k) + return 0, ErrCacheMiss } rv, ok := v.Object.(int) if !ok { @@ -563,7 +582,7 @@ func (c *cache) DecrementInt8(k string, n int8) (int8, error) { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return 0, fmt.Errorf("Item %s not found", k) + return 0, ErrCacheMiss } rv, ok := v.Object.(int8) if !ok { @@ -584,7 +603,7 @@ func (c *cache) DecrementInt16(k string, n int16) (int16, error) { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return 0, fmt.Errorf("Item %s not found", k) + return 0, ErrCacheMiss } rv, ok := v.Object.(int16) if !ok { @@ -605,7 +624,7 @@ func (c *cache) DecrementInt32(k string, n int32) (int32, error) { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return 0, fmt.Errorf("Item %s not found", k) + return 0, ErrCacheMiss } rv, ok := v.Object.(int32) if !ok { @@ -626,7 +645,7 @@ func (c *cache) DecrementInt64(k string, n int64) (int64, error) { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return 0, fmt.Errorf("Item %s not found", k) + return 0, ErrCacheMiss } rv, ok := v.Object.(int64) if !ok { @@ -647,7 +666,7 @@ func (c *cache) DecrementUint(k string, n uint) (uint, error) { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return 0, fmt.Errorf("Item %s not found", k) + return 0, ErrCacheMiss } rv, ok := v.Object.(uint) if !ok { @@ -668,7 +687,7 @@ func (c *cache) DecrementUintptr(k string, n uintptr) (uintptr, error) { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return 0, fmt.Errorf("Item %s not found", k) + return 0, ErrCacheMiss } rv, ok := v.Object.(uintptr) if !ok { @@ -689,7 +708,7 @@ func (c *cache) DecrementUint8(k string, n uint8) (uint8, error) { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return 0, fmt.Errorf("Item %s not found", k) + return 0, ErrCacheMiss } rv, ok := v.Object.(uint8) if !ok { @@ -710,7 +729,7 @@ func (c *cache) DecrementUint16(k string, n uint16) (uint16, error) { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return 0, fmt.Errorf("Item %s not found", k) + return 0, ErrCacheMiss } rv, ok := v.Object.(uint16) if !ok { @@ -731,7 +750,7 @@ func (c *cache) DecrementUint32(k string, n uint32) (uint32, error) { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return 0, fmt.Errorf("Item %s not found", k) + return 0, ErrCacheMiss } rv, ok := v.Object.(uint32) if !ok { @@ -752,7 +771,7 @@ func (c *cache) DecrementUint64(k string, n uint64) (uint64, error) { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return 0, fmt.Errorf("Item %s not found", k) + return 0, ErrCacheMiss } rv, ok := v.Object.(uint64) if !ok { @@ -773,7 +792,7 @@ func (c *cache) DecrementFloat32(k string, n float32) (float32, error) { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return 0, fmt.Errorf("Item %s not found", k) + return 0, ErrCacheMiss } rv, ok := v.Object.(float32) if !ok { @@ -794,7 +813,7 @@ func (c *cache) DecrementFloat64(k string, n float64) (float64, error) { v, found := c.items[k] if !found || v.Expired() { c.Unlock() - return 0, fmt.Errorf("Item %s not found", k) + return 0, ErrCacheMiss } rv, ok := v.Object.(float64) if !ok { @@ -964,19 +983,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, capacity int) *cache { if de == 0 { de = -1 } c := &cache{ defaultExpiration: de, items: m, + capacity: capacity, } 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, capacity int) *Cache { + c := newCache(de, m, capacity) // 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 +1015,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, capacity int) *Cache { items := make(map[string]*Item) - return newCacheWithJanitor(defaultExpiration, cleanupInterval, items) + return newCacheWithJanitor(defaultExpiration, cleanupInterval, items, capacity) } // Return a new cache with a given default expiration duration and cleanup @@ -1021,6 +1041,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, capacity int) *Cache { + return newCacheWithJanitor(defaultExpiration, cleanupInterval, items, capacity) } diff --git a/cache_test.go b/cache_test.go index d5b2a60..d0e8ad5 100644 --- a/cache_test.go +++ b/cache_test.go @@ -15,8 +15,10 @@ type TestStruct struct { Children []*TestStruct } +const cacheCapacity int = 300 * 1024 + func TestCache(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) a, found := tc.Get("a") if found || a != nil { @@ -71,7 +73,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, cacheCapacity) tc.Set("a", 1, DefaultExpiration) tc.Set("b", 2, NoExpiration) tc.Set("c", 3, 20*time.Millisecond) @@ -117,7 +119,7 @@ func TestNewFrom(t *testing.T) { Expiration: nil, }, } - tc := NewFrom(DefaultExpiration, 0, m) + tc := NewFrom(DefaultExpiration, 0, m, cacheCapacity) a, found := tc.Get("a") if !found { t.Fatal("Did not find a") @@ -135,7 +137,7 @@ func TestNewFrom(t *testing.T) { } func TestStorePointerToStruct(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("foo", &TestStruct{Num: 1}, DefaultExpiration) x, found := tc.Get("foo") if !found { @@ -155,7 +157,7 @@ func TestStorePointerToStruct(t *testing.T) { } func TestIncrementWithInt(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("tint", 1, DefaultExpiration) err := tc.Increment("tint", 2) if err != nil { @@ -171,7 +173,7 @@ func TestIncrementWithInt(t *testing.T) { } func TestIncrementWithInt8(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("tint8", int8(1), DefaultExpiration) err := tc.Increment("tint8", 2) if err != nil { @@ -187,7 +189,7 @@ func TestIncrementWithInt8(t *testing.T) { } func TestIncrementWithInt16(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("tint16", int16(1), DefaultExpiration) err := tc.Increment("tint16", 2) if err != nil { @@ -203,7 +205,7 @@ func TestIncrementWithInt16(t *testing.T) { } func TestIncrementWithInt32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("tint32", int32(1), DefaultExpiration) err := tc.Increment("tint32", 2) if err != nil { @@ -219,7 +221,7 @@ func TestIncrementWithInt32(t *testing.T) { } func TestIncrementWithInt64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("tint64", int64(1), DefaultExpiration) err := tc.Increment("tint64", 2) if err != nil { @@ -235,7 +237,7 @@ func TestIncrementWithInt64(t *testing.T) { } func TestIncrementWithUint(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("tuint", uint(1), DefaultExpiration) err := tc.Increment("tuint", 2) if err != nil { @@ -251,7 +253,7 @@ func TestIncrementWithUint(t *testing.T) { } func TestIncrementWithUintptr(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("tuintptr", uintptr(1), DefaultExpiration) err := tc.Increment("tuintptr", 2) if err != nil { @@ -268,7 +270,7 @@ func TestIncrementWithUintptr(t *testing.T) { } func TestIncrementWithUint8(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("tuint8", uint8(1), DefaultExpiration) err := tc.Increment("tuint8", 2) if err != nil { @@ -284,7 +286,7 @@ func TestIncrementWithUint8(t *testing.T) { } func TestIncrementWithUint16(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("tuint16", uint16(1), DefaultExpiration) err := tc.Increment("tuint16", 2) if err != nil { @@ -301,7 +303,7 @@ func TestIncrementWithUint16(t *testing.T) { } func TestIncrementWithUint32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("tuint32", uint32(1), DefaultExpiration) err := tc.Increment("tuint32", 2) if err != nil { @@ -317,7 +319,7 @@ func TestIncrementWithUint32(t *testing.T) { } func TestIncrementWithUint64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("tuint64", uint64(1), DefaultExpiration) err := tc.Increment("tuint64", 2) if err != nil { @@ -334,7 +336,7 @@ func TestIncrementWithUint64(t *testing.T) { } func TestIncrementWithFloat32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("float32", float32(1.5), DefaultExpiration) err := tc.Increment("float32", 2) if err != nil { @@ -350,7 +352,7 @@ func TestIncrementWithFloat32(t *testing.T) { } func TestIncrementWithFloat64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("float64", float64(1.5), DefaultExpiration) err := tc.Increment("float64", 2) if err != nil { @@ -366,7 +368,7 @@ func TestIncrementWithFloat64(t *testing.T) { } func TestIncrementFloatWithFloat32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("float32", float32(1.5), DefaultExpiration) err := tc.IncrementFloat("float32", 2) if err != nil { @@ -382,7 +384,7 @@ func TestIncrementFloatWithFloat32(t *testing.T) { } func TestIncrementFloatWithFloat64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("float64", float64(1.5), DefaultExpiration) err := tc.IncrementFloat("float64", 2) if err != nil { @@ -398,7 +400,7 @@ func TestIncrementFloatWithFloat64(t *testing.T) { } func TestDecrementWithInt(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("int", int(5), DefaultExpiration) err := tc.Decrement("int", 2) if err != nil { @@ -414,7 +416,7 @@ func TestDecrementWithInt(t *testing.T) { } func TestDecrementWithInt8(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("int8", int8(5), DefaultExpiration) err := tc.Decrement("int8", 2) if err != nil { @@ -430,7 +432,7 @@ func TestDecrementWithInt8(t *testing.T) { } func TestDecrementWithInt16(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("int16", int16(5), DefaultExpiration) err := tc.Decrement("int16", 2) if err != nil { @@ -446,7 +448,7 @@ func TestDecrementWithInt16(t *testing.T) { } func TestDecrementWithInt32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("int32", int32(5), DefaultExpiration) err := tc.Decrement("int32", 2) if err != nil { @@ -462,7 +464,7 @@ func TestDecrementWithInt32(t *testing.T) { } func TestDecrementWithInt64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("int64", int64(5), DefaultExpiration) err := tc.Decrement("int64", 2) if err != nil { @@ -478,7 +480,7 @@ func TestDecrementWithInt64(t *testing.T) { } func TestDecrementWithUint(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("uint", uint(5), DefaultExpiration) err := tc.Decrement("uint", 2) if err != nil { @@ -494,7 +496,7 @@ func TestDecrementWithUint(t *testing.T) { } func TestDecrementWithUintptr(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("uintptr", uintptr(5), DefaultExpiration) err := tc.Decrement("uintptr", 2) if err != nil { @@ -510,7 +512,7 @@ func TestDecrementWithUintptr(t *testing.T) { } func TestDecrementWithUint8(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("uint8", uint8(5), DefaultExpiration) err := tc.Decrement("uint8", 2) if err != nil { @@ -526,7 +528,7 @@ func TestDecrementWithUint8(t *testing.T) { } func TestDecrementWithUint16(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("uint16", uint16(5), DefaultExpiration) err := tc.Decrement("uint16", 2) if err != nil { @@ -542,7 +544,7 @@ func TestDecrementWithUint16(t *testing.T) { } func TestDecrementWithUint32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("uint32", uint32(5), DefaultExpiration) err := tc.Decrement("uint32", 2) if err != nil { @@ -558,7 +560,7 @@ func TestDecrementWithUint32(t *testing.T) { } func TestDecrementWithUint64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("uint64", uint64(5), DefaultExpiration) err := tc.Decrement("uint64", 2) if err != nil { @@ -574,7 +576,7 @@ func TestDecrementWithUint64(t *testing.T) { } func TestDecrementWithFloat32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("float32", float32(5.5), DefaultExpiration) err := tc.Decrement("float32", 2) if err != nil { @@ -590,7 +592,7 @@ func TestDecrementWithFloat32(t *testing.T) { } func TestDecrementWithFloat64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("float64", float64(5.5), DefaultExpiration) err := tc.Decrement("float64", 2) if err != nil { @@ -606,7 +608,7 @@ func TestDecrementWithFloat64(t *testing.T) { } func TestDecrementFloatWithFloat32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("float32", float32(5.5), DefaultExpiration) err := tc.DecrementFloat("float32", 2) if err != nil { @@ -622,7 +624,7 @@ func TestDecrementFloatWithFloat32(t *testing.T) { } func TestDecrementFloatWithFloat64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("float64", float64(5.5), DefaultExpiration) err := tc.DecrementFloat("float64", 2) if err != nil { @@ -638,7 +640,7 @@ func TestDecrementFloatWithFloat64(t *testing.T) { } func TestIncrementInt(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("tint", 1, DefaultExpiration) n, err := tc.IncrementInt("tint", 2) if err != nil { @@ -657,7 +659,7 @@ func TestIncrementInt(t *testing.T) { } func TestIncrementInt8(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("tint8", int8(1), DefaultExpiration) n, err := tc.IncrementInt8("tint8", 2) if err != nil { @@ -676,7 +678,7 @@ func TestIncrementInt8(t *testing.T) { } func TestIncrementInt16(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("tint16", int16(1), DefaultExpiration) n, err := tc.IncrementInt16("tint16", 2) if err != nil { @@ -695,7 +697,7 @@ func TestIncrementInt16(t *testing.T) { } func TestIncrementInt32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("tint32", int32(1), DefaultExpiration) n, err := tc.IncrementInt32("tint32", 2) if err != nil { @@ -714,7 +716,7 @@ func TestIncrementInt32(t *testing.T) { } func TestIncrementInt64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("tint64", int64(1), DefaultExpiration) n, err := tc.IncrementInt64("tint64", 2) if err != nil { @@ -733,7 +735,7 @@ func TestIncrementInt64(t *testing.T) { } func TestIncrementUint(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("tuint", uint(1), DefaultExpiration) n, err := tc.IncrementUint("tuint", 2) if err != nil { @@ -752,7 +754,7 @@ func TestIncrementUint(t *testing.T) { } func TestIncrementUintptr(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("tuintptr", uintptr(1), DefaultExpiration) n, err := tc.IncrementUintptr("tuintptr", 2) if err != nil { @@ -771,7 +773,7 @@ func TestIncrementUintptr(t *testing.T) { } func TestIncrementUint8(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("tuint8", uint8(1), DefaultExpiration) n, err := tc.IncrementUint8("tuint8", 2) if err != nil { @@ -790,7 +792,7 @@ func TestIncrementUint8(t *testing.T) { } func TestIncrementUint16(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("tuint16", uint16(1), DefaultExpiration) n, err := tc.IncrementUint16("tuint16", 2) if err != nil { @@ -809,7 +811,7 @@ func TestIncrementUint16(t *testing.T) { } func TestIncrementUint32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("tuint32", uint32(1), DefaultExpiration) n, err := tc.IncrementUint32("tuint32", 2) if err != nil { @@ -828,7 +830,7 @@ func TestIncrementUint32(t *testing.T) { } func TestIncrementUint64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("tuint64", uint64(1), DefaultExpiration) n, err := tc.IncrementUint64("tuint64", 2) if err != nil { @@ -847,7 +849,7 @@ func TestIncrementUint64(t *testing.T) { } func TestIncrementFloat32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("float32", float32(1.5), DefaultExpiration) n, err := tc.IncrementFloat32("float32", 2) if err != nil { @@ -866,7 +868,7 @@ func TestIncrementFloat32(t *testing.T) { } func TestIncrementFloat64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("float64", float64(1.5), DefaultExpiration) n, err := tc.IncrementFloat64("float64", 2) if err != nil { @@ -885,7 +887,7 @@ func TestIncrementFloat64(t *testing.T) { } func TestDecrementInt8(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("int8", int8(5), DefaultExpiration) n, err := tc.DecrementInt8("int8", 2) if err != nil { @@ -904,7 +906,7 @@ func TestDecrementInt8(t *testing.T) { } func TestDecrementInt16(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("int16", int16(5), DefaultExpiration) n, err := tc.DecrementInt16("int16", 2) if err != nil { @@ -923,7 +925,7 @@ func TestDecrementInt16(t *testing.T) { } func TestDecrementInt32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("int32", int32(5), DefaultExpiration) n, err := tc.DecrementInt32("int32", 2) if err != nil { @@ -942,7 +944,7 @@ func TestDecrementInt32(t *testing.T) { } func TestDecrementInt64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("int64", int64(5), DefaultExpiration) n, err := tc.DecrementInt64("int64", 2) if err != nil { @@ -961,7 +963,7 @@ func TestDecrementInt64(t *testing.T) { } func TestDecrementUint(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("uint", uint(5), DefaultExpiration) n, err := tc.DecrementUint("uint", 2) if err != nil { @@ -980,7 +982,7 @@ func TestDecrementUint(t *testing.T) { } func TestDecrementUintptr(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("uintptr", uintptr(5), DefaultExpiration) n, err := tc.DecrementUintptr("uintptr", 2) if err != nil { @@ -999,7 +1001,7 @@ func TestDecrementUintptr(t *testing.T) { } func TestDecrementUint8(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("uint8", uint8(5), DefaultExpiration) n, err := tc.DecrementUint8("uint8", 2) if err != nil { @@ -1018,7 +1020,7 @@ func TestDecrementUint8(t *testing.T) { } func TestDecrementUint16(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("uint16", uint16(5), DefaultExpiration) n, err := tc.DecrementUint16("uint16", 2) if err != nil { @@ -1037,7 +1039,7 @@ func TestDecrementUint16(t *testing.T) { } func TestDecrementUint32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("uint32", uint32(5), DefaultExpiration) n, err := tc.DecrementUint32("uint32", 2) if err != nil { @@ -1056,7 +1058,7 @@ func TestDecrementUint32(t *testing.T) { } func TestDecrementUint64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("uint64", uint64(5), DefaultExpiration) n, err := tc.DecrementUint64("uint64", 2) if err != nil { @@ -1075,7 +1077,7 @@ func TestDecrementUint64(t *testing.T) { } func TestDecrementFloat32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("float32", float32(5), DefaultExpiration) n, err := tc.DecrementFloat32("float32", 2) if err != nil { @@ -1094,7 +1096,7 @@ func TestDecrementFloat32(t *testing.T) { } func TestDecrementFloat64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("float64", float64(5), DefaultExpiration) n, err := tc.DecrementFloat64("float64", 2) if err != nil { @@ -1113,7 +1115,7 @@ func TestDecrementFloat64(t *testing.T) { } func TestAdd(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) err := tc.Add("foo", "bar", DefaultExpiration) if err != nil { t.Error("Couldn't add foo even though it shouldn't exist") @@ -1125,7 +1127,7 @@ func TestAdd(t *testing.T) { } func TestReplace(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) err := tc.Replace("foo", "bar", DefaultExpiration) if err == nil { t.Error("Replaced foo when it shouldn't exist") @@ -1138,7 +1140,7 @@ func TestReplace(t *testing.T) { } func TestDelete(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("foo", "bar", DefaultExpiration) tc.Delete("foo") x, found := tc.Get("foo") @@ -1151,7 +1153,7 @@ func TestDelete(t *testing.T) { } func TestItemCount(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("foo", "1", DefaultExpiration) tc.Set("bar", "2", DefaultExpiration) tc.Set("baz", "3", DefaultExpiration) @@ -1161,7 +1163,7 @@ func TestItemCount(t *testing.T) { } func TestFlush(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("foo", "bar", DefaultExpiration) tc.Set("baz", "yes", DefaultExpiration) tc.Flush() @@ -1182,7 +1184,7 @@ func TestFlush(t *testing.T) { } func TestIncrementOverflowInt(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("int8", int8(127), DefaultExpiration) err := tc.Increment("int8", 1) if err != nil { @@ -1197,7 +1199,7 @@ func TestIncrementOverflowInt(t *testing.T) { } func TestIncrementOverflowUint(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("uint8", uint8(255), DefaultExpiration) err := tc.Increment("uint8", 1) if err != nil { @@ -1211,7 +1213,7 @@ func TestIncrementOverflowUint(t *testing.T) { } func TestDecrementUnderflowUint(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("uint8", uint8(0), DefaultExpiration) err := tc.Decrement("uint8", 1) if err != nil { @@ -1225,7 +1227,7 @@ func TestDecrementUnderflowUint(t *testing.T) { } func TestCacheSerialization(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) testFillAndSerialize(t, tc) // Check if gob.Register behaves properly even after multiple gob.Register @@ -1261,7 +1263,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, cacheCapacity) err = oc.Load(fp) if err != nil { t.Fatal("Couldn't load cache from fp:", err) @@ -1352,7 +1354,7 @@ func testFillAndSerialize(t *testing.T, tc *Cache) { } func TestFileSerialization(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Add("a", "a", DefaultExpiration) tc.Add("b", "b", DefaultExpiration) f, err := ioutil.TempFile("", "go-cache-cache.dat") @@ -1363,7 +1365,7 @@ func TestFileSerialization(t *testing.T) { f.Close() tc.SaveFile(fname) - oc := New(DefaultExpiration, 0) + oc := New(DefaultExpiration, 0, cacheCapacity) oc.Add("a", "aa", 0) // this should not be overwritten err = oc.LoadFile(fname) if err != nil { @@ -1391,20 +1393,20 @@ func TestFileSerialization(t *testing.T) { } func TestSerializeUnserializable(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) ch := make(chan bool, 1) ch <- true tc.Set("chan", ch, DefaultExpiration) fp := &bytes.Buffer{} err := tc.Save(fp) // this should fail gracefully - if err.Error() != "gob NewTypeObject can't handle type: chan bool" { + if err != nil && err.Error() != "gob NewTypeObject can't handle type: chan bool" { t.Error("Error from Save was not gob NewTypeObject can't handle type chan bool:", err) } } func BenchmarkCacheGet(b *testing.B) { b.StopTimer() - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("foo", "bar", DefaultExpiration) b.StartTimer() for i := 0; i < b.N; i++ { @@ -1428,7 +1430,7 @@ func BenchmarkRWMutexMapGet(b *testing.B) { func BenchmarkCacheGetConcurrent(b *testing.B) { b.StopTimer() - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) tc.Set("foo", "bar", DefaultExpiration) wg := new(sync.WaitGroup) workers := runtime.NumCPU() @@ -1476,7 +1478,7 @@ func BenchmarkCacheGetManyConcurrent(b *testing.B) { // in sharded_test.go. b.StopTimer() n := 10000 - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) keys := make([]string, n) for i := 0; i < n; i++ { k := "foo" + strconv.Itoa(n) @@ -1500,7 +1502,7 @@ func BenchmarkCacheGetManyConcurrent(b *testing.B) { func BenchmarkCacheSet(b *testing.B) { b.StopTimer() - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) b.StartTimer() for i := 0; i < b.N; i++ { tc.Set("foo", "bar", DefaultExpiration) @@ -1521,7 +1523,7 @@ func BenchmarkRWMutexMapSet(b *testing.B) { func BenchmarkCacheSetDelete(b *testing.B) { b.StopTimer() - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) b.StartTimer() for i := 0; i < b.N; i++ { tc.Set("foo", "bar", DefaultExpiration) @@ -1546,7 +1548,7 @@ func BenchmarkRWMutexMapSetDelete(b *testing.B) { func BenchmarkCacheSetDeleteSingleLock(b *testing.B) { b.StopTimer() - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, cacheCapacity) b.StartTimer() for i := 0; i < b.N; i++ { tc.Lock() diff --git a/size.go b/size.go new file mode 100644 index 0000000..8283827 --- /dev/null +++ b/size.go @@ -0,0 +1,75 @@ +package cache + +import ( + "reflect" +) + +// Size returns how many bytes Write would generate to encode the value v, which +// must be a fixed-size value or a slice of fixed-size values, or a pointer to such data. +// If v is neither of these, Size returns -1. +func size(v interface{}) int { + return dataSize(reflect.Indirect(reflect.ValueOf(v))) +} + +// dataSize returns the number of bytes the actual data represented by v occupies in memory. +// For compound structures, it sums the sizes of the elements. Thus, for instance, for a slice +// it returns the length of the slice times the element size and does not count the memory +// occupied by the header. If the type of v is not acceptable, dataSize returns -1. +func dataSize(v reflect.Value) int { + if v.Kind() == reflect.Slice { + sum := 0 + for i := 0; i < v.Len(); i++ { + s := sizeof(v.Index(i), v.Type().Elem()) + if s < 0 { + return -1 + } + sum += s + } + return sum + } + return sizeof(v, v.Type()) +} + +// sizeof returns the size >= 0 of variables for the given type or -1 if the type is not acceptable. +func sizeof(v reflect.Value, t reflect.Type) int { + switch t.Kind() { + case reflect.Array: + if s := sizeof(v, t.Elem()); s >= 0 { + return s * t.Len() + } + + case reflect.Struct: + sum := 0 + for i, n := 0, t.NumField(); i < n; i++ { + s := dataSize(v.Field(i)) + if s < 0 { + return -1 + } + sum += s + } + return sum + + case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint, + reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int, + reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128, + reflect.Uintptr, reflect.Ptr: + return int(t.Size()) + + case reflect.String: + return len(v.String()) + + case reflect.Map: + sum := 0 + for _, mapKey := range v.MapKeys() { + keySize := dataSize(mapKey) + valueSize := dataSize(v.MapIndex(mapKey)) + if keySize < 0 || valueSize < 0 { + return -1 + } + sum += keySize + valueSize + } + return sum + } + + return -1 +} diff --git a/size_test.go b/size_test.go new file mode 100644 index 0000000..4868511 --- /dev/null +++ b/size_test.go @@ -0,0 +1,27 @@ +package cache + +import ( + "net/http" + "testing" +) + +func TestSize(t *testing.T) { + type testType struct { + status int + header http.Header + str string + data []byte + numbers []int + } + + testObj := testType{ + header: http.Header{ + "Use-Agent": []string{"abc", "de", "d"}, + }, + str: "a", + data: []byte{'a'}, + numbers: []int{2, 3}, + } + s := size(testObj) + t.Log("size:", s) +}