diff --git a/cache2go/cache.go b/cache2go/cache.go index 10df14575..9fde2248c 100644 --- a/cache2go/cache.go +++ b/cache2go/cache.go @@ -3,6 +3,7 @@ package cache2go import ( "errors" + "strings" "sync" "time" ) @@ -139,3 +140,21 @@ func Flush() { defer mux.Unlock() cache = make(map[string]timestampedValue) } + +func CountEntries(prefix string) (result int) { + for key, _ := range cache { + if strings.HasPrefix(key, prefix) { + result++ + } + } + return +} + +func XCountEntries(prefix string) (result int) { + for key, _ := range xcache { + if strings.HasPrefix(key, prefix) { + result++ + } + } + return +} diff --git a/engine/destinations_test.go b/engine/destinations_test.go index 55004ead0..a4ebc2105 100644 --- a/engine/destinations_test.go +++ b/engine/destinations_test.go @@ -84,7 +84,7 @@ func TestDestinationGetExists(t *testing.T) { func TestDestinationGetExistsCache(t *testing.T) { storageGetter.GetDestination("NAT", false) - if _, err := cache2go.GetCached("NAT"); err != nil { + if _, err := cache2go.GetCached(DESTINATION_PREFIX + "NAT"); err != nil { t.Error("Destination not cached:", err) } } diff --git a/engine/storage_map.go b/engine/storage_map.go index ae0eb134f..9363afff7 100644 --- a/engine/storage_map.go +++ b/engine/storage_map.go @@ -22,11 +22,11 @@ import ( "errors" "fmt" + "strings" + "time" "github.com/cgrates/cgrates/cache2go" "github.com/cgrates/cgrates/history" "github.com/cgrates/cgrates/utils" - "strings" - "time" ) type MapStorage struct { @@ -79,13 +79,14 @@ func (ms *MapStorage) ExistsData(categ, subject string) (bool, error) { } func (ms *MapStorage) GetRatingPlan(key string, checkDb bool) (rp *RatingPlan, err error) { + key = RATING_PLAN_PREFIX + key if x, err := cache2go.GetCached(key); err == nil { return x.(*RatingPlan), nil } if !checkDb { return nil, errors.New(utils.ERR_NOT_FOUND) } - if values, ok := ms.dict[RATING_PLAN_PREFIX+key]; ok { + if values, ok := ms.dict[key]; ok { rp = new(RatingPlan) err = ms.ms.Unmarshal(values, rp) @@ -101,7 +102,7 @@ func (ms *MapStorage) SetRatingPlan(rp *RatingPlan) (err error) { ms.dict[RATING_PLAN_PREFIX+rp.Id] = result response := 0 go historyScribe.Record(&history.Record{RATING_PLAN_PREFIX + rp.Id, rp}, &response) - cache2go.Cache(rp.Id, rp) + cache2go.Cache(RATING_PLAN_PREFIX+rp.Id, rp) return } @@ -125,13 +126,14 @@ func (ms *MapStorage) SetRatingProfile(rp *RatingProfile) (err error) { } func (ms *MapStorage) GetDestination(key string, checkDb bool) (dest *Destination, err error) { + key = DESTINATION_PREFIX + key if x, err := cache2go.GetCached(key); err == nil { return x.(*Destination), nil } if !checkDb { return nil, errors.New(utils.ERR_NOT_FOUND) } - if values, ok := ms.dict[DESTINATION_PREFIX+key]; ok { + if values, ok := ms.dict[key]; ok { dest = &Destination{Id: key} err = ms.ms.Unmarshal(values, dest) cache2go.Cache(key, dest) @@ -146,7 +148,7 @@ func (ms *MapStorage) SetDestination(dest *Destination) (err error) { ms.dict[DESTINATION_PREFIX+dest.Id] = result response := 0 go historyScribe.Record(&history.Record{DESTINATION_PREFIX + dest.Id, dest}, &response) - cache2go.Cache(dest.Id, dest) + cache2go.Cache(DESTINATION_PREFIX+dest.Id, dest) return } diff --git a/engine/storage_redis.go b/engine/storage_redis.go index 56c07b272..e5d5d3e26 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -118,6 +118,7 @@ func (rs *RedisStorage) ExistsData(category, subject string) (bool, error) { } func (rs *RedisStorage) GetRatingPlan(key string, checkDb bool) (rp *RatingPlan, err error) { + key = RATING_PLAN_PREFIX + key if x, err := cache2go.GetCached(key); err == nil { return x.(*RatingPlan), nil } @@ -125,7 +126,7 @@ func (rs *RedisStorage) GetRatingPlan(key string, checkDb bool) (rp *RatingPlan, return nil, errors.New(utils.ERR_NOT_FOUND) } var values []byte - if values, err = rs.db.Get(RATING_PLAN_PREFIX + key); err == nil { + if values, err = rs.db.Get(key); err == nil { b := bytes.NewBuffer(values) r, err := zlib.NewReader(b) if err != nil { @@ -154,7 +155,7 @@ func (rs *RedisStorage) SetRatingPlan(rp *RatingPlan) (err error) { response := 0 go historyScribe.Record(&history.Record{RATING_PLAN_PREFIX + rp.Id, rp}, &response) } - cache2go.Cache(rp.Id, rp) + cache2go.Cache(RATING_PLAN_PREFIX+rp.Id, rp) return } @@ -178,6 +179,7 @@ func (rs *RedisStorage) SetRatingProfile(rp *RatingProfile) (err error) { } func (rs *RedisStorage) GetDestination(key string, checkDb bool) (dest *Destination, err error) { + key = DESTINATION_PREFIX + key if x, err := cache2go.GetCached(key); err == nil { return x.(*Destination), nil } @@ -185,7 +187,7 @@ func (rs *RedisStorage) GetDestination(key string, checkDb bool) (dest *Destinat return nil, errors.New(utils.ERR_NOT_FOUND) } var values []byte - if values, err = rs.db.Get(DESTINATION_PREFIX + key); len(values) > 0 && err == nil { + if values, err = rs.db.Get(key); len(values) > 0 && err == nil { b := bytes.NewBuffer(values) r, err := zlib.NewReader(b) if err != nil { @@ -217,7 +219,7 @@ func (rs *RedisStorage) SetDestination(dest *Destination) (err error) { response := 0 go historyScribe.Record(&history.Record{DESTINATION_PREFIX + dest.Id, dest}, &response) } - cache2go.Cache(dest.Id, dest) + cache2go.Cache(DESTINATION_PREFIX+dest.Id, dest) return }