diff --git a/cache2go/cache.go b/cache2go/cache.go index 9b7e6d2b8..10df14575 100644 --- a/cache2go/cache.go +++ b/cache2go/cache.go @@ -111,14 +111,14 @@ func RemKey(key string) { } func XRemKey(key string) { - mux.Lock() - defer mux.Unlock() + xMux.Lock() + defer xMux.Unlock() if r, ok := xcache[key]; ok { if r.timer() != nil { r.timer().Stop() } } - delete(cache, key) + delete(xcache, key) } // Delete all keys from expiraton cache diff --git a/cache2go/cache_test.go b/cache2go/cache_test.go index f2af3f18d..010e786a5 100644 --- a/cache2go/cache_test.go +++ b/cache2go/cache_test.go @@ -73,3 +73,26 @@ func TestFlushNoTimout(t *testing.T) { t.Error("Error expiring data") } } + +func TestRemKey(t *testing.T) { + Cache("t1", "test") + if t1, err := GetCached("t1"); err != nil || t1 != "test" { + t.Error("Error setting cache") + } + RemKey("t1") + if t1, err := GetCached("t1"); err == nil || t1 == "test" { + t.Error("Error removing cached key") + } +} + +func TestXRemKey(t *testing.T) { + a := &myStruct{data: "mama are mere"} + a.XCache("mama", 10*time.Second, a) + if t1, err := GetXCached("mama"); err != nil || t1 != a { + t.Error("Error setting xcache") + } + XRemKey("mama") + if t1, err := GetXCached("mama"); err == nil || t1 == a { + t.Error("Error removing xcached key: ", err, t1) + } +} diff --git a/engine/storage_map.go b/engine/storage_map.go index d0b52e315..a2d61a727 100644 --- a/engine/storage_map.go +++ b/engine/storage_map.go @@ -21,6 +21,7 @@ package engine import ( "errors" "fmt" + "github.com/cgrates/cgrates/cache2go" "github.com/cgrates/cgrates/history" "github.com/cgrates/cgrates/utils" @@ -45,14 +46,18 @@ func (ms *MapStorage) Flush() error { } func (ms *MapStorage) PreCache(dKeys, rppKeys []string) error { + prefixLen := len(DESTINATION_PREFIX) + prefixLen1 := len(RATING_PLAN_PREFIX) for k, _ := range ms.dict { if strings.HasPrefix(k, DESTINATION_PREFIX) { - if _, err := ms.GetDestination(k[len(DESTINATION_PREFIX):]); err != nil { + cache2go.RemKey(k[prefixLen:]) + if _, err := ms.GetDestination(k[prefixLen:]); err != nil { return err } } if strings.HasPrefix(k, RATING_PLAN_PREFIX) { - if _, err := ms.GetRatingPlan(k[len(RATING_PLAN_PREFIX):]); err != nil { + cache2go.RemKey(k[prefixLen1:]) + if _, err := ms.GetRatingPlan(k[prefixLen1:]); err != nil { return err } } diff --git a/engine/storage_redis.go b/engine/storage_redis.go index 528134821..f0b89bf6f 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -72,9 +72,10 @@ func (rs *RedisStorage) PreCache(dKeys, rpKeys []string) (err error) { return } } + prefixLen := len(DESTINATION_PREFIX) for _, key := range dKeys { - cache2go.RemKey(key) - if _, err = rs.GetDestination(key[len(DESTINATION_PREFIX):]); err != nil { + cache2go.RemKey(key[prefixLen:]) + if _, err = rs.GetDestination(key[prefixLen:]); err != nil { return err } } @@ -83,9 +84,10 @@ func (rs *RedisStorage) PreCache(dKeys, rpKeys []string) (err error) { return } } + prefixLen = len(RATING_PLAN_PREFIX) for _, key := range rpKeys { - cache2go.RemKey(key) - if _, err = rs.GetRatingPlan(key[len(RATING_PLAN_PREFIX):]); err != nil { + cache2go.RemKey(key[prefixLen:]) + if _, err = rs.GetRatingPlan(key[prefixLen:]); err != nil { return err } } diff --git a/engine/storage_test.go b/engine/storage_test.go index 2717c6c0c..477419808 100644 --- a/engine/storage_test.go +++ b/engine/storage_test.go @@ -95,6 +95,16 @@ func TestStorageDestinationContainsPrefixNotExisting(t *testing.T) { } } +func TestPreCacheRefresh(t *testing.T) { + storageGetter.SetDestination(&Destination{"T11", []string{"0"}}) + storageGetter.GetDestination("T11") + storageGetter.SetDestination(&Destination{"T11", []string{"1"}}) + storageGetter.PreCache(nil, nil) + if d, err := storageGetter.GetDestination("T11"); err != nil || d.Prefixes[0] != "1" { + t.Error("Error refreshing cache:", d) + } +} + /************************** Benchmarks *****************************/ func GetUB() *UserBalance { diff --git a/test.sh b/test.sh index 2ac429a0c..4355e005d 100755 --- a/test.sh +++ b/test.sh @@ -6,6 +6,7 @@ go test -i github.com/cgrates/cgrates/config go test -i github.com/cgrates/cgrates/cmd/cgr-engine go test -i github.com/cgrates/cgrates/mediator go test -i github.com/cgrates/fsock +go test -i github.com/cgrates/cgrates/cache2go go test -i github.com/cgrates/cgrates/cdrs go test -i github.com/cgrates/cgrates/utils go test -i github.com/cgrates/cgrates/history @@ -29,8 +30,10 @@ go test github.com/cgrates/fsock fs=$? go test github.com/cgrates/cgrates/history hs=$? +go test github.com/cgrates/cgrates/cache2go +c2g=$? go test github.com/cgrates/cgrates/cdrexporter cdre=$? -exit $en && $sm && $cfg && $bl && $cr && $md && $cdr && $fs && $ut && $hs && $cdre +exit $en && $sm && $cfg && $bl && $cr && $md && $cdr && $fs && $ut && $hs && $c2g && $cdre diff --git a/test_local.sh b/test_local.sh index cff5ad42e..cf4704926 100755 --- a/test_local.sh +++ b/test_local.sh @@ -7,6 +7,7 @@ go test -i github.com/cgrates/cgrates/config go test -i github.com/cgrates/cgrates/cmd/cgr-engine go test -i github.com/cgrates/cgrates/mediator go test -i github.com/cgrates/fsock +go test -i github.com/cgrates/cgrates/cache2go go test -i github.com/cgrates/cgrates/cdrs go test -i github.com/cgrates/cgrates/utils go test -i github.com/cgrates/cgrates/history @@ -32,8 +33,10 @@ go test github.com/cgrates/fsock fs=$? go test github.com/cgrates/cgrates/history hs=$? +go test github.com/cgrates/cgrates/cache2go +c2g=$? go test github.com/cgrates/cgrates/cdrexporter cdre=$? -exit $ap && $en && $sm && $cfg && $bl && $cr && $md && $cdr && $fs && $ut && $hs && $cdre +exit $ap && $en && $sm && $cfg && $bl && $cr && $md && $cdr && $fs && $ut && $hs && $c2g && $cdre