func testOnStorITCacheDerivedChargers(t *testing.T) { dcs := &utils.DerivedChargers{ DestinationIDs: make(utils.StringMap), Chargers: []*utils.DerivedCharger{ &utils.DerivedCharger{RunID: "extra1", RunFilters: "^filteredHeader1/filterValue1/", RequestTypeField: "^prepaid", DirectionField: utils.META_DEFAULT, TenantField: utils.META_DEFAULT, CategoryField: utils.META_DEFAULT, AccountField: "rif", SubjectField: "rif", DestinationField: utils.META_DEFAULT, SetupTimeField: utils.META_DEFAULT, PDDField: utils.META_DEFAULT, AnswerTimeField: utils.META_DEFAULT, UsageField: utils.META_DEFAULT, SupplierField: utils.META_DEFAULT, DisconnectCauseField: utils.META_DEFAULT, CostField: utils.META_DEFAULT, RatedField: utils.META_DEFAULT}, &utils.DerivedCharger{RunID: "extra2", RequestTypeField: utils.META_DEFAULT, DirectionField: utils.META_DEFAULT, TenantField: utils.META_DEFAULT, CategoryField: utils.META_DEFAULT, AccountField: "ivo", SubjectField: "ivo", DestinationField: utils.META_DEFAULT, SetupTimeField: utils.META_DEFAULT, PDDField: utils.META_DEFAULT, AnswerTimeField: utils.META_DEFAULT, UsageField: utils.META_DEFAULT, SupplierField: utils.META_DEFAULT, DisconnectCauseField: utils.META_DEFAULT, CostField: utils.META_DEFAULT, RatedField: utils.META_DEFAULT}, }} keyDCS := utils.ConcatenatedKey("*out", "itsyscom.com", "call", "dan", "dan") if err := onStor.SetDerivedChargers(keyDCS, dcs, utils.NonTransactional); err != nil { t.Error(err) } if _, hasIt := cache.Get(utils.DERIVEDCHARGERS_PREFIX + keyDCS); hasIt { t.Error("Already in cache") } if err := onStor.CacheDataFromDB(utils.DERIVEDCHARGERS_PREFIX, []string{keyDCS}, false); err != nil { t.Error(err) } if itm, hasIt := cache.Get(utils.DERIVEDCHARGERS_PREFIX + keyDCS); !hasIt { t.Error("Did not cache") } else if rcv := itm.(*utils.DerivedChargers); !reflect.DeepEqual(dcs, rcv) { t.Errorf("Expecting: %+v, received: %+v", dcs, rcv) } }
func testOnStorITCacheDestinations(t *testing.T) { if err := onStor.CacheDataFromDB("INVALID", nil, false); err == nil || err.Error() != utils.UnsupportedCachePrefix { t.Error(err) } dst := &Destination{Id: "TEST_CACHE", Prefixes: []string{"+491", "+492", "+493"}} if err := onStor.SetDestination(dst, utils.NonTransactional); err != nil { t.Error(err) } if _, hasIt := cache.Get(utils.DESTINATION_PREFIX + dst.Id); hasIt { t.Error("Already in cache") } if err := onStor.CacheDataFromDB(utils.DESTINATION_PREFIX, []string{dst.Id}, true); err != nil { // Should not cache due to mustBeCached t.Error(err) } if _, hasIt := cache.Get(utils.DESTINATION_PREFIX + dst.Id); hasIt { t.Error("Should not be in cache") } if err := onStor.CacheDataFromDB(utils.DESTINATION_PREFIX, []string{dst.Id}, false); err != nil { t.Error(err) } if itm, hasIt := cache.Get(utils.DESTINATION_PREFIX + dst.Id); !hasIt { t.Error("Did not cache") } else if !reflect.DeepEqual(dst, itm.(*Destination)) { t.Error("Wrong item in the cache") } }
func TestStorageCacheRemoveCachedAliases(t *testing.T) { ala := &Alias{ Direction: "*out", Tenant: "vdf", Category: "0", Account: "b1", Subject: "b1", Context: utils.ALIAS_CONTEXT_RATING, } alb := &Alias{ Direction: "*out", Tenant: "vdf", Category: "0", Account: "b1", Subject: "b1", Context: "*other", } accountingStorage.RemoveAlias(ala.GetId(), utils.NonTransactional) accountingStorage.RemoveAlias(alb.GetId(), utils.NonTransactional) if _, ok := cache.Get(utils.ALIASES_PREFIX + ala.GetId()); ok { t.Error("Error removing cached alias: ", ok) } if _, ok := cache.Get(utils.ALIASES_PREFIX + alb.GetId()); ok { t.Error("Error removing cached alias: ", ok) } if _, ok := cache.Get(utils.REVERSE_ALIASES_PREFIX + "aaa" + utils.ALIAS_CONTEXT_RATING); ok { t.Error("Error removing cached reverse alias: ", ok) } if _, ok := cache.Get(utils.REVERSE_ALIASES_PREFIX + "aaa" + utils.ALIAS_CONTEXT_RATING); ok { t.Error("Error removing cached reverse alias: ", ok) } }
func testOnStorITCacheRatingProfile(t *testing.T) { rpf := &RatingProfile{ Id: "*out:test:0:trp", RatingPlanActivations: RatingPlanActivations{ &RatingPlanActivation{ ActivationTime: time.Date(2013, 10, 1, 0, 0, 0, 0, time.UTC).Local(), RatingPlanId: "TDRT", FallbackKeys: []string{"*out:test:0:danb", "*out:test:0:rif"}, CdrStatQueueIds: []string{}, }}, } if err := onStor.SetRatingProfile(rpf, utils.NonTransactional); err != nil { t.Error(err) } if _, hasIt := cache.Get(utils.RATING_PROFILE_PREFIX + rpf.Id); hasIt { t.Error("Already in cache") } if err := onStor.CacheDataFromDB(utils.RATING_PROFILE_PREFIX, []string{rpf.Id}, false); err != nil { t.Error(err) } if itm, hasIt := cache.Get(utils.RATING_PROFILE_PREFIX + rpf.Id); !hasIt { t.Error("Did not cache") } else if rcvRp := itm.(*RatingProfile); !reflect.DeepEqual(rpf, rcvRp) { t.Errorf("Expecting: %+v, received: %+v", rpf, rcvRp) } }
func testOnStorITCacheSharedGroup(t *testing.T) { sg := &SharedGroup{ Id: "SG1", AccountParameters: map[string]*SharingParameters{ "*any": &SharingParameters{ Strategy: "*lowest", RatingSubject: "", }, }, MemberIds: make(utils.StringMap), } if err := onStor.SetSharedGroup(sg, utils.NonTransactional); err != nil { t.Error(err) } if _, hasIt := cache.Get(utils.SHARED_GROUP_PREFIX + sg.Id); hasIt { t.Error("Already in cache") } if err := onStor.CacheDataFromDB(utils.SHARED_GROUP_PREFIX, []string{sg.Id}, false); err != nil { t.Error(err) } if itm, hasIt := cache.Get(utils.SHARED_GROUP_PREFIX + sg.Id); !hasIt { t.Error("Did not cache") } else if rcv := itm.(*SharedGroup); !reflect.DeepEqual(sg, rcv) { t.Errorf("Expecting: %+v, received: %+v", sg, rcv) } }
func testOnStorITCacheResourceLimit(t *testing.T) { rL := &ResourceLimit{ ID: "RL_TEST", Weight: 10, Filters: []*RequestFilter{ &RequestFilter{Type: MetaString, FieldName: "Account", Values: []string{"dan", "1002"}}, &RequestFilter{Type: MetaRSRFields, Values: []string{"Subject(~^1.*1$)", "Destination(1002)"}, rsrFields: utils.ParseRSRFieldsMustCompile("Subject(~^1.*1$);Destination(1002)", utils.INFIELD_SEP), }}, ActivationTime: time.Date(2014, 7, 3, 13, 43, 0, 0, time.UTC).Local(), ExpiryTime: time.Date(2015, 7, 3, 13, 43, 0, 0, time.UTC).Local(), Limit: 1, ActionTriggers: make(ActionTriggers, 0), UsageTTL: time.Duration(1 * time.Millisecond), Usage: make(map[string]*ResourceUsage), } if err := onStor.SetResourceLimit(rL, utils.NonTransactional); err != nil { t.Error(err) } if _, hasIt := cache.Get(utils.ResourceLimitsPrefix + rL.ID); hasIt { t.Error("Already in cache") } if err := onStor.CacheDataFromDB(utils.ResourceLimitsPrefix, []string{rL.ID}, false); err != nil { t.Error(err) } if itm, hasIt := cache.Get(utils.ResourceLimitsPrefix + rL.ID); !hasIt { t.Error("Did not cache") } else if rcv := itm.(*ResourceLimit); !reflect.DeepEqual(rL, rcv) { t.Errorf("Expecting: %+v, received: %+v", rL.ActivationTime, rcv.ActivationTime) } }
func testOnStorITCacheActions(t *testing.T) { acts := Actions{ &Action{ Id: "MINI", ActionType: TOPUP_RESET, ExpirationString: UNLIMITED, Weight: 10, Balance: &BalanceFilter{ Type: utils.StringPointer(utils.MONETARY), Uuid: utils.StringPointer(utils.GenUUID()), Directions: utils.StringMapPointer(utils.NewStringMap(utils.OUT)), Value: &utils.ValueFormula{Static: 10, Params: make(map[string]interface{})}, Weight: utils.Float64Pointer(10), Disabled: utils.BoolPointer(false), Timings: make([]*RITiming, 0), Blocker: utils.BoolPointer(false), }, }, &Action{ Id: "MINI", ActionType: TOPUP, ExpirationString: UNLIMITED, Weight: 10, Balance: &BalanceFilter{ Type: utils.StringPointer(utils.VOICE), Uuid: utils.StringPointer(utils.GenUUID()), Directions: utils.StringMapPointer(utils.NewStringMap(utils.OUT)), Value: &utils.ValueFormula{Static: 100, Params: make(map[string]interface{})}, Weight: utils.Float64Pointer(10), RatingSubject: utils.StringPointer("test"), DestinationIDs: utils.StringMapPointer(utils.NewStringMap("NAT")), Disabled: utils.BoolPointer(false), Timings: make([]*RITiming, 0), Blocker: utils.BoolPointer(false), }, }, } if err := onStor.SetActions(acts[0].Id, acts, utils.NonTransactional); err != nil { t.Error(err) } if _, hasIt := cache.Get(utils.ACTION_PREFIX + acts[0].Id); hasIt { t.Error("Already in cache") } if err := onStor.CacheDataFromDB(utils.ACTION_PREFIX, []string{acts[0].Id}, false); err != nil { t.Error(err) } if itm, hasIt := cache.Get(utils.ACTION_PREFIX + acts[0].Id); !hasIt { t.Error("Did not cache") } else if rcv := itm.(Actions); !reflect.DeepEqual(acts, rcv) { t.Errorf("Expecting: %+v, received: %+v", acts, rcv) } }
func testOnStorITCacheRatingPlan(t *testing.T) { rp := &RatingPlan{ Id: "TEST_RP_CACHE", Timings: map[string]*RITiming{ "59a981b9": &RITiming{ Years: utils.Years{}, Months: utils.Months{}, MonthDays: utils.MonthDays{}, WeekDays: utils.WeekDays{1, 2, 3, 4, 5}, StartTime: "00:00:00", }, }, Ratings: map[string]*RIRate{ "ebefae11": &RIRate{ ConnectFee: 0, Rates: []*Rate{ &Rate{ GroupIntervalStart: 0, Value: 0.2, RateIncrement: time.Second, RateUnit: time.Minute, }, }, RoundingMethod: utils.ROUNDING_MIDDLE, RoundingDecimals: 4, }, }, DestinationRates: map[string]RPRateList{ "GERMANY": []*RPRate{ &RPRate{ Timing: "59a981b9", Rating: "ebefae11", Weight: 10, }, }, }, } if err := onStor.SetRatingPlan(rp, utils.NonTransactional); err != nil { t.Error(err) } if _, hasIt := cache.Get(utils.RATING_PLAN_PREFIX + rp.Id); hasIt { t.Error("Already in cache") } if err := onStor.CacheDataFromDB(utils.RATING_PLAN_PREFIX, []string{rp.Id}, false); err != nil { t.Error(err) } if itm, hasIt := cache.Get(utils.RATING_PLAN_PREFIX + rp.Id); !hasIt { t.Error("Did not cache") } else if rcvRp := itm.(*RatingPlan); !reflect.DeepEqual(rp, rcvRp) { t.Error("Wrong item in the cache") } }
func testOnStorITCacheActionPlan(t *testing.T) { ap := &ActionPlan{ Id: "MORE_MINUTES", AccountIDs: utils.StringMap{"vdf:minitsboy": true}, ActionTimings: []*ActionTiming{ &ActionTiming{ Uuid: utils.GenUUID(), Timing: &RateInterval{ Timing: &RITiming{ Years: utils.Years{2012}, Months: utils.Months{}, MonthDays: utils.MonthDays{}, WeekDays: utils.WeekDays{}, StartTime: utils.ASAP, }, }, Weight: 10, ActionsID: "MINI", }, &ActionTiming{ Uuid: utils.GenUUID(), Timing: &RateInterval{ Timing: &RITiming{ Years: utils.Years{2012}, Months: utils.Months{}, MonthDays: utils.MonthDays{}, WeekDays: utils.WeekDays{}, StartTime: utils.ASAP, }, }, Weight: 10, ActionsID: "SHARED", }, }, } if err := onStor.SetActionPlan(ap.Id, ap, true, utils.NonTransactional); err != nil { t.Error(err) } if _, hasIt := cache.Get(utils.ACTION_PLAN_PREFIX + ap.Id); hasIt { t.Error("Already in cache") } if err := onStor.CacheDataFromDB(utils.ACTION_PLAN_PREFIX, []string{ap.Id}, false); err != nil { t.Error(err) } if itm, hasIt := cache.Get(utils.ACTION_PLAN_PREFIX + ap.Id); !hasIt { t.Error("Did not cache") } else if rcv := itm.(*ActionPlan); !reflect.DeepEqual(ap, rcv) { t.Errorf("Expecting: %+v, received: %+v", ap, rcv) } }
func (ms *MongoStorage) GetAlias(key string, skipCache bool, transactionID string) (al *Alias, err error) { origKey := key key = utils.ALIASES_PREFIX + key if !skipCache { if x, ok := cache.Get(key); ok { if x != nil { al = &Alias{Values: x.(AliasValues)} al.SetId(origKey) return al, nil } return nil, utils.ErrNotFound } } var kv struct { Key string Value AliasValues } session, col := ms.conn(colAls) defer session.Close() cCommit := cacheCommit(transactionID) if err = col.Find(bson.M{"key": origKey}).One(&kv); err == nil { al = &Alias{Values: kv.Value} al.SetId(origKey) if err == nil { cache.Set(key, al.Values, cCommit, transactionID) } } else { cache.Set(key, nil, cCommit, transactionID) return nil, utils.ErrNotFound } return }
func (rs *RedisStorage) GetAlias(key string, skipCache bool, transactionID string) (al *Alias, err error) { origKey := key key = utils.ALIASES_PREFIX + key if !skipCache { if x, ok := cache.Get(key); ok { if x != nil { al = &Alias{Values: x.(AliasValues)} al.SetId(origKey) return al, nil } return nil, utils.ErrNotFound } } var values []byte if values, err = rs.Cmd("GET", key).Bytes(); err == nil { al = &Alias{Values: make(AliasValues, 0)} al.SetId(origKey) err = rs.ms.Unmarshal(values, &al.Values) } else { cache.Set(key, nil, cacheCommit(transactionID), transactionID) return nil, utils.ErrNotFound } cache.Set(key, al.Values, cacheCommit(transactionID), transactionID) return }
// Limit will only retrieve the last n items out of history, newest first func (ms *MongoStorage) GetLoadHistory(limit int, skipCache bool, transactionID string) (loadInsts []*utils.LoadInstance, err error) { if limit == 0 { return nil, nil } if !skipCache { if x, ok := cache.Get(utils.LOADINST_KEY); ok { if x != nil { items := x.([]*utils.LoadInstance) if len(items) < limit || limit == -1 { return items, nil } return items[:limit], nil } return nil, utils.ErrNotFound } } var kv struct { Key string Value []*utils.LoadInstance } session, col := ms.conn(colLht) defer session.Close() err = col.Find(bson.M{"key": utils.LOADINST_KEY}).One(&kv) cCommit := cacheCommit(transactionID) if err == nil { loadInsts = kv.Value cache.RemKey(utils.LOADINST_KEY, cCommit, transactionID) cache.Set(utils.LOADINST_KEY, loadInsts, cCommit, transactionID) } if len(loadInsts) < limit || limit == -1 { return loadInsts, nil } return loadInsts[:limit], nil }
func (ms *MongoStorage) GetReverseAlias(reverseID string, skipCache bool, transactionID string) (ids []string, err error) { if !skipCache { if x, ok := cache.Get(utils.REVERSE_ALIASES_PREFIX + reverseID); ok { if x != nil { return x.([]string), nil } return nil, utils.ErrNotFound } } var result struct { Key string Value []string } session, col := ms.conn(colRls) defer session.Close() if err = col.Find(bson.M{"key": reverseID}).One(&result); err == nil { ids = result.Value cache.Set(utils.REVERSE_ALIASES_PREFIX+reverseID, ids, cacheCommit(transactionID), transactionID) } else { cache.Set(utils.REVERSE_ALIASES_PREFIX+reverseID, nil, cacheCommit(transactionID), transactionID) return nil, utils.ErrNotFound } return }
func (ms *MongoStorage) GetActionPlan(key string, skipCache bool, transactionID string) (ats *ActionPlan, err error) { if !skipCache { if x, ok := cache.Get(utils.ACTION_PLAN_PREFIX + key); ok { if x != nil { return x.(*ActionPlan), nil } return nil, utils.ErrNotFound } } var kv struct { Key string Value []byte } session, col := ms.conn(colApl) defer session.Close() err = col.Find(bson.M{"key": key}).One(&kv) if err == nil { b := bytes.NewBuffer(kv.Value) r, err := zlib.NewReader(b) if err != nil { return nil, err } out, err := ioutil.ReadAll(r) if err != nil { return nil, err } r.Close() err = ms.ms.Unmarshal(out, &ats) if err != nil { return nil, err } } cache.Set(utils.ACTION_PLAN_PREFIX+key, ats, cacheCommit(transactionID), transactionID) return }
// GetDestination retrieves a destination with id from tp_db func (rs *RedisStorage) GetDestination(key string, skipCache bool, transactionID string) (dest *Destination, err error) { key = utils.DESTINATION_PREFIX + key if !skipCache { if x, ok := cache.Get(key); ok { if x == nil { return nil, utils.ErrNotFound } return x.(*Destination), nil } } var values []byte if values, err = rs.Cmd("GET", key).Bytes(); err != nil { if err.Error() == "wrong type" { // did not find the destination cache.Set(key, nil, cacheCommit(transactionID), transactionID) err = utils.ErrNotFound } return } b := bytes.NewBuffer(values) r, err := zlib.NewReader(b) if err != nil { return nil, err } out, err := ioutil.ReadAll(r) if err != nil { return nil, err } r.Close() err = rs.ms.Unmarshal(out, &dest) if err != nil { return nil, err } cache.Set(key, dest, cacheCommit(transactionID), transactionID) return }
func (rs *RedisStorage) GetActionPlan(key string, skipCache bool, transactionID string) (ats *ActionPlan, err error) { key = utils.ACTION_PLAN_PREFIX + key if !skipCache { if x, ok := cache.Get(key); ok { if x != nil { return x.(*ActionPlan), nil } return nil, utils.ErrNotFound } } var values []byte if values, err = rs.Cmd("GET", key).Bytes(); err == nil { b := bytes.NewBuffer(values) r, err := zlib.NewReader(b) if err != nil { return nil, err } out, err := ioutil.ReadAll(r) if err != nil { return nil, err } r.Close() ats = &ActionPlan{} err = rs.ms.Unmarshal(out, &ats) } cache.Set(key, ats, cacheCommit(transactionID), transactionID) return }
func (rs *RedisStorage) GetReverseAlias(reverseID string, skipCache bool, transactionID string) (ids []string, err error) { key := utils.REVERSE_ALIASES_PREFIX + reverseID if !skipCache { if x, ok := cache.Get(key); ok { if x == nil { return nil, utils.ErrNotFound } return x.([]string), nil } } if ids, err = rs.Cmd("SMEMBERS", key).List(); err != nil { if err.Error() == "wrong type" { // did not find the destination cache.Set(key, nil, cacheCommit(transactionID), transactionID) err = utils.ErrNotFound } return } if len(ids) == 0 { cache.Set(key, nil, cacheCommit(transactionID), transactionID) err = utils.ErrNotFound return } cache.Set(key, ids, cacheCommit(transactionID), transactionID) return }
func (ms *MongoStorage) GetDerivedChargers(key string, skipCache bool, transactionID string) (dcs *utils.DerivedChargers, err error) { if !skipCache { if x, ok := cache.Get(utils.DERIVEDCHARGERS_PREFIX + key); ok { if x != nil { return x.(*utils.DerivedChargers), nil } return nil, utils.ErrNotFound } } var kv struct { Key string Value *utils.DerivedChargers } session, col := ms.conn(colDcs) defer session.Close() err = col.Find(bson.M{"key": key}).One(&kv) cCommit := cacheCommit(transactionID) if err == nil { dcs = kv.Value } else { cache.Set(utils.DERIVEDCHARGERS_PREFIX+key, nil, cCommit, transactionID) return nil, utils.ErrNotFound } cache.Set(utils.DERIVEDCHARGERS_PREFIX+key, dcs, cCommit, transactionID) return }
func (ms *MongoStorage) GetLCR(key string, skipCache bool, transactionID string) (lcr *LCR, err error) { if !skipCache { if x, ok := cache.Get(utils.LCR_PREFIX + key); ok { if x != nil { return x.(*LCR), nil } return nil, utils.ErrNotFound } } var result struct { Key string Value *LCR } session, col := ms.conn(colLcr) defer session.Close() cCommit := cacheCommit(transactionID) if err = col.Find(bson.M{"key": key}).One(&result); err == nil { lcr = result.Value } else { cache.Set(utils.LCR_PREFIX+key, nil, cCommit, transactionID) return nil, utils.ErrNotFound } cache.Set(utils.LCR_PREFIX+key, lcr, cCommit, transactionID) return }
func (ms *MongoStorage) MatchReqFilterIndex(dbKey, fieldValKey string) (itemIDs utils.StringMap, err error) { fldValSplt := strings.Split(fieldValKey, utils.CONCATENATED_KEY_SEP) if len(fldValSplt) != 2 { return nil, fmt.Errorf("malformed key in query: %s", fldValSplt) } if x, ok := cache.Get(dbKey + fieldValKey); ok { // Attempt to find in cache first if x != nil { return x.(utils.StringMap), nil } return nil, utils.ErrNotFound } session, col := ms.conn(colRFI) defer session.Close() var result struct { Key string Value map[string]map[string]utils.StringMap } fldKey := fmt.Sprintf("value.%s.%s", fldValSplt[0], fldValSplt[1]) if err = col.Find( bson.M{"key": dbKey, fldKey: bson.M{"$exists": true}}).Select( bson.M{fldKey: true}).One(&result); err != nil { if err == mgo.ErrNotFound { err = utils.ErrNotFound cache.Set(dbKey+fieldValKey, nil, true, utils.NonTransactional) } return nil, err } itemIDs = result.Value[fldValSplt[0]][fldValSplt[1]] cache.Set(dbKey+fieldValKey, itemIDs, true, utils.NonTransactional) return }
func (rs *RedisStorage) GetDestination(key string, skipCache bool, transactionID string) (dest *Destination, err error) { key = utils.DESTINATION_PREFIX + key if !skipCache { if x, ok := cache.Get(key); ok { if x != nil { return x.(*Destination), nil } return nil, utils.ErrNotFound } } var values []byte if values, err = rs.Cmd("GET", key).Bytes(); len(values) > 0 && err == nil { b := bytes.NewBuffer(values) r, err := zlib.NewReader(b) if err != nil { return nil, err } out, err := ioutil.ReadAll(r) if err != nil { return nil, err } r.Close() dest = new(Destination) err = rs.ms.Unmarshal(out, dest) if err != nil { cache.Set(key, dest, cacheCommit(transactionID), transactionID) } } else { cache.Set(key, nil, cacheCommit(transactionID), transactionID) return nil, err } return }
func (rs *RedisStorage) GetAlias(key string, skipCache bool, transactionID string) (al *Alias, err error) { cacheKey := utils.ALIASES_PREFIX + key cCommit := cacheCommit(transactionID) if !skipCache { if x, ok := cache.Get(cacheKey); ok { if x == nil { return nil, utils.ErrNotFound } al = x.(*Alias) return } } var values []byte if values, err = rs.Cmd("GET", cacheKey).Bytes(); err != nil { if err.Error() == "wrong type" { // did not find the destination cache.Set(key, nil, cCommit, transactionID) err = utils.ErrNotFound } return } al = &Alias{Values: make(AliasValues, 0)} al.SetId(key) if err = rs.ms.Unmarshal(values, &al.Values); err != nil { return } cache.Set(cacheKey, al, cCommit, transactionID) return }
func (ms *MapStorage) GetAlias(key string, skipCache bool, transactionID string) (al *Alias, err error) { ms.mu.RLock() defer ms.mu.RUnlock() origKey := key key = utils.ALIASES_PREFIX + key if !skipCache { if x, ok := cache.Get(key); ok { if x != nil { al = &Alias{Values: x.(AliasValues)} al.SetId(origKey) return al, nil } return nil, utils.ErrNotFound } } cCommit := cacheCommit(transactionID) if values, ok := ms.dict[key]; ok { al = &Alias{Values: make(AliasValues, 0)} al.SetId(key[len(utils.ALIASES_PREFIX):]) err = ms.ms.Unmarshal(values, &al.Values) if err == nil { cache.Set(key, al.Values, cCommit, transactionID) } } else { cache.Set(key, nil, cCommit, transactionID) return nil, utils.ErrNotFound } return al, nil }
func (ms *MapStorage) GetDestination(key string, skipCache bool, transactionID string) (dest *Destination, err error) { ms.mu.RLock() defer ms.mu.RUnlock() cCommit := cacheCommit(transactionID) key = utils.DESTINATION_PREFIX + key if !skipCache { if x, ok := cache.Get(key); ok { if x != nil { return x.(*Destination), nil } return nil, utils.ErrNotFound } } if values, ok := ms.dict[key]; ok { b := bytes.NewBuffer(values) r, err := zlib.NewReader(b) if err != nil { return nil, err } out, err := ioutil.ReadAll(r) if err != nil { return nil, err } r.Close() dest = new(Destination) err = ms.ms.Unmarshal(out, dest) if err != nil { cache.Set(key, dest, cCommit, transactionID) } } else { cache.Set(key, nil, cCommit, transactionID) return nil, utils.ErrNotFound } return }
func (rs *RedisStorage) GetResourceLimit(id string, skipCache bool, transactionID string) (rl *ResourceLimit, err error) { key := utils.ResourceLimitsPrefix + id if !skipCache { if x, ok := cache.Get(key); ok { if x == nil { return nil, utils.ErrNotFound } return x.(*ResourceLimit), nil } } var values []byte if values, err = rs.Cmd("GET", key).Bytes(); err != nil { if err.Error() == "wrong type" { // did not find the destination cache.Set(key, nil, cacheCommit(transactionID), transactionID) err = utils.ErrNotFound } return } if err = rs.ms.Unmarshal(values, &rl); err != nil { return } for _, fltr := range rl.Filters { if err = fltr.CompileValues(); err != nil { return } } cache.Set(key, rl, cacheCommit(transactionID), transactionID) return }
func (ms *MapStorage) GetRatingPlan(key string, skipCache bool, transactionID string) (rp *RatingPlan, err error) { ms.mu.RLock() defer ms.mu.RUnlock() key = utils.RATING_PLAN_PREFIX + key if !skipCache { if x, ok := cache.Get(key); ok { if x != nil { return x.(*RatingPlan), nil } return nil, utils.ErrNotFound } } cCommit := cacheCommit(transactionID) if values, ok := ms.dict[key]; ok { b := bytes.NewBuffer(values) r, err := zlib.NewReader(b) if err != nil { return nil, err } out, err := ioutil.ReadAll(r) if err != nil { return nil, err } r.Close() rp = new(RatingPlan) err = ms.ms.Unmarshal(out, rp) } else { cache.Set(key, nil, cCommit, transactionID) return nil, utils.ErrNotFound } cache.Set(key, rp, cCommit, transactionID) return }
func (ms *MapStorage) MatchReqFilterIndex(dbKey, fieldValKey string) (itemIDs utils.StringMap, err error) { ms.mu.RLock() defer ms.mu.RUnlock() if x, ok := cache.Get(dbKey + fieldValKey); ok { // Attempt to find in cache first if x != nil { return x.(utils.StringMap), nil } return nil, utils.ErrNotFound } // Not found in cache, check in DB values, ok := ms.dict[dbKey] if !ok { cache.Set(dbKey+fieldValKey, nil, true, utils.NonTransactional) return nil, utils.ErrNotFound } var indexes map[string]map[string]utils.StringMap if err = ms.ms.Unmarshal(values, &indexes); err != nil { return nil, err } keySplt := strings.Split(fieldValKey, ":") if _, hasIt := indexes[keySplt[0]]; hasIt { itemIDs = indexes[keySplt[0]][keySplt[1]] } cache.Set(dbKey+fieldValKey, itemIDs, true, utils.NonTransactional) return }
func (ms *MapStorage) GetResourceLimit(id string, skipCache bool, transactionID string) (rl *ResourceLimit, err error) { ms.mu.RLock() defer ms.mu.RUnlock() key := utils.ResourceLimitsPrefix + id if !skipCache { if x, ok := cache.Get(key); ok { if x != nil { return x.(*ResourceLimit), nil } return nil, utils.ErrNotFound } } values, ok := ms.dict[key] if !ok { cache.Set(key, nil, cacheCommit(transactionID), transactionID) return nil, utils.ErrNotFound } err = ms.ms.Unmarshal(values, &rl) if err != nil { return nil, err } for _, fltr := range rl.Filters { if err := fltr.CompileValues(); err != nil { return nil, err } } cache.Set(key, rl, cacheCommit(transactionID), transactionID) return }
func testOnStorITCacheReverseAlias(t *testing.T) { als := &Alias{ Direction: "*out", Tenant: "itsyscom.com", Category: "call", Account: "dan", Subject: "dan", Context: "*rating", Values: AliasValues{ &AliasValue{ DestinationId: "EU", Pairs: AliasPairs{ "Account": map[string]string{ "dan": "dan1", "rif": "rif1", }, "Calling": map[string]string{ "11234": "2234", }, }, Weight: 10, }, &AliasValue{ DestinationId: "US", Pairs: AliasPairs{"Account": map[string]string{"dan": "dan2"}}, Weight: 20, }, }, } if err := onStor.SetReverseAlias(als, utils.NonTransactional); err != nil { t.Error(err) } rvAlsID := strings.Join([]string{als.Values[1].Pairs["Account"]["dan"], "Account", als.Context}, "") if _, hasIt := cache.Get(utils.REVERSE_ALIASES_PREFIX + rvAlsID); hasIt { t.Error("Already in cache") } if err := onStor.CacheDataFromDB(utils.REVERSE_ALIASES_PREFIX, []string{rvAlsID}, false); err != nil { t.Error(err) } eRvrsAls := []string{utils.ConcatenatedKey(als.GetId(), als.Values[1].DestinationId)} if itm, hasIt := cache.Get(utils.REVERSE_ALIASES_PREFIX + rvAlsID); !hasIt { t.Error("Did not cache") } else if rcv := itm.([]string); !reflect.DeepEqual(eRvrsAls, rcv) { t.Errorf("Expecting: %+v, received: %+v", eRvrsAls, rcv) } }
func testOnStorITCacheAlias(t *testing.T) { als := &Alias{ Direction: "*out", Tenant: "cgrates.org", Category: "call", Account: "dan", Subject: "dan", Context: "*rating", Values: AliasValues{ &AliasValue{ DestinationId: "EU_LANDLINE", Pairs: AliasPairs{ "Subject": map[string]string{ "dan": "dan1", "rif": "rif1", }, "Cli": map[string]string{ "0723": "0724", }, }, Weight: 10, }, &AliasValue{ DestinationId: "GLOBAL1", Pairs: AliasPairs{"Subject": map[string]string{"dan": "dan2"}}, Weight: 20, }, }, } if err := onStor.SetAlias(als, utils.NonTransactional); err != nil { t.Error(err) } if _, hasIt := cache.Get(utils.ALIASES_PREFIX + als.GetId()); hasIt { t.Error("Already in cache") } if err := onStor.CacheDataFromDB(utils.ALIASES_PREFIX, []string{als.GetId()}, false); err != nil { t.Error(err) } if itm, hasIt := cache.Get(utils.ALIASES_PREFIX + als.GetId()); !hasIt { t.Error("Did not cache") } else if rcv := itm.(*Alias); !reflect.DeepEqual(als, rcv) { t.Errorf("Expecting: %+v, received: %+v", als, rcv) } }