cache test and Update added
This commit is contained in:
parent
26fb4f1043
commit
86164a864c
16
cache.go
16
cache.go
|
@ -109,6 +109,22 @@ func (c *cache) Replace(k string, x interface{}, d time.Duration) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update only if it is already existing, keep same ttl
|
||||||
|
// Set new expiry time which is now - its expiration
|
||||||
|
func (c *cache) Update(k string, x interface{}) error {
|
||||||
|
c.mu.Lock()
|
||||||
|
_, found := c.get(k)
|
||||||
|
if !found {
|
||||||
|
c.mu.Unlock()
|
||||||
|
return fmt.Errorf("Item %s doesn't exist", k)
|
||||||
|
}
|
||||||
|
newTtl := time.Unix(0, c.items[k].Expiration)
|
||||||
|
c.set(k, x, newTtl.Sub(time.Now()))
|
||||||
|
c.mu.Unlock()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Get an item from the cache. Returns the item or nil, and a bool indicating
|
// Get an item from the cache. Returns the item or nil, and a bool indicating
|
||||||
// whether the key was found.
|
// whether the key was found.
|
||||||
func (c *cache) Get(k string) (interface{}, bool) {
|
func (c *cache) Get(k string) (interface{}, bool) {
|
||||||
|
|
|
@ -27,7 +27,7 @@ var shardedKeys = []string{
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestShardedCache(t *testing.T) {
|
func TestShardedCache(t *testing.T) {
|
||||||
tc := unexportedNewSharded(DefaultExpiration, 0, 13)
|
tc := NewSharded(DefaultExpiration, 0, 13)
|
||||||
for _, v := range shardedKeys {
|
for _, v := range shardedKeys {
|
||||||
tc.Set(v, "value", DefaultExpiration)
|
tc.Set(v, "value", DefaultExpiration)
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ func BenchmarkShardedCacheGetNotExpiring(b *testing.B) {
|
||||||
|
|
||||||
func benchmarkShardedCacheGet(b *testing.B, exp time.Duration) {
|
func benchmarkShardedCacheGet(b *testing.B, exp time.Duration) {
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
tc := unexportedNewSharded(exp, 0, 10)
|
tc := NewSharded(exp, 0, 10)
|
||||||
tc.Set("foobarba", "zquux", DefaultExpiration)
|
tc.Set("foobarba", "zquux", DefaultExpiration)
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
|
@ -62,7 +62,7 @@ func BenchmarkShardedCacheGetManyConcurrentNotExpiring(b *testing.B) {
|
||||||
func benchmarkShardedCacheGetManyConcurrent(b *testing.B, exp time.Duration) {
|
func benchmarkShardedCacheGetManyConcurrent(b *testing.B, exp time.Duration) {
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
n := 10000
|
n := 10000
|
||||||
tsc := unexportedNewSharded(exp, 0, 20)
|
tsc := NewSharded(exp, 0, 20)
|
||||||
keys := make([]string, n)
|
keys := make([]string, n)
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
k := "foo" + strconv.Itoa(n)
|
k := "foo" + strconv.Itoa(n)
|
||||||
|
|
Loading…
Reference in New Issue