diff --git a/cache.go b/cache.go index f4b1740..5561af8 100644 --- a/cache.go +++ b/cache.go @@ -145,7 +145,7 @@ func (c *Cache) worker() { for { select { case item := <-c.promotables: - if c.doPromote(item) && atomic.LoadInt64(&c.size) > c.maxItems { + if c.doPromote(item) && atomic.LoadInt64(&c.size) > c.maxSize { c.gc() } case item := <-c.deletables: diff --git a/cache_test.go b/cache_test.go index edf1ddc..088bff2 100644 --- a/cache_test.go +++ b/cache_test.go @@ -64,7 +64,7 @@ func (_ CacheTests) TrackerDoesNotCleanupHeldInstance() { } func (_ CacheTests) RemovesOldestItemWhenFull() { - cache := New(Configure().MaxItems(5).ItemsToPrune(1)) + cache := New(Configure().MaxSize(5).ItemsToPrune(1)) for i := 0; i < 7; i++ { cache.Set(strconv.Itoa(i), i, time.Minute) } @@ -75,7 +75,7 @@ func (_ CacheTests) RemovesOldestItemWhenFull() { } func (_ CacheTests) RemovesOldestItemWhenFullBySizer() { - cache := New(Configure().MaxItems(9).ItemsToPrune(2)) + cache := New(Configure().MaxSize(9).ItemsToPrune(2)) for i := 0; i < 7; i++ { cache.Set(strconv.Itoa(i), &SizedItem{i, 2}, time.Minute) } diff --git a/configuration.go b/configuration.go index 736c362..daa8357 100644 --- a/configuration.go +++ b/configuration.go @@ -1,7 +1,7 @@ package ccache type Configuration struct { - maxItems int64 + maxSize int64 buckets int itemsToPrune int deleteBuffer int @@ -12,7 +12,7 @@ type Configuration struct { // Creates a configuration object with sensible defaults // Use this as the start of the fluent configuration: -// e.g.: ccache.New(ccache.Configure().MaxItems(10000)) +// e.g.: ccache.New(ccache.Configure().MaxSize(10000)) func Configure() *Configuration { return &Configuration{ buckets: 16, @@ -20,15 +20,15 @@ func Configure() *Configuration { deleteBuffer: 1024, getsPerPromote: 3, promoteBuffer: 1024, - maxItems: 5000, + maxSize: 5000, tracking: false, } } -// The max number of items to store in the cache +// The max size for the cache // [5000] -func (c *Configuration) MaxItems(max int64) *Configuration { - c.maxItems = max +func (c *Configuration) MaxSize(max int64) *Configuration { + c.maxSize = max return c } diff --git a/layeredcache.go b/layeredcache.go index 8aa4690..77d817c 100644 --- a/layeredcache.go +++ b/layeredcache.go @@ -156,7 +156,7 @@ func (c *LayeredCache) worker() { for { select { case item := <-c.promotables: - if c.doPromote(item) && atomic.LoadInt64(&c.size) > c.maxItems { + if c.doPromote(item) && atomic.LoadInt64(&c.size) > c.maxSize { c.gc() } case item := <-c.deletables: diff --git a/layeredcache_test.go b/layeredcache_test.go index 45ab90f..fde1439 100644 --- a/layeredcache_test.go +++ b/layeredcache_test.go @@ -124,7 +124,7 @@ func (_ LayeredCacheTests) TrackerDoesNotCleanupHeldInstance() { } func (_ LayeredCacheTests) RemovesOldestItemWhenFull() { - cache := Layered(Configure().MaxItems(5).ItemsToPrune(1)) + cache := Layered(Configure().MaxSize(5).ItemsToPrune(1)) cache.Set("xx", "a", 23, time.Minute) for i := 0; i < 7; i++ { cache.Set(strconv.Itoa(i), "a", i, time.Minute) @@ -144,7 +144,7 @@ func newLayered() *LayeredCache { } func (_ LayeredCacheTests) RemovesOldestItemWhenFullBySizer() { - cache := Layered(Configure().MaxItems(9).ItemsToPrune(2)) + cache := Layered(Configure().MaxSize(9).ItemsToPrune(2)) for i := 0; i < 7; i++ { cache.Set("pri", strconv.Itoa(i), &SizedItem{i, 2}, time.Minute) } diff --git a/readme.md b/readme.md index 2223f06..cbc0837 100644 --- a/readme.md +++ b/readme.md @@ -28,14 +28,14 @@ var cache = ccache.New(ccache.Configure()) `Configure` exposes a chainable API: ```go -var cache = ccache.New(ccache.Configure().MaxItems(1000).itemsToPrune(100)) +var cache = ccache.New(ccache.Configure().MaxSize(1000).itemsToPrune(100)) ``` The most likely configuration options to tweak are: -* `MaxItems(int)` - the maximum number of items to store in the cache (default: 5000) +* `MaxSize(int)` - the maximum number size to store in the cache (default: 5000) * `GetsPerPromote(int)` - the number of times an item is fetched before we promote it. For large caches with long TTLs, it normally isn't necessary to promote an item after every fetch (default: 3) -* `ItemsToPrune(int)` - the number of items to prune when we hit `MaxItems`. Freeing up more than 1 slot at a time improved performance (default: 500) +* `ItemsToPrune(int)` - the number of items to prune when we hit `MaxSize`. Freeing up more than 1 slot at a time improved performance (default: 500) Configurations that change the internals of the cache, which aren't as likely to need tweaking: @@ -146,3 +146,7 @@ cache.Delete("/users/goku", "type:xml") // OR cache.DeleteAll("/users/goku") ``` +## Size +By default, items added to a cache have a size of 1. This means that if you configure `MaxSize(10000)`, you'll be able to store 10000 items in the cache. + +However, if the values you set into the cache have a method `Size() int64`, this size will be used. Note that ccache has an overhead of ~350 bytes per entry, which isn't taken into account. In other words, given a filled up cache, with `MaxSize(4096000)` and items that return a `Size() int64` of 2048, we can expect to find 2000 items (4096000/2048) taking a total space of 4796000 bytes.