renamed MaxItems to MaxSize, updated readme
This commit is contained in:
2
cache.go
2
cache.go
@@ -145,7 +145,7 @@ func (c *Cache) worker() {
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case item := <-c.promotables:
|
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()
|
c.gc()
|
||||||
}
|
}
|
||||||
case item := <-c.deletables:
|
case item := <-c.deletables:
|
||||||
|
@@ -64,7 +64,7 @@ func (_ CacheTests) TrackerDoesNotCleanupHeldInstance() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (_ CacheTests) RemovesOldestItemWhenFull() {
|
func (_ CacheTests) RemovesOldestItemWhenFull() {
|
||||||
cache := New(Configure().MaxItems(5).ItemsToPrune(1))
|
cache := New(Configure().MaxSize(5).ItemsToPrune(1))
|
||||||
for i := 0; i < 7; i++ {
|
for i := 0; i < 7; i++ {
|
||||||
cache.Set(strconv.Itoa(i), i, time.Minute)
|
cache.Set(strconv.Itoa(i), i, time.Minute)
|
||||||
}
|
}
|
||||||
@@ -75,7 +75,7 @@ func (_ CacheTests) RemovesOldestItemWhenFull() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (_ CacheTests) RemovesOldestItemWhenFullBySizer() {
|
func (_ CacheTests) RemovesOldestItemWhenFullBySizer() {
|
||||||
cache := New(Configure().MaxItems(9).ItemsToPrune(2))
|
cache := New(Configure().MaxSize(9).ItemsToPrune(2))
|
||||||
for i := 0; i < 7; i++ {
|
for i := 0; i < 7; i++ {
|
||||||
cache.Set(strconv.Itoa(i), &SizedItem{i, 2}, time.Minute)
|
cache.Set(strconv.Itoa(i), &SizedItem{i, 2}, time.Minute)
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
package ccache
|
package ccache
|
||||||
|
|
||||||
type Configuration struct {
|
type Configuration struct {
|
||||||
maxItems int64
|
maxSize int64
|
||||||
buckets int
|
buckets int
|
||||||
itemsToPrune int
|
itemsToPrune int
|
||||||
deleteBuffer int
|
deleteBuffer int
|
||||||
@@ -12,7 +12,7 @@ type Configuration struct {
|
|||||||
|
|
||||||
// Creates a configuration object with sensible defaults
|
// Creates a configuration object with sensible defaults
|
||||||
// Use this as the start of the fluent configuration:
|
// 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 {
|
func Configure() *Configuration {
|
||||||
return &Configuration{
|
return &Configuration{
|
||||||
buckets: 16,
|
buckets: 16,
|
||||||
@@ -20,15 +20,15 @@ func Configure() *Configuration {
|
|||||||
deleteBuffer: 1024,
|
deleteBuffer: 1024,
|
||||||
getsPerPromote: 3,
|
getsPerPromote: 3,
|
||||||
promoteBuffer: 1024,
|
promoteBuffer: 1024,
|
||||||
maxItems: 5000,
|
maxSize: 5000,
|
||||||
tracking: false,
|
tracking: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// The max number of items to store in the cache
|
// The max size for the cache
|
||||||
// [5000]
|
// [5000]
|
||||||
func (c *Configuration) MaxItems(max int64) *Configuration {
|
func (c *Configuration) MaxSize(max int64) *Configuration {
|
||||||
c.maxItems = max
|
c.maxSize = max
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -156,7 +156,7 @@ func (c *LayeredCache) worker() {
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case item := <-c.promotables:
|
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()
|
c.gc()
|
||||||
}
|
}
|
||||||
case item := <-c.deletables:
|
case item := <-c.deletables:
|
||||||
|
@@ -124,7 +124,7 @@ func (_ LayeredCacheTests) TrackerDoesNotCleanupHeldInstance() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (_ LayeredCacheTests) RemovesOldestItemWhenFull() {
|
func (_ LayeredCacheTests) RemovesOldestItemWhenFull() {
|
||||||
cache := Layered(Configure().MaxItems(5).ItemsToPrune(1))
|
cache := Layered(Configure().MaxSize(5).ItemsToPrune(1))
|
||||||
cache.Set("xx", "a", 23, time.Minute)
|
cache.Set("xx", "a", 23, time.Minute)
|
||||||
for i := 0; i < 7; i++ {
|
for i := 0; i < 7; i++ {
|
||||||
cache.Set(strconv.Itoa(i), "a", i, time.Minute)
|
cache.Set(strconv.Itoa(i), "a", i, time.Minute)
|
||||||
@@ -144,7 +144,7 @@ func newLayered() *LayeredCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (_ LayeredCacheTests) RemovesOldestItemWhenFullBySizer() {
|
func (_ LayeredCacheTests) RemovesOldestItemWhenFullBySizer() {
|
||||||
cache := Layered(Configure().MaxItems(9).ItemsToPrune(2))
|
cache := Layered(Configure().MaxSize(9).ItemsToPrune(2))
|
||||||
for i := 0; i < 7; i++ {
|
for i := 0; i < 7; i++ {
|
||||||
cache.Set("pri", strconv.Itoa(i), &SizedItem{i, 2}, time.Minute)
|
cache.Set("pri", strconv.Itoa(i), &SizedItem{i, 2}, time.Minute)
|
||||||
}
|
}
|
||||||
|
10
readme.md
10
readme.md
@@ -28,14 +28,14 @@ var cache = ccache.New(ccache.Configure())
|
|||||||
`Configure` exposes a chainable API:
|
`Configure` exposes a chainable API:
|
||||||
|
|
||||||
```go
|
```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:
|
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)
|
* `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:
|
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
|
// OR
|
||||||
cache.DeleteAll("/users/goku")
|
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.
|
||||||
|
Reference in New Issue
Block a user