more cache fixes

This commit is contained in:
Radu Ioan Fericean
2014-04-28 12:45:21 +03:00
parent 21176f7c88
commit c9493005cd
2 changed files with 17 additions and 10 deletions

View File

@@ -67,8 +67,10 @@ func (xe *XEntry) expire() {
<-xe.t.C
if !xe.keepAlive {
xMux.Lock()
delete(xcache, xe.key)
descount(xe.key)
if _, ok := xcache[xe.key]; ok {
delete(xcache, xe.key)
descount(xe.key)
}
xMux.Unlock()
}
}
@@ -137,7 +139,10 @@ func GetKeyAge(key string) (time.Duration, error) {
func RemKey(key string) {
mux.Lock()
defer mux.Unlock()
delete(cache, key)
if _, ok := cache[key]; ok {
delete(cache, key)
descount(key)
}
xMux.Lock()
defer xMux.Unlock()
if r, ok := xcache[key]; ok {
@@ -145,8 +150,10 @@ func RemKey(key string) {
r.timer().Stop()
}
}
delete(xcache, key)
descount(key)
if _, ok := xcache[key]; ok {
delete(xcache, key)
descount(key)
}
}
func RemPrefixKey(prefix string) {
@@ -235,7 +242,7 @@ func descount(key string) {
cMux.Lock()
defer cMux.Unlock()
prefix := key[:PREFIX_LEN]
if _, ok := counters[prefix]; ok {
if value, ok := counters[prefix]; ok && value > 0 {
counters[prefix] -= 1
}
}

View File

@@ -75,12 +75,12 @@ func TestFlushNoTimout(t *testing.T) {
}
func TestRemKey(t *testing.T) {
Cache("t1", "test")
if t1, err := GetCached("t1"); err != nil || t1 != "test" {
Cache("t11_mm", "test")
if t1, err := GetCached("t11_mm"); err != nil || t1 != "test" {
t.Error("Error setting cache")
}
RemKey("t1")
if t1, err := GetCached("t1"); err == nil || t1 == "test" {
RemKey("t11_mm")
if t1, err := GetCached("t11_mm"); err == nil || t1 == "test" {
t.Error("Error removing cached key")
}
}