2014-02-28 13:10:42 +01:00
|
|
|
package ccache
|
|
|
|
|
|
|
|
import (
|
2014-10-25 02:46:18 +02:00
|
|
|
. "github.com/karlseguin/expect"
|
2014-02-28 13:10:42 +01:00
|
|
|
"strconv"
|
2014-03-23 00:52:26 +01:00
|
|
|
"testing"
|
2014-02-28 13:10:42 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-10-25 02:46:18 +02:00
|
|
|
type CacheTests struct{}
|
|
|
|
|
|
|
|
func Test_Cache(t *testing.T) {
|
|
|
|
Expectify(new(CacheTests), t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CacheTests) GCsTheOldestItems() {
|
2014-02-28 13:10:42 +01:00
|
|
|
cache := New(Configure().ItemsToPrune(10))
|
|
|
|
for i := 0; i < 500; i++ {
|
|
|
|
cache.Set(strconv.Itoa(i), i, time.Minute)
|
|
|
|
}
|
2014-10-25 02:46:18 +02:00
|
|
|
//let the items get promoted (and added to our list)
|
|
|
|
time.Sleep(time.Millisecond * 10)
|
2014-02-28 13:10:42 +01:00
|
|
|
cache.gc()
|
2014-10-25 02:46:18 +02:00
|
|
|
Expect(cache.Get("9")).To.Equal(nil)
|
|
|
|
Expect(cache.Get("10").(int)).To.Equal(10)
|
2014-02-28 13:10:42 +01:00
|
|
|
}
|
|
|
|
|
2014-10-25 02:46:18 +02:00
|
|
|
func (c *CacheTests) PromotedItemsDontGetPruned() {
|
2014-02-28 13:10:42 +01:00
|
|
|
cache := New(Configure().ItemsToPrune(10).GetsPerPromote(1))
|
|
|
|
for i := 0; i < 500; i++ {
|
|
|
|
cache.Set(strconv.Itoa(i), i, time.Minute)
|
|
|
|
}
|
2014-02-28 16:50:42 +01:00
|
|
|
time.Sleep(time.Millisecond * 10) //run the worker once to init the list
|
2014-02-28 13:10:42 +01:00
|
|
|
cache.Get("9")
|
|
|
|
time.Sleep(time.Millisecond * 10)
|
|
|
|
cache.gc()
|
2014-10-25 02:46:18 +02:00
|
|
|
Expect(cache.Get("9").(int)).To.Equal(9)
|
|
|
|
Expect(cache.Get("10")).To.Equal(nil)
|
|
|
|
Expect(cache.Get("11").(int)).To.Equal(11)
|
2014-02-28 13:10:42 +01:00
|
|
|
}
|
|
|
|
|
2014-10-25 02:46:18 +02:00
|
|
|
func (c *CacheTests) TrackerDoesNotCleanupHeldInstance() {
|
2014-02-28 13:10:42 +01:00
|
|
|
cache := New(Configure().ItemsToPrune(10).Track())
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
cache.Set(strconv.Itoa(i), i, time.Minute)
|
|
|
|
}
|
|
|
|
item := cache.TrackingGet("0")
|
|
|
|
time.Sleep(time.Millisecond * 10)
|
|
|
|
cache.gc()
|
2014-10-25 02:46:18 +02:00
|
|
|
Expect(cache.Get("0").(int)).To.Equal(0)
|
|
|
|
Expect(cache.Get("1")).To.Equal(nil)
|
2014-02-28 13:10:42 +01:00
|
|
|
item.Release()
|
|
|
|
cache.gc()
|
2014-10-25 02:46:18 +02:00
|
|
|
Expect(cache.Get("0")).To.Equal(nil)
|
2014-02-28 13:10:42 +01:00
|
|
|
}
|
This is a sad commit.
How do you decide you need to purge your cache? Relying on runtime.ReadMemStats
sucks for two reasons. First, it's a stop-the-world call, which is pretty bad
in general and down right stupid for a supposedly concurrent-focused package.
Secondly, it only tells you the total memory usage, but most time you really
want to limit the amount of memory the cache itself uses.
Since there's no great way to determine the size of an object, that means users
need to supply the size. One way is to make it so that any cached item satisfies
a simple interface which exposes a Size() method. With this, we can track how
much memory is set put and a delete releases. But it's hard for consumers to
know how much memory they're taking when storing complex object (the entire point
of an in-process cache is to avoid having to serialize the data). Since any Size()
is bound to be a rough guess, we can simplify the entire thing by evicting based
on # of items.
This works really bad when items vary greatly in size (an HTTP cache), but in
a lot of other cases it works great. Furthermore, even for an HTTP cache, given
enough values, it should average out in most cases.
Whatever. This improve performance and should improve the usability of the cache.
It is a pretty big breaking change though.
2014-04-08 17:36:28 +02:00
|
|
|
|
2014-10-25 02:46:18 +02:00
|
|
|
func (c *CacheTests) RemovesOldestItemWhenFull() {
|
This is a sad commit.
How do you decide you need to purge your cache? Relying on runtime.ReadMemStats
sucks for two reasons. First, it's a stop-the-world call, which is pretty bad
in general and down right stupid for a supposedly concurrent-focused package.
Secondly, it only tells you the total memory usage, but most time you really
want to limit the amount of memory the cache itself uses.
Since there's no great way to determine the size of an object, that means users
need to supply the size. One way is to make it so that any cached item satisfies
a simple interface which exposes a Size() method. With this, we can track how
much memory is set put and a delete releases. But it's hard for consumers to
know how much memory they're taking when storing complex object (the entire point
of an in-process cache is to avoid having to serialize the data). Since any Size()
is bound to be a rough guess, we can simplify the entire thing by evicting based
on # of items.
This works really bad when items vary greatly in size (an HTTP cache), but in
a lot of other cases it works great. Furthermore, even for an HTTP cache, given
enough values, it should average out in most cases.
Whatever. This improve performance and should improve the usability of the cache.
It is a pretty big breaking change though.
2014-04-08 17:36:28 +02:00
|
|
|
cache := New(Configure().MaxItems(5).ItemsToPrune(1))
|
|
|
|
for i := 0; i < 7; i++ {
|
|
|
|
cache.Set(strconv.Itoa(i), i, time.Minute)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond * 10)
|
2014-10-25 02:46:18 +02:00
|
|
|
Expect(cache.Get("0")).To.Equal(nil)
|
|
|
|
Expect(cache.Get("1")).To.Equal(nil)
|
|
|
|
Expect(cache.Get("2").(int)).To.Equal(2)
|
This is a sad commit.
How do you decide you need to purge your cache? Relying on runtime.ReadMemStats
sucks for two reasons. First, it's a stop-the-world call, which is pretty bad
in general and down right stupid for a supposedly concurrent-focused package.
Secondly, it only tells you the total memory usage, but most time you really
want to limit the amount of memory the cache itself uses.
Since there's no great way to determine the size of an object, that means users
need to supply the size. One way is to make it so that any cached item satisfies
a simple interface which exposes a Size() method. With this, we can track how
much memory is set put and a delete releases. But it's hard for consumers to
know how much memory they're taking when storing complex object (the entire point
of an in-process cache is to avoid having to serialize the data). Since any Size()
is bound to be a rough guess, we can simplify the entire thing by evicting based
on # of items.
This works really bad when items vary greatly in size (an HTTP cache), but in
a lot of other cases it works great. Furthermore, even for an HTTP cache, given
enough values, it should average out in most cases.
Whatever. This improve performance and should improve the usability of the cache.
It is a pretty big breaking change though.
2014-04-08 17:36:28 +02:00
|
|
|
}
|