fixed two caching issues

This commit is contained in:
Radu Ioan Fericean
2013-11-27 20:13:25 +02:00
parent f203feab20
commit 859890f0b4
7 changed files with 57 additions and 11 deletions

View File

@@ -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

View File

@@ -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)
}
}

View File

@@ -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
}
}

View File

@@ -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
}
}

View File

@@ -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 {

View File

@@ -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

View File

@@ -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