Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
dc835ef4f8
|
@ -103,4 +103,4 @@ one) to recover from downtime quickly. (See the docs for `NewFrom()` for caveats
|
|||
|
||||
### Reference
|
||||
|
||||
`godoc` or [http://godoc.org/github.com/pmylund/go-cache](http://godoc.org/github.com/pmylund/go-cache)
|
||||
`godoc` or [http://godoc.org/github.com/patrickmn/go-cache](http://godoc.org/github.com/patrickmn/go-cache)
|
||||
|
|
55
cache.go
55
cache.go
|
@ -10,19 +10,17 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
var emptyTime = time.Time{}
|
||||
|
||||
type Item struct {
|
||||
Object interface{}
|
||||
Expiration time.Time
|
||||
Expiration int64
|
||||
}
|
||||
|
||||
// Returns true if the item has expired.
|
||||
func (item Item) Expired() bool {
|
||||
if item.Expiration == emptyTime {
|
||||
if item.Expiration == 0 {
|
||||
return false
|
||||
}
|
||||
return item.Expiration.Before(time.Now())
|
||||
return time.Now().UnixNano() > item.Expiration
|
||||
}
|
||||
|
||||
const (
|
||||
|
@ -51,20 +49,31 @@ type cache struct {
|
|||
// (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 interface{}, 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.set(k, x, d)
|
||||
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 interface{}, d time.Duration) {
|
||||
e := emptyTime
|
||||
var e int64
|
||||
if d == DefaultExpiration {
|
||||
d = c.defaultExpiration
|
||||
}
|
||||
if d > 0 {
|
||||
e = time.Now().Add(d)
|
||||
e = time.Now().Add(d).UnixNano()
|
||||
}
|
||||
c.items[k] = Item{
|
||||
Object: x,
|
||||
|
@ -104,16 +113,34 @@ func (c *cache) Replace(k string, x interface{}, d time.Duration) error {
|
|||
// whether the key was found.
|
||||
func (c *cache) Get(k string) (interface{}, bool) {
|
||||
c.mu.RLock()
|
||||
x, found := c.get(k)
|
||||
// "Inlining" of get and Expired
|
||||
item, found := c.items[k]
|
||||
if !found {
|
||||
c.mu.RUnlock()
|
||||
return x, found
|
||||
return nil, false
|
||||
}
|
||||
if item.Expiration > 0 {
|
||||
if time.Now().UnixNano() > item.Expiration {
|
||||
c.mu.RUnlock()
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
c.mu.RUnlock()
|
||||
return item.Object, true
|
||||
}
|
||||
|
||||
func (c *cache) get(k string) (interface{}, bool) {
|
||||
item, found := c.items[k]
|
||||
if !found || item.Expired() {
|
||||
if !found {
|
||||
return nil, false
|
||||
}
|
||||
// "Inlining" of Expired
|
||||
if item.Expiration > 0 {
|
||||
if time.Now().UnixNano() > item.Expiration {
|
||||
c.mu.RUnlock()
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
return item.Object, true
|
||||
}
|
||||
|
||||
|
@ -868,9 +895,11 @@ type keyAndValue struct {
|
|||
// 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 {
|
||||
if v.Expired() {
|
||||
// "Inlining" of expired
|
||||
if v.Expiration > 0 && now > v.Expiration {
|
||||
ov, evicted := c.delete(k)
|
||||
if evicted {
|
||||
evictedItems = append(evictedItems, keyAndValue{k, ov})
|
||||
|
@ -888,8 +917,8 @@ func (c *cache) DeleteExpired() {
|
|||
// not when it is overwritten.) Set to nil to disable.
|
||||
func (c *cache) OnEvicted(f func(string, interface{})) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
c.onEvicted = f
|
||||
c.mu.Unlock()
|
||||
}
|
||||
|
||||
// Write the cache's items (using Gob) to an io.Writer.
|
||||
|
|
|
@ -110,11 +110,11 @@ func TestNewFrom(t *testing.T) {
|
|||
m := map[string]Item{
|
||||
"a": Item{
|
||||
Object: 1,
|
||||
Expiration: emptyTime,
|
||||
Expiration: 0,
|
||||
},
|
||||
"b": Item{
|
||||
Object: 2,
|
||||
Expiration: emptyTime,
|
||||
Expiration: 0,
|
||||
},
|
||||
}
|
||||
tc := NewFrom(DefaultExpiration, 0, m)
|
||||
|
@ -1425,9 +1425,17 @@ func TestSerializeUnserializable(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func BenchmarkCacheGet(b *testing.B) {
|
||||
func BenchmarkCacheGetExpiring(b *testing.B) {
|
||||
benchmarkCacheGet(b, 5*time.Minute)
|
||||
}
|
||||
|
||||
func BenchmarkCacheGetNotExpiring(b *testing.B) {
|
||||
benchmarkCacheGet(b, NoExpiration)
|
||||
}
|
||||
|
||||
func benchmarkCacheGet(b *testing.B, exp time.Duration) {
|
||||
b.StopTimer()
|
||||
tc := New(DefaultExpiration, 0)
|
||||
tc := New(exp, 0)
|
||||
tc.Set("foo", "bar", DefaultExpiration)
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
|
@ -1449,9 +1457,17 @@ func BenchmarkRWMutexMapGet(b *testing.B) {
|
|||
}
|
||||
}
|
||||
|
||||
func BenchmarkCacheGetConcurrent(b *testing.B) {
|
||||
func BenchmarkCacheGetConcurrentExpiring(b *testing.B) {
|
||||
benchmarkCacheGetConcurrent(b, 5*time.Minute)
|
||||
}
|
||||
|
||||
func BenchmarkCacheGetConcurrentNotExpiring(b *testing.B) {
|
||||
benchmarkCacheGetConcurrent(b, NoExpiration)
|
||||
}
|
||||
|
||||
func benchmarkCacheGetConcurrent(b *testing.B, exp time.Duration) {
|
||||
b.StopTimer()
|
||||
tc := New(DefaultExpiration, 0)
|
||||
tc := New(exp, 0)
|
||||
tc.Set("foo", "bar", DefaultExpiration)
|
||||
wg := new(sync.WaitGroup)
|
||||
workers := runtime.NumCPU()
|
||||
|
@ -1493,13 +1509,21 @@ func BenchmarkRWMutexMapGetConcurrent(b *testing.B) {
|
|||
wg.Wait()
|
||||
}
|
||||
|
||||
func BenchmarkCacheGetManyConcurrent(b *testing.B) {
|
||||
func BenchmarkCacheGetManyConcurrentExpiring(b *testing.B) {
|
||||
benchmarkCacheGetManyConcurrent(b, 5*time.Minute)
|
||||
}
|
||||
|
||||
func BenchmarkCacheGetManyConcurrentNotExpiring(b *testing.B) {
|
||||
benchmarkCacheGetManyConcurrent(b, NoExpiration)
|
||||
}
|
||||
|
||||
func benchmarkCacheGetManyConcurrent(b *testing.B, exp time.Duration) {
|
||||
// This is the same as BenchmarkCacheGetConcurrent, but its result
|
||||
// can be compared against BenchmarkShardedCacheGetManyConcurrent
|
||||
// in sharded_test.go.
|
||||
b.StopTimer()
|
||||
n := 10000
|
||||
tc := New(DefaultExpiration, 0)
|
||||
tc := New(exp, 0)
|
||||
keys := make([]string, n)
|
||||
for i := 0; i < n; i++ {
|
||||
k := "foo" + strconv.Itoa(n)
|
||||
|
@ -1521,9 +1545,17 @@ func BenchmarkCacheGetManyConcurrent(b *testing.B) {
|
|||
wg.Wait()
|
||||
}
|
||||
|
||||
func BenchmarkCacheSet(b *testing.B) {
|
||||
func BenchmarkCacheSetExpiring(b *testing.B) {
|
||||
benchmarkCacheSet(b, 5*time.Minute)
|
||||
}
|
||||
|
||||
func BenchmarkCacheSetNotExpiring(b *testing.B) {
|
||||
benchmarkCacheSet(b, NoExpiration)
|
||||
}
|
||||
|
||||
func benchmarkCacheSet(b *testing.B, exp time.Duration) {
|
||||
b.StopTimer()
|
||||
tc := New(DefaultExpiration, 0)
|
||||
tc := New(exp, 0)
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
tc.Set("foo", "bar", DefaultExpiration)
|
||||
|
@ -1602,7 +1634,7 @@ func BenchmarkIncrementInt(b *testing.B) {
|
|||
}
|
||||
}
|
||||
|
||||
func BenchmarkDeleteExpired(b *testing.B) {
|
||||
func BenchmarkDeleteExpiredLoop(b *testing.B) {
|
||||
b.StopTimer()
|
||||
tc := New(5*time.Minute, 0)
|
||||
tc.mu.Lock()
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"strconv"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// func TestDjb33(t *testing.T) {
|
||||
|
@ -32,9 +33,17 @@ func TestShardedCache(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func BenchmarkShardedCacheGet(b *testing.B) {
|
||||
func BenchmarkShardedCacheGetExpiring(b *testing.B) {
|
||||
benchmarkShardedCacheGet(b, 5*time.Minute)
|
||||
}
|
||||
|
||||
func BenchmarkShardedCacheGetNotExpiring(b *testing.B) {
|
||||
benchmarkShardedCacheGet(b, NoExpiration)
|
||||
}
|
||||
|
||||
func benchmarkShardedCacheGet(b *testing.B, exp time.Duration) {
|
||||
b.StopTimer()
|
||||
tc := unexportedNewSharded(DefaultExpiration, 0, 10)
|
||||
tc := unexportedNewSharded(exp, 0, 10)
|
||||
tc.Set("foobarba", "zquux", DefaultExpiration)
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
|
@ -42,10 +51,18 @@ func BenchmarkShardedCacheGet(b *testing.B) {
|
|||
}
|
||||
}
|
||||
|
||||
func BenchmarkShardedCacheGetManyConcurrent(b *testing.B) {
|
||||
func BenchmarkShardedCacheGetManyConcurrentExpiring(b *testing.B) {
|
||||
benchmarkShardedCacheGetManyConcurrent(b, 5*time.Minute)
|
||||
}
|
||||
|
||||
func BenchmarkShardedCacheGetManyConcurrentNotExpiring(b *testing.B) {
|
||||
benchmarkShardedCacheGetManyConcurrent(b, NoExpiration)
|
||||
}
|
||||
|
||||
func benchmarkShardedCacheGetManyConcurrent(b *testing.B, exp time.Duration) {
|
||||
b.StopTimer()
|
||||
n := 10000
|
||||
tsc := unexportedNewSharded(DefaultExpiration, 0, 20)
|
||||
tsc := unexportedNewSharded(exp, 0, 20)
|
||||
keys := make([]string, n)
|
||||
for i := 0; i < n; i++ {
|
||||
k := "foo" + strconv.Itoa(n)
|
||||
|
|
Loading…
Reference in New Issue