started new destination caching

This commit is contained in:
Radu Ioan Fericean
2013-12-17 17:51:38 +02:00
parent 3e4067b4b0
commit d69378a626
7 changed files with 36 additions and 22 deletions

View File

@@ -779,7 +779,7 @@ func TestActionResetAllCounters(t *testing.T) {
len(ub.UnitCounters[0].Balances) != 2 ||
len(ub.BalanceMap[MINUTES]) != 2 ||
ub.ActionTriggers[0].Executed != true {
t.Errorf("Reset counters action failed: %+v", ub.UnitCounters[0])
t.Errorf("Reset counters action failed: %+v", ub.UnitCounters)
}
if len(ub.UnitCounters) < 1 {
t.FailNow()

View File

@@ -19,7 +19,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package engine
import (
// "log"
"testing"
"time"
)

View File

@@ -43,7 +43,6 @@ func init() {
//storageGetter, _ = NewMongoStorage(db_server, "27017", "cgrates_test", "", "")
storageGetter, _ = NewRedisStorage("127.0.0.1:6379", 11, "", utils.MSGPACK)
}
storageLogger = storageGetter.(LogStorage)
}

View File

@@ -38,7 +38,7 @@ type Destination struct {
}
// returns prefix precision
func (d *Destination) containsPrefix1(prefix string) int {
func (d *Destination) containsPrefix(prefix string) int {
if d == nil {
return 0
}

View File

@@ -23,6 +23,9 @@ import (
"fmt"
"sort"
"time"
"github.com/cgrates/cgrates/cache2go"
"github.com/cgrates/cgrates/utils"
)
type RatingProfile struct {
@@ -110,17 +113,19 @@ func (rp *RatingProfile) GetRatingPlansForPrefix(cd *CallDescriptor) (err error)
}
bestPrecision := 0
var rps RateIntervalList
for dId, _ := range rpl.DestinationRates {
//precision, err := storageGetter.DestinationContainsPrefix(dId, cd.Destination)
d, err := storageGetter.GetDestination(dId, false)
if err != nil {
Logger.Err(fmt.Sprintf("Error checking destination: %v", err))
continue
for _, p := range utils.SplitPrefix(cd.Destination) {
if x, err := cache2go.GetCached(p); err == nil {
destIds := x.([]string)
for _, dId := range destIds {
if _, ok := rpl.DestinationRates[dId]; ok {
rps = rpl.RateIntervalList(dId)
bestPrecision = len(p)
break
}
}
}
precision := d.containsPrefix(cd.Destination)
if precision > bestPrecision {
bestPrecision = precision
rps = rpl.RateIntervalList(dId)
if rps != nil {
break
}
}
// check if it's the first ri and add a blank one for the initial part not covered

View File

@@ -52,6 +52,7 @@ func (ms *MapStorage) PreCache(dKeys, rpKeys, rpfKeys, actKeys []string) error {
for k, _ := range ms.dict {
if strings.HasPrefix(k, DESTINATION_PREFIX) {
cache2go.RemKey(k)
// TODO: here I must delete all optimized prefixes
if _, err := ms.GetDestination(k[len(DESTINATION_PREFIX):], true); err != nil {
return err
}
@@ -115,7 +116,6 @@ 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(RATING_PLAN_PREFIX+rp.Id, rp)
return
}
@@ -143,7 +143,6 @@ func (ms *MapStorage) SetRatingProfile(rpf *RatingProfile) (err error) {
ms.dict[RATING_PROFILE_PREFIX+rpf.Id] = result
response := 0
go historyScribe.Record(&history.Record{RATING_PROFILE_PREFIX + rpf.Id, rpf}, &response)
cache2go.Cache(RATING_PROFILE_PREFIX+rpf.Id, rpf)
return
}
@@ -158,6 +157,15 @@ func (ms *MapStorage) GetDestination(key string, checkDb bool) (dest *Destinatio
if values, ok := ms.dict[key]; ok {
dest = &Destination{Id: key}
err = ms.ms.Unmarshal(values, dest)
// create optimized structure
for _, p := range dest.Prefixes {
var ids []string
if x, err := cache2go.GetCached(p); err == nil {
ids = x.([]string)
}
ids = append(ids, dest.Id)
cache2go.Cache(p, ids)
}
dest.OptimizePrefixes()
cache2go.Cache(key, dest)
} else {
@@ -171,7 +179,6 @@ 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(DESTINATION_PREFIX+dest.Id, dest)
return
}
@@ -202,7 +209,6 @@ func (ms *MapStorage) GetActions(key string, checkDb bool) (as Actions, err erro
func (ms *MapStorage) SetActions(key string, as Actions) (err error) {
result, err := ms.ms.Marshal(&as)
ms.dict[ACTION_PREFIX+key] = result
cache2go.Cache(DESTINATION_PREFIX+key, as)
return
}

View File

@@ -190,7 +190,6 @@ func (rs *RedisStorage) SetRatingPlan(rp *RatingPlan) (err error) {
response := 0
go historyScribe.Record(&history.Record{RATING_PLAN_PREFIX + rp.Id, rp}, &response)
}
cache2go.Cache(RATING_PLAN_PREFIX+rp.Id, rp)
return
}
@@ -218,7 +217,6 @@ func (rs *RedisStorage) SetRatingProfile(rpf *RatingProfile) (err error) {
response := 0
go historyScribe.Record(&history.Record{RATING_PROFILE_PREFIX + rpf.Id, rpf}, &response)
}
cache2go.Cache(RATING_PROFILE_PREFIX+rpf.Id, rpf)
return
}
@@ -244,6 +242,15 @@ func (rs *RedisStorage) GetDestination(key string, checkDb bool) (dest *Destinat
r.Close()
dest = new(Destination)
err = rs.ms.Unmarshal(out, dest)
// create optimized structure
for _, p := range dest.Prefixes {
var ids []string
if x, err := cache2go.GetCached(p); err == nil {
ids = x.([]string)
}
ids = append(ids, dest.Id)
cache2go.Cache(p, ids)
}
dest.OptimizePrefixes()
cache2go.Cache(key, dest)
}
@@ -264,7 +271,6 @@ func (rs *RedisStorage) SetDestination(dest *Destination) (err error) {
response := 0
go historyScribe.Record(&history.Record{DESTINATION_PREFIX + dest.Id, dest}, &response)
}
cache2go.Cache(DESTINATION_PREFIX+dest.Id, dest)
return
}
@@ -287,7 +293,6 @@ func (rs *RedisStorage) GetActions(key string, checkDb bool) (as Actions, err er
func (rs *RedisStorage) SetActions(key string, as Actions) (err error) {
result, err := rs.ms.Marshal(&as)
err = rs.db.Set(ACTION_PREFIX+key, result)
cache2go.Cache(ACTION_PREFIX+key, as)
return
}