From 987311e3b42050193306fa7d48c618bc461ccea8 Mon Sep 17 00:00:00 2001 From: Peng Gao Date: Sat, 27 Aug 2016 18:05:46 +0800 Subject: [PATCH] Refactor code 1. Add benchmark performance in README.md 2. Use an Attr as argument for New to initiate a cache, in this way, more default settings can be supported. 3. Add fast way for delete method, if no onEvicted callback function set. 4. Use bool to indicate k existence in Get method to avoid pointer escape (which leads to memory allocation on heap), reference link below to get detail information. https://groups.google.com/forum/#!topic/golang-nuts/1WCWAtOsyDU Signed-off-by: Peng Gao --- README.md | 28 ++++ cache.go | 146 +++++++++++--------- cache_test.go | 190 ++++++++++++++++++-------- cachemap/cache.tmpl | 324 ++++++++++++++++++++++++++++++++++++++++++++ cachemap/main.go | 318 ++----------------------------------------- sharded.go | 8 +- sharded_test.go | 9 +- valtyp.go | 2 +- 8 files changed, 591 insertions(+), 434 deletions(-) create mode 100644 cachemap/cache.tmpl diff --git a/README.md b/README.md index 168ff7b..bd47807 100644 --- a/README.md +++ b/README.md @@ -105,3 +105,31 @@ one) to recover from downtime quickly. (See the docs for `NewFrom()` for caveats ### Reference `godoc` or [http://godoc.org/github.com/patrickmn/go-cache](http://godoc.org/github.com/patrickmn/go-cache) + +### Benchmark + +| benchmark\package | go-cache | cachemap | +|-----------------------------------------------------|-----------------------|----------------------| +| BenchmarkCacheGetExpiring-v | 30000000,46.3 ns/op | 20000000,43.4 ns/op | +| BenchmarkCacheGetNotExpiring-v | 50000000,29.6 ns/op | 50000000,29.6 ns/op | +| BenchmarkRWMutexMapGet-x | 50000000,26.7 ns/op | 50000000,26.6 ns/op | +| BenchmarkRWMutexInterfaceMapGetStruct-x | 20000000,75.1 ns/op | 20000000,66.1 ns/op | +| BenchmarkRWMutexInterfaceMapGetString-x | 20000000,75.3 ns/op | 20000000,67.6 ns/op | +| BenchmarkCacheGetConcurrentExpiring-v | 20000000,67.8 ns/op | 20000000,68.9 ns/op | +| BenchmarkCacheGetConcurrentNotExpiring-v | 20000000,69.2 ns/op | 20000000,68.6 ns/op | +| BenchmarkRWMutexMapGetConcurrent-x | 30000000,57.4 ns/op | 20000000,64.7 ns/op | +| BenchmarkCacheGetManyConcurrentExpiring-v | 100000000,68.0 ns/op | 100000000,66.7 ns/op | +| BenchmarkCacheGetManyConcurrentNotExpiring-v | 2000000000,68.3 ns/op | 20000000,69.3 ns/op | +| BenchmarkCacheSetExpiring-4 | 10000000,173 ns/op | 20000000,91.4 ns/op | +| BenchmarkCacheSetNotExpiring-4 | 10000000,123 ns/op | 20000000,100 ns/op | +| BenchmarkRWMutexMapSet-4 | 20000000,88.5 ns/op | 20000000,74.5 ns/op | +| BenchmarkCacheSetDelete-4 | 5000000,257 ns/op | 10000000,151 ns/op | +| BenchmarkRWMutexMapSetDelete-4 | 10000000,180 ns/op | 10000000,154 ns/op | +| BenchmarkCacheSetDeleteSingleLock-4 | 10000000,211 ns/op | 20000000,118 ns/op | +| BenchmarkRWMutexMapSetDeleteSingleLock-4 | 10000000,142 ns/op | 20000000,118 ns/op | +| BenchmarkIncrementInt-4 | 10000000,167 ns/op | | +| BenchmarkDeleteExpiredLoop-4 | 500,2584384 ns/op | 1000,2173019 ns/op | +| BenchmarkShardedCacheGetExpiring-4 | 20000000,79.5 ns/op | 20000000,67.9 ns/op | +| BenchmarkShardedCacheGetNotExpiring-4 | 30000000,59.3 ns/op | 20000000,49.9 ns/op | +| BenchmarkShardedCacheGetManyConcurrentExpiring-4 | 2000000000,52.4 ns/op | 10000000,75.8 ns/op | +| BenchmarkShardedCacheGetManyConcurrentNotExpiring-4 | 100000000,68.2 ns/op | 20000000,75.8 ns/op | diff --git a/cache.go b/cache.go index cc180ae..134f9c7 100644 --- a/cache.go +++ b/cache.go @@ -1,5 +1,7 @@ package cache +// The package is used as a template, don't use it directly! + import ( "fmt" "runtime" @@ -7,29 +9,41 @@ import ( "time" ) +// Attr is cachmap attribute +type Attr_tpl struct { + // An (optional) function that is called with the key and value when an + // item is evicted from the cache. (Including when it is deleted manually, but + // not when it is overwritten.) Set to nil to disable. + OnEvicted func(k string, v ValueType_tpl) + + DefaultCleanupInterval time.Duration // default clean interval + DefaultExpiration time.Duration // default expiration duration + Size int64 // initial size of map +} + +// Item struct type Item struct { - Object ValueType + Object ValueType_tpl Expiration int64 } -// Returns true if the item has expired. +// Expired Returns true if the item has expired, if valid Expiration is set. func (item Item) Expired() bool { - if item.Expiration == 0 { - return false - } - return time.Now().UnixNano() > item.Expiration + return item.Expiration != 0 && time.Now().UnixNano() > item.Expiration } const ( - // For use with functions that take no expiration time. + // NoExpiration is for use with functions that take no expiration time. NoExpiration time.Duration = -1 - // For use with functions that take an expiration time. Equivalent to - // passing in the same expiration duration as was given to New() or - // NewFrom() when the cache was created (e.g. 5 minutes.) + // DefaultExpiration is for use with functions that take an + // expiration time. Equivalent to passing in the same expiration + // duration as was given to New() or NewFrom() when the cache was + // created (e.g. 5 minutes.) DefaultExpiration time.Duration = 0 ) -type Cache struct { +// Cache struct +type Cache_tpl struct { *cache // If this is confusing, see the comment at the bottom of New() } @@ -38,14 +52,14 @@ type cache struct { defaultExpiration time.Duration items map[string]Item mu sync.RWMutex - onEvicted func(string, *ValueType) + onEvicted func(string, ValueType_tpl) janitor *janitor } // Add an item to the cache, replacing any existing item. If the duration is 0 // (DefaultExpiration), the cache's default expiration time is used. If it is -1 // (NoExpiration), the item never expires. -func (c *cache) Set(k string, x ValueType, d time.Duration) { +func (c *cache) Set(k string, x ValueType_tpl, d time.Duration) { // "Inlining" of set var e int64 if d == DefaultExpiration { @@ -64,7 +78,7 @@ func (c *cache) Set(k string, x ValueType, d time.Duration) { c.mu.Unlock() } -func (c *cache) set(k string, x ValueType, d time.Duration) { +func (c *cache) set(k string, x ValueType_tpl, d time.Duration) { var e int64 if d == DefaultExpiration { d = c.defaultExpiration @@ -80,7 +94,7 @@ func (c *cache) set(k string, x ValueType, d time.Duration) { // Add an item to the cache only if an item doesn't already exist for the given // key, or if the existing item has expired. Returns an error otherwise. -func (c *cache) Add(k string, x ValueType, d time.Duration) error { +func (c *cache) Add(k string, x ValueType_tpl, d time.Duration) error { c.mu.Lock() _, found := c.get(k) if found { @@ -94,7 +108,7 @@ func (c *cache) Add(k string, x ValueType, d time.Duration) error { // Set a new value for the cache key only if it already exists, and the existing // item hasn't expired. Returns an error otherwise. -func (c *cache) Replace(k string, x ValueType, d time.Duration) error { +func (c *cache) Replace(k string, x ValueType_tpl, d time.Duration) error { c.mu.Lock() _, found := c.get(k) if !found { @@ -108,35 +122,24 @@ func (c *cache) Replace(k string, x ValueType, d time.Duration) error { // Get an item from the cache. Returns the item or nil, and a bool indicating // whether the key was found. -func (c *cache) Get(k string) *ValueType { +func (c *cache) Get(k string) (ValueType_tpl, bool) { c.mu.RLock() // "Inlining" of get and Expired item, found := c.items[k] - if !found { + // TODO: inline time.Now implementation + if !found || item.Expiration > 0 && time.Now().UnixNano() > item.Expiration { c.mu.RUnlock() - return nil - } - if item.Expiration > 0 { - if time.Now().UnixNano() > item.Expiration { - c.mu.RUnlock() - return nil - } + return ValueType_tpl(0), false } c.mu.RUnlock() - return &item.Object + return item.Object, true } -func (c *cache) get(k string) (*ValueType, bool) { +func (c *cache) get(k string) (*ValueType_tpl, bool) { item, found := c.items[k] - if !found { + if !found || item.Expiration > 0 && time.Now().UnixNano() > item.Expiration { return nil, false } - // "Inlining" of Expired - if item.Expiration > 0 { - if time.Now().UnixNano() > item.Expiration { - return nil, false - } - } return &item.Object, true } @@ -164,6 +167,14 @@ func (c *cache) Decrement(k string, n int64) error { // Delete an item from the cache. Does nothing if the key is not in the cache. func (c *cache) Delete(k string) { + // fast path + if c.onEvicted == nil { + c.mu.Lock() + c.deleteFast(k) + c.mu.Unlock() + return + } + // slow path c.mu.Lock() v, evicted := c.delete(k) c.mu.Unlock() @@ -172,26 +183,41 @@ func (c *cache) Delete(k string) { } } -func (c *cache) delete(k string) (*ValueType, bool) { - if c.onEvicted != nil { - if v, found := c.items[k]; found { - delete(c.items, k) - return &v.Object, true - } +func (c *cache) delete(k string) (ValueType_tpl, bool) { + if v, found := c.items[k]; found { + delete(c.items, k) + return v.Object, true } + //TODO: zeroValue + return 0, false +} + +func (c *cache) deleteFast(k string) { delete(c.items, k) - return nil, false } type keyAndValue struct { key string - value *ValueType + value ValueType_tpl } // Delete all expired items from the cache. func (c *cache) DeleteExpired() { var evictedItems []keyAndValue now := time.Now().UnixNano() + // fast path + if c.onEvicted == nil { + c.mu.Lock() + for k, v := range c.items { + // "Inlining" of expired + if v.Expiration > 0 && now > v.Expiration { + c.deleteFast(k) + } + } + c.mu.Unlock() + return + } + // slow path c.mu.Lock() for k, v := range c.items { // "Inlining" of expired @@ -208,15 +234,6 @@ func (c *cache) DeleteExpired() { } } -// Sets an (optional) function that is called with the key and value when an -// item is evicted from the cache. (Including when it is deleted manually, but -// not when it is overwritten.) Set to nil to disable. -func (c *cache) OnEvicted(f func(string, *ValueType)) { - c.mu.Lock() - c.onEvicted = f - c.mu.Unlock() -} - // Returns the number of items in the cache. This may include items that have // expired, but have not yet been cleaned up. Equivalent to len(c.Items()). func (c *cache) ItemCount() int { @@ -252,7 +269,7 @@ func (j *janitor) Run(c *cache) { } } -func stopJanitor(c *Cache) { +func stopJanitor(c *Cache_tpl) { c.janitor.stop <- true } @@ -275,30 +292,29 @@ func newCache(de time.Duration, m map[string]Item) *cache { return c } -func newCacheWithJanitor(de time.Duration, ci time.Duration, m map[string]Item) *Cache { +func newCacheWithJanitor(de time.Duration, ci time.Duration, m map[string]Item, onEvicted func(k string, v ValueType_tpl)) *Cache_tpl { c := newCache(de, m) + c.onEvicted = onEvicted // 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 // garbage collected, the finalizer stops the janitor goroutine, after // which c can be collected. - C := &Cache{c} + C := &Cache_tpl{c} if ci > 0 { runJanitor(c, ci) - // 如果C被回收了,但是c不会,因为stopJanitor是一个一直运行的 - // goroutine对c一直有引用不会被回收,所以加一个Finalizer来停掉 - // 这个goroutine然后让c被回收. runtime.SetFinalizer(C, stopJanitor) } return C } -// Return a new cache with a given default expiration duration and cleanup -// interval. If the expiration duration is less than one (or NoExpiration), -// 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 { - items := make(map[string]Item) - return newCacheWithJanitor(defaultExpiration, cleanupInterval, items) +// New Returns a new cache with a given default expiration duration and +// cleanup interval. If the expiration duration is less than one +// (or NoExpiration), 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_tpl(attr Attr_tpl) *Cache_tpl { + items := make(map[string]Item, attr.Size) + return newCacheWithJanitor(attr.DefaultExpiration, attr.DefaultCleanupInterval, items, attr.OnEvicted) } diff --git a/cache_test.go b/cache_test.go index b0e077b..c1a4d80 100644 --- a/cache_test.go +++ b/cache_test.go @@ -14,67 +14,98 @@ type TestStruct struct { } func TestCache(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New_tpl(Attr_tpl{ + DefaultExpiration: DefaultExpiration, + DefaultCleanupInterval: 0, + }) - a := tc.Get("a") - if a != nil { + a, found := tc.Get("a") + if found || a != 0 { t.Error("Getting A found value that shouldn't exist:", a) } - b := tc.Get("b") - if b != nil { + b, found := tc.Get("b") + if found || b != 0 { t.Error("Getting B found value that shouldn't exist:", b) } - c := tc.Get("c") - if c != nil { + c, found := tc.Get("c") + if found || c != 0 { t.Error("Getting C found value that shouldn't exist:", c) } - tc.Set("a", ValueType(1), DefaultExpiration) + tc.Set("a", 1, DefaultExpiration) + tc.Set("b", 2, DefaultExpiration) + tc.Set("c", 3, DefaultExpiration) - x := tc.Get("a") - if x == nil { + x, found := tc.Get("a") + if !found { + t.Error("a was not found while getting a2") + } + if x == 0 { t.Error("x for a is nil") - } else if a2 := int(*x); a2+2 != 3 { + } else if a2 := x; a2+2 != 3 { t.Error("a2 (which should be 1) plus 2 does not equal 3; value:", a2) } + x, found = tc.Get("b") + if !found { + t.Error("b was not found while getting b2") + } + if x == 0 { + t.Error("x for b is nil") + } else if b2 := x; b2+2 != 4 { + t.Error("b2 (which should be 2) plus 2 does not equal 4; value:", b2) + } + + x, found = tc.Get("c") + if !found { + t.Error("c was not found while getting c2") + } + if x == 0 { + t.Error("x for c is nil") + } else if c2 := x; c2+1 != 4 { + t.Error("c2 (which should be 3) plus 1 does not equal 4; value:", c2) + } + } func TestCacheTimes(t *testing.T) { - var x *ValueType + var found bool - tc := New(50*time.Millisecond, 1*time.Millisecond) + tc := New_tpl(Attr_tpl{ + DefaultExpiration: 50 * time.Millisecond, + DefaultCleanupInterval: 1 * time.Millisecond, + }) tc.Set("a", 1, DefaultExpiration) tc.Set("b", 2, NoExpiration) tc.Set("c", 3, 20*time.Millisecond) tc.Set("d", 4, 70*time.Millisecond) <-time.After(25 * time.Millisecond) - x = tc.Get("c") - if x != nil { - t.Error("Found c when it should have been automatically deleted", *x) + _, found = tc.Get("c") + if found { + t.Error("Found c when it should have been automatically deleted") } <-time.After(30 * time.Millisecond) - x = tc.Get("a") - if x != nil { - t.Error("Found a when it should have been automatically deleted", *x) + _, found = tc.Get("a") + if found { + t.Error("Found a when it should have been automatically deleted") } - x = tc.Get("b") - if x == nil { + _, found = tc.Get("b") + if !found { t.Error("Did not find b even though it was set to never expire") } - x = tc.Get("d") - if x == nil { + _, found = tc.Get("d") + if !found { t.Error("Did not find d even though it was set to expire later than the default") } <-time.After(20 * time.Millisecond) - x = tc.Get("d") - if x != nil { + _, found = tc.Get("d") + if found { t.Error("Found d when it should have been automatically deleted (later than the default)") } } @@ -84,7 +115,10 @@ func TestIncrementWithInt(t *testing.T) { } func TestAdd(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New_tpl(Attr_tpl{ + DefaultExpiration: DefaultExpiration, + DefaultCleanupInterval: 0, + }) err := tc.Add("foo", 1, DefaultExpiration) if err != nil { t.Error("Couldn't add foo even though it shouldn't exist") @@ -96,7 +130,10 @@ func TestAdd(t *testing.T) { } func TestReplace(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New_tpl(Attr_tpl{ + DefaultExpiration: DefaultExpiration, + DefaultCleanupInterval: -1, + }) err := tc.Replace("foo", 1, DefaultExpiration) if err == nil { t.Error("Replaced foo when it shouldn't exist") @@ -107,19 +144,27 @@ func TestReplace(t *testing.T) { t.Error("Couldn't replace existing key foo") } } - func TestDelete(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New_tpl(Attr_tpl{ + DefaultExpiration: DefaultExpiration, + DefaultCleanupInterval: 0, + }) tc.Set("foo", 1, DefaultExpiration) tc.Delete("foo") - x := tc.Get("foo") - if x != nil { + x, found := tc.Get("foo") + if found { + t.Error("foo was found, but it should have been deleted") + } + if x != 0 { t.Error("x is not nil:", x) } } func TestItemCount(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New_tpl(Attr_tpl{ + DefaultExpiration: DefaultExpiration, + DefaultCleanupInterval: 0, + }) tc.Set("foo", 1, DefaultExpiration) tc.Set("bar", 2, DefaultExpiration) tc.Set("baz", 3, DefaultExpiration) @@ -129,39 +174,51 @@ func TestItemCount(t *testing.T) { } func TestFlush(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New_tpl(Attr_tpl{ + DefaultExpiration: DefaultExpiration, + DefaultCleanupInterval: -1, + }) tc.Set("foo", 1, DefaultExpiration) tc.Set("baz", 2, DefaultExpiration) tc.Flush() - x := tc.Get("foo") - if x != nil { + x, found := tc.Get("foo") + if found { + t.Error("foo was found, but it should have been deleted") + } + if x != 0 { t.Error("x is not nil:", x) } - x = tc.Get("baz") - if x != nil { + x, found = tc.Get("baz") + if found { + t.Error("baz was found, but it should have been deleted") + } + if x != 0 { t.Error("x is not nil:", x) } } func TestOnEvicted(t *testing.T) { - tc := New(DefaultExpiration, 0) - tc.Set("foo", ValueType(3), DefaultExpiration) + tc := New_tpl(Attr_tpl{ + DefaultExpiration: DefaultExpiration, + DefaultCleanupInterval: 0, + }) + tc.Set("foo", 3, DefaultExpiration) if tc.onEvicted != nil { t.Fatal("tc.onEvicted is not nil") } works := false - tc.OnEvicted(func(k string, v *ValueType) { - if k == "foo" && int(*v) == 3 { + tc.OnEvicted(func(k string, v ValueType_tpl) { + if k == "foo" && v == 3 { works = true } - tc.Set("bar", ValueType(4), DefaultExpiration) + tc.Set("bar", 4, DefaultExpiration) }) tc.Delete("foo") - x := tc.Get("bar") + x, _ := tc.Get("bar") if !works { t.Error("works bool not true") } - if int(*x) != 4 { + if x != 4 { t.Error("bar was not 4") } } @@ -176,7 +233,10 @@ func BenchmarkCacheGetNotExpiring(b *testing.B) { func benchmarkCacheGet(b *testing.B, exp time.Duration) { b.StopTimer() - tc := New(exp, 0) + tc := New_tpl(Attr_tpl{ + DefaultExpiration: exp, + DefaultCleanupInterval: 0, + }) tc.Set("foo", 1, DefaultExpiration) b.StartTimer() for i := 0; i < b.N; i++ { @@ -237,7 +297,10 @@ func BenchmarkCacheGetConcurrentNotExpiring(b *testing.B) { func benchmarkCacheGetConcurrent(b *testing.B, exp time.Duration) { b.StopTimer() - tc := New(exp, 0) + tc := New_tpl(Attr_tpl{ + DefaultExpiration: exp, + DefaultCleanupInterval: 0, + }) tc.Set("foo", 1, DefaultExpiration) wg := new(sync.WaitGroup) workers := runtime.NumCPU() @@ -293,20 +356,24 @@ func benchmarkCacheGetManyConcurrent(b *testing.B, exp time.Duration) { // in sharded_test.go. b.StopTimer() n := 10000 - tc := New(exp, 0) + tc := New_tpl(Attr_tpl{ + DefaultExpiration: exp, + DefaultCleanupInterval: 0, + }) keys := make([]string, n) for i := 0; i < n; i++ { k := "foo" + strconv.Itoa(i) keys[i] = k - tc.Set(k, ValueType(1), DefaultExpiration) + tc.Set(k, ValueType_tpl(1), DefaultExpiration) } each := b.N / n wg := new(sync.WaitGroup) wg.Add(n) for _, v := range keys { + x := v go func() { for j := 0; j < each; j++ { - tc.Get(v) + tc.Get(x) } wg.Done() }() @@ -325,7 +392,10 @@ func BenchmarkCacheSetNotExpiring(b *testing.B) { func benchmarkCacheSet(b *testing.B, exp time.Duration) { b.StopTimer() - tc := New(exp, 0) + tc := New_tpl(Attr_tpl{ + DefaultExpiration: exp, + DefaultCleanupInterval: 0, + }) b.StartTimer() for i := 0; i < b.N; i++ { tc.Set("foo", 1, DefaultExpiration) @@ -346,7 +416,10 @@ func BenchmarkRWMutexMapSet(b *testing.B) { func BenchmarkCacheSetDelete(b *testing.B) { b.StopTimer() - tc := New(DefaultExpiration, 0) + tc := New_tpl(Attr_tpl{ + DefaultExpiration: DefaultExpiration, + DefaultCleanupInterval: 0, + }) b.StartTimer() for i := 0; i < b.N; i++ { tc.Set("foo", 1, DefaultExpiration) @@ -371,7 +444,10 @@ func BenchmarkRWMutexMapSetDelete(b *testing.B) { func BenchmarkCacheSetDeleteSingleLock(b *testing.B) { b.StopTimer() - tc := New(DefaultExpiration, 0) + tc := New_tpl(Attr_tpl{ + DefaultExpiration: DefaultExpiration, + DefaultCleanupInterval: 0, + }) b.StartTimer() for i := 0; i < b.N; i++ { tc.mu.Lock() @@ -397,7 +473,10 @@ func BenchmarkRWMutexMapSetDeleteSingleLock(b *testing.B) { func BenchmarkIncrementInt(b *testing.B) { b.Skip() b.StopTimer() - tc := New(DefaultExpiration, 0) + tc := New_tpl(Attr_tpl{ + DefaultExpiration: DefaultExpiration, + DefaultCleanupInterval: 0, + }) tc.Set("foo", 0, DefaultExpiration) b.StartTimer() for i := 0; i < b.N; i++ { @@ -407,7 +486,10 @@ func BenchmarkIncrementInt(b *testing.B) { func BenchmarkDeleteExpiredLoop(b *testing.B) { b.StopTimer() - tc := New(5*time.Minute, 0) + tc := New_tpl(Attr_tpl{ + DefaultExpiration: 5 * time.Minute, + DefaultCleanupInterval: 0, + }) tc.mu.Lock() for i := 0; i < 100000; i++ { tc.set(strconv.Itoa(i), 1, DefaultExpiration) diff --git a/cachemap/cache.tmpl b/cachemap/cache.tmpl new file mode 100644 index 0000000..26dc373 --- /dev/null +++ b/cachemap/cache.tmpl @@ -0,0 +1,324 @@ +package {{.PackageName}} + +import ( + "fmt" + "runtime" + "sync" + "time" +) + +// Attr is cachmap attribute +type {{.ValueType}}CacheAttr struct { + OnEvicted func(k string, v {{.ValueType}}) // called when k evicted if set + DefaultCleanupInterval time.Duration // default clean interval + DefaultExpiration time.Duration // default expiration duration + Size int64 // inital size of map +} + +// Item struct +type Item struct { + Object {{.ValueType}} + Expiration int64 +} + +// Expired Returns true if the item has expired, if valid Expiration is set. +func (item Item) Expired() bool { + return item.Expiration != 0 && time.Now().UnixNano() > item.Expiration +} + +const ( + // NoExpiration is for use with functions that take no expiration time. + NoExpiration time.Duration = -1 + // DefaultExpiration is for use with functions that take an + // expiration time. Equivalent to passing in the same expiration + // duration as was given to New() or NewFrom() when the cache was + // created (e.g. 5 minutes.) + DefaultExpiration time.Duration = 0 +) + +// Cache struct +type {{.ValueType}}Cache struct { + *cache + // If this is confusing, see the comment at the bottom of New() +} + +type cache struct { + defaultExpiration time.Duration + items map[string]Item + mu sync.RWMutex + onEvicted func(string, {{.ValueType}}) + janitor *janitor +} + +// Add an item to the cache, replacing any existing item. If the duration is 0 +// (DefaultExpiration), the cache's default expiration time is used. If it is -1 +// (NoExpiration), the item never expires. +func (c *cache) Set(k string, x {{.ValueType}}, d time.Duration) { + // "Inlining" of set + var e int64 + if d == DefaultExpiration { + d = c.defaultExpiration + } + if d > 0 { + e = time.Now().Add(d).UnixNano() + } + c.mu.Lock() + c.items[k] = Item{ + Object: x, + Expiration: e, + } + // TODO: Calls to mu.Unlock are currently not deferred because defer + // adds ~200 ns (as of go1.) + c.mu.Unlock() +} + +func (c *cache) set(k string, x {{.ValueType}}, d time.Duration) { + var e int64 + if d == DefaultExpiration { + d = c.defaultExpiration + } + if d > 0 { + e = time.Now().Add(d).UnixNano() + } + c.items[k] = Item{ + Object: x, + Expiration: e, + } +} + +// Add an item to the cache only if an item doesn't already exist for the given +// key, or if the existing item has expired. Returns an error otherwise. +func (c *cache) Add(k string, x {{.ValueType}}, d time.Duration) error { + c.mu.Lock() + _, found := c.get(k) + if found { + c.mu.Unlock() + return fmt.Errorf("Item %s already exists", k) + } + c.set(k, x, d) + c.mu.Unlock() + return nil +} + +// Set a new value for the cache key only if it already exists, and the existing +// item hasn't expired. Returns an error otherwise. +func (c *cache) Replace(k string, x {{.ValueType}}, d time.Duration) error { + c.mu.Lock() + _, found := c.get(k) + if !found { + c.mu.Unlock() + return fmt.Errorf("Item %s doesn't exist", k) + } + c.set(k, x, d) + c.mu.Unlock() + return nil +} + +// Get an item from the cache. Returns the item or nil, and a bool indicating +// whether the key was found. +func (c *cache) Get(k string) ({{.ValueType}}, bool) { + c.mu.RLock() + // "Inlining" of get and Expired + item, found := c.items[k] + // TODO: inline time.Now implementation + if !found || item.Expiration > 0 && time.Now().UnixNano() > item.Expiration { + c.mu.RUnlock() + return {{.ValueType}}(0), false + } + c.mu.RUnlock() + return item.Object, true +} + +func (c *cache) get(k string) (*{{.ValueType}}, bool) { + item, found := c.items[k] + if !found || item.Expiration > 0 && time.Now().UnixNano() > item.Expiration { + return nil, false + } + return &item.Object, true +} + +// Increment an item of type int, int8, int16, int32, int64, uintptr, uint, +// uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the +// item's value is not an integer, if it was not found, or if it is not +// possible to increment it by n. To retrieve the incremented value, use one +// of the specialized methods, e.g. IncrementInt64. +// TODO: Increment for numberic type. +func (c *cache) Increment(k string, n int64) error { + return nil +} + +// Decrement an item of type int, int8, int16, int32, int64, uintptr, uint, +// uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the +// item's value is not an integer, if it was not found, or if it is not +// possible to decrement it by n. To retrieve the decremented value, use one +// of the specialized methods, e.g. DecrementInt64. +// TODO: Decrement +func (c *cache) Decrement(k string, n int64) error { + // TODO: Implement Increment and Decrement more cleanly. + // (Cannot do Increment(k, n*-1) for uints.) + return nil +} + +// Delete an item from the cache. Does nothing if the key is not in the cache. +func (c *cache) Delete(k string) { + // fast path + if c.onEvicted == nil { + c.mu.Lock() + c.deleteFast(k) + c.mu.Unlock() + return + } + // slow path + c.mu.Lock() + v, evicted := c.delete(k) + c.mu.Unlock() + if evicted { + c.onEvicted(k, v) + } +} + +func (c *cache) delete(k string) ({{.ValueType}}, bool) { + if v, found := c.items[k]; found { + delete(c.items, k) + return v.Object, true + } + //TODO: zeroValue + return 0, false +} + +func (c *cache) deleteFast(k string) { + delete(c.items, k) +} + +type keyAndValue struct { + key string + value {{.ValueType}} +} + +// Delete all expired items from the cache. +func (c *cache) DeleteExpired() { + var evictedItems []keyAndValue + now := time.Now().UnixNano() + // fast path + if c.onEvicted == nil { + c.mu.Lock() + for k, v := range c.items { + // "Inlining" of expired + if v.Expiration > 0 && now > v.Expiration { + c.deleteFast(k) + } + } + c.mu.Unlock() + return + } + // slow path + c.mu.Lock() + for k, v := range c.items { + // "Inlining" of expired + if v.Expiration > 0 && now > v.Expiration { + ov, evicted := c.delete(k) + if evicted { + evictedItems = append(evictedItems, keyAndValue{k, ov}) + } + } + } + c.mu.Unlock() + for _, v := range evictedItems { + c.onEvicted(v.key, v.value) + } +} + +// Sets an (optional) function that is called with the key and value when an +// item is evicted from the cache. (Including when it is deleted manually, but +// not when it is overwritten.) Set to nil to disable. +// 这里加锁没有意义 +func (c *cache) OnEvicted(f func(string, {{.ValueType}})) { + c.mu.Lock() + c.onEvicted = f + c.mu.Unlock() +} + +// Returns the number of items in the cache. This may include items that have +// expired, but have not yet been cleaned up. Equivalent to len(c.Items()). +func (c *cache) ItemCount() int { + c.mu.RLock() + n := len(c.items) + c.mu.RUnlock() + return n +} + +// Delete all items from the cache. +func (c *cache) Flush() { + c.mu.Lock() + c.items = map[string]Item{} + c.mu.Unlock() +} + +type janitor struct { + Interval time.Duration + stop chan bool +} + +func (j *janitor) Run(c *cache) { + j.stop = make(chan bool) + ticker := time.NewTicker(j.Interval) + for { + select { + case <-ticker.C: + c.DeleteExpired() + case <-j.stop: + ticker.Stop() + return + } + } +} + +func stopJanitor(c *{{.ValueType}}Cache) { + c.janitor.stop <- true +} + +func runJanitor(c *cache, ci time.Duration) { + j := &janitor{ + Interval: ci, + } + c.janitor = j + go j.Run(c) +} + +func newCache(de time.Duration, m map[string]Item) *cache { + if de == 0 { + de = -1 + } + c := &cache{ + defaultExpiration: de, + items: m, + } + return c +} + +func newCacheWithJanitor(de time.Duration, ci time.Duration, m map[string]Item, onEvicted func(k string, v {{.ValueType}})) *{{.ValueType}}Cache { + c := newCache(de, m) + c.onEvicted = onEvicted + // 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 + // garbage collected, the finalizer stops the janitor goroutine, after + // which c can be collected. + C := &{{.ValueType}}Cache{c} + if ci > 0 { + runJanitor(c, ci) + runtime.SetFinalizer(C, stopJanitor) + } + return C +} + +// New Returns a new cache with a given default expiration duration and +// cleanup interval. If the expiration duration is less than one +// (or NoExpiration), 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{{.ValueType}}Cache(attr {{.ValueType}}CacheAttr) *{{.ValueType}}Cache { + items := make(map[string]Item, attr.Size) + return newCacheWithJanitor(attr.DefaultExpiration, attr.DefaultCleanupInterval, items, attr.OnEvicted) +} diff --git a/cachemap/main.go b/cachemap/main.go index 362cb1c..3c2dd4e 100644 --- a/cachemap/main.go +++ b/cachemap/main.go @@ -6,319 +6,25 @@ import ( "go/parser" "go/token" "os" + "path" + "path/filepath" + "runtime" "text/template" ) -var cachemapTemplate = `// Automatically generated file; DO NOT EDIT -package {{ .PackageName }} - -import ( - "fmt" - "runtime" - "sync" - "time" -) - -type Item struct { - Object {{ .ValueType }} - Expiration int64 -} - -// Returns true if the item has expired. -func (item Item) Expired() bool { - if item.Expiration == 0 { - return false - } - return time.Now().UnixNano() > item.Expiration -} - -const ( - // For use with functions that take no expiration time. - NoExpiration time.Duration = -1 - // For use with functions that take an expiration time. Equivalent to - // passing in the same expiration duration as was given to {{ .Cache }}New(). - DefaultExpiration time.Duration = 0 -) - -type {{ .Cache }} struct { - *cache - // If this is confusing, see the comment at the bottom of {{ .Cache }}New() -} - -type cache struct { - defaultExpiration time.Duration - items map[string]Item - mu sync.RWMutex - onEvicted func(string, *{{ .ValueType }}) - janitor *janitor -} - -// Add an item to the cache, replacing any existing item. If the duration is 0 -// (DefaultExpiration), the cache's default expiration time is used. If it is -1 -// (NoExpiration), the item never expires. -func (c *cache) Set(k string, x {{ .ValueType }}, d time.Duration) { - // "Inlining" of set - var e int64 - if d == DefaultExpiration { - d = c.defaultExpiration - } - if d > 0 { - e = time.Now().Add(d).UnixNano() - } - c.mu.Lock() - c.items[k] = Item{ - Object: x, - Expiration: e, - } - // TODO: Calls to mu.Unlock are currently not deferred because defer - // adds ~200 ns (as of go1.) - c.mu.Unlock() -} - -func (c *cache) set(k string, x {{ .ValueType }}, d time.Duration) { - var e int64 - if d == DefaultExpiration { - d = c.defaultExpiration - } - if d > 0 { - e = time.Now().Add(d).UnixNano() - } - c.items[k] = Item{ - Object: x, - Expiration: e, - } -} - -// Add an item to the cache only if an item doesn't already exist for the given -// key, or if the existing item has expired. Returns an error otherwise. -func (c *cache) Add(k string, x {{ .ValueType }}, d time.Duration) error { - c.mu.Lock() - _, found := c.get(k) - if found { - c.mu.Unlock() - return fmt.Errorf("Item %s already exists", k) - } - c.set(k, x, d) - c.mu.Unlock() - return nil -} - -// Set a new value for the cache key only if it already exists, and the existing -// item hasn't expired. Returns an error otherwise. -func (c *cache) Replace(k string, x {{ .ValueType }}, d time.Duration) error { - c.mu.Lock() - _, found := c.get(k) - if !found { - c.mu.Unlock() - return fmt.Errorf("Item %s doesn't exist", k) - } - c.set(k, x, d) - c.mu.Unlock() - return nil -} - -// Get an item from the cache. Returns the item or nil, and a bool indicating -// whether the key was found. -func (c *cache) Get(k string) *{{ .ValueType }} { - c.mu.RLock() - // "Inlining" of get and Expired - item, found := c.items[k] - if !found { - c.mu.RUnlock() - return nil - } - if item.Expiration > 0 { - if time.Now().UnixNano() > item.Expiration { - c.mu.RUnlock() - return nil - } - } - c.mu.RUnlock() - return &item.Object -} - -func (c *cache) get(k string) (*{{ .ValueType }}, bool) { - item, found := c.items[k] - if !found { - return nil, false - } - // "Inlining" of Expired - if item.Expiration > 0 { - if time.Now().UnixNano() > item.Expiration { - return nil, false - } - } - return &item.Object, true -} - -// Increment an item of type int, int8, int16, int32, int64, uintptr, uint, -// uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the -// item's value is not an integer, if it was not found, or if it is not -// possible to increment it by n. To retrieve the incremented value, use one -// of the specialized methods, e.g. IncrementInt64. -// TODO: Increment for numberic type. -func (c *cache) Increment(k string, n int64) error { - return nil -} - -// Decrement an item of type int, int8, int16, int32, int64, uintptr, uint, -// uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the -// item's value is not an integer, if it was not found, or if it is not -// possible to decrement it by n. To retrieve the decremented value, use one -// of the specialized methods, e.g. DecrementInt64. -// TODO: Decrement -func (c *cache) Decrement(k string, n int64) error { - // TODO: Implement Increment and Decrement more cleanly. - // (Cannot do Increment(k, n*-1) for uints.) - return nil -} - -// Delete an item from the cache. Does nothing if the key is not in the cache. -func (c *cache) Delete(k string) { - c.mu.Lock() - v, evicted := c.delete(k) - c.mu.Unlock() - if evicted { - c.onEvicted(k, v) - } -} - -func (c *cache) delete(k string) (*{{ .ValueType }}, bool) { - if c.onEvicted != nil { - if v, found := c.items[k]; found { - delete(c.items, k) - return &v.Object, true - } - } - delete(c.items, k) - return nil, false -} - -type keyAndValue struct { - key string - value *{{ .ValueType }} -} - -// Delete all expired items from the cache. -func (c *cache) DeleteExpired() { - var evictedItems []keyAndValue - now := time.Now().UnixNano() - c.mu.Lock() - for k, v := range c.items { - // "Inlining" of expired - if v.Expiration > 0 && now > v.Expiration { - ov, evicted := c.delete(k) - if evicted { - evictedItems = append(evictedItems, keyAndValue{k, ov}) - } - } - } - c.mu.Unlock() - for _, v := range evictedItems { - c.onEvicted(v.key, v.value) - } -} - -// Sets an (optional) function that is called with the key and value when an -// item is evicted from the cache. (Including when it is deleted manually, but -// not when it is overwritten.) Set to nil to disable. -func (c *cache) OnEvicted(f func(string, *{{ .ValueType }})) { - c.mu.Lock() - c.onEvicted = f - c.mu.Unlock() -} - -// Returns the number of items in the cache. This may include items that have -// expired, but have not yet been cleaned up. Equivalent to len(c.Items()). -func (c *cache) ItemCount() int { - c.mu.RLock() - n := len(c.items) - c.mu.RUnlock() - return n -} - -// Delete all items from the cache. -func (c *cache) Flush() { - c.mu.Lock() - c.items = map[string]Item{} - c.mu.Unlock() -} - -type janitor struct { - Interval time.Duration - stop chan bool -} - -func (j *janitor) Run(c *cache) { - j.stop = make(chan bool) - ticker := time.NewTicker(j.Interval) - for { - select { - case <-ticker.C: - c.DeleteExpired() - case <-j.stop: - ticker.Stop() - return - } - } -} - -func stopJanitor(c *{{ .Cache }}) { - c.janitor.stop <- true -} - -func runJanitor(c *cache, ci time.Duration) { - j := &janitor{ - Interval: ci, - } - c.janitor = j - go j.Run(c) -} - -func newCache(de time.Duration, m map[string]Item) *cache { - if de == 0 { - de = -1 - } - c := &cache{ - defaultExpiration: de, - items: m, - } - return c -} - -func newCacheWithJanitor(de time.Duration, ci time.Duration, m map[string]Item) *{{ .Cache }} { - c := newCache(de, m) - // 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 - // garbage collected, the finalizer stops the janitor goroutine, after - // which c can be collected. - C := &{{ .Cache }}{c} - if ci > 0 { - runJanitor(c, ci) - // 如果C被回收了,但是c不会,因为stopJanitor是一个一直运行的 - // goroutine对c一直有引用不会被回收,所以加一个Finalizer来停掉 - // 这个goroutine然后让c被回收. - runtime.SetFinalizer(C, stopJanitor) - } - return C -} - -// Return a new cache with a given default expiration duration and cleanup -// interval. If the expiration duration is less than one (or NoExpiration), -// 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 {{ .Cache }}New(defaultExpiration, cleanupInterval time.Duration) *{{ .Cache }}{ - items := make(map[string]Item) - return newCacheWithJanitor(defaultExpiration, cleanupInterval, items) -}` - func fatal(v ...interface{}) { fmt.Fprintln(os.Stderr, v...) os.Exit(1) } +func packageDir() string { + _, filename, _, ok := runtime.Caller(0) + if !ok { + panic("No caller information") + } + return path.Dir(filename) +} + func main() { keyType := flag.String("k", "", "key type") valueType := flag.String("v", "", "value type") @@ -343,7 +49,7 @@ func main() { fatal(err) } defer f.Close() - tpl, err := template.New("cachemap").Parse(cachemapTemplate) + tpl, err := template.New("cache.tmpl").ParseFiles(filepath.Join(packageDir(), "cache.tmpl")) if err != nil { fatal(err) } diff --git a/sharded.go b/sharded.go index b1ab771..4d360ac 100644 --- a/sharded.go +++ b/sharded.go @@ -66,19 +66,19 @@ func (sc *shardedCache) bucket(k string) *cache { return sc.cs[djb33(sc.seed, k)%sc.m] } -func (sc *shardedCache) Set(k string, x ValueType, d time.Duration) { +func (sc *shardedCache) Set(k string, x ValueType_tpl, d time.Duration) { sc.bucket(k).Set(k, x, d) } -func (sc *shardedCache) Add(k string, x ValueType, d time.Duration) error { +func (sc *shardedCache) Add(k string, x ValueType_tpl, d time.Duration) error { return sc.bucket(k).Add(k, x, d) } -func (sc *shardedCache) Replace(k string, x ValueType, d time.Duration) error { +func (sc *shardedCache) Replace(k string, x ValueType_tpl, d time.Duration) error { return sc.bucket(k).Replace(k, x, d) } -func (sc *shardedCache) Get(k string) *ValueType { +func (sc *shardedCache) Get(k string) (ValueType_tpl, bool) { return sc.bucket(k).Get(k) } diff --git a/sharded_test.go b/sharded_test.go index 01e1e11..c462f02 100644 --- a/sharded_test.go +++ b/sharded_test.go @@ -29,7 +29,7 @@ var shardedKeys = []string{ func TestShardedCache(t *testing.T) { tc := unexportedNewSharded(DefaultExpiration, 0, 13) for _, v := range shardedKeys { - tc.Set(v, ValueType(1), DefaultExpiration) + tc.Set(v, ValueType_tpl(1), DefaultExpiration) } } @@ -44,7 +44,7 @@ func BenchmarkShardedCacheGetNotExpiring(b *testing.B) { func benchmarkShardedCacheGet(b *testing.B, exp time.Duration) { b.StopTimer() tc := unexportedNewSharded(exp, 0, 10) - tc.Set("foobarba", ValueType(1), DefaultExpiration) + tc.Set("foobarba", ValueType_tpl(1), DefaultExpiration) b.StartTimer() for i := 0; i < b.N; i++ { tc.Get("foobarba") @@ -67,15 +67,16 @@ func benchmarkShardedCacheGetManyConcurrent(b *testing.B, exp time.Duration) { for i := 0; i < n; i++ { k := "foo" + strconv.Itoa(i) keys[i] = k - tsc.Set(k, ValueType(1), DefaultExpiration) + tsc.Set(k, ValueType_tpl(1), DefaultExpiration) } each := b.N / n wg := new(sync.WaitGroup) wg.Add(n) for _, v := range keys { + x := v go func() { for j := 0; j < each; j++ { - tsc.Get(v) + tsc.Get(x) } wg.Done() }() diff --git a/valtyp.go b/valtyp.go index b31a4d3..905d600 100644 --- a/valtyp.go +++ b/valtyp.go @@ -1,3 +1,3 @@ package cache -type ValueType int +type ValueType_tpl int