diff --git a/engine/datamanager.go b/engine/datamanager.go index adba745e8..d05a31feb 100644 --- a/engine/datamanager.go +++ b/engine/datamanager.go @@ -112,3 +112,24 @@ func (dm *DataManager) RemoveFilter(tenant, id, transactionID string) (err error cacheCommit(transactionID), transactionID) return } + +func (dm *DataManager) GetThreshold(tenant, id string, skipCache bool, transactionID string) (th *Threshold, err error) { + key := utils.ThresholdPrefix + utils.ConcatenatedKey(tenant, id) + if !skipCache { + if x, ok := cache.Get(key); ok { + if x == nil { + return nil, utils.ErrNotFound + } + return x.(*Threshold), nil + } + } + th, err = dm.dataDB.GetThresholdDrv(tenant, id) + if err != nil { + if err == utils.ErrNotFound { + cache.Set(key, nil, cacheCommit(transactionID), transactionID) + } + return nil, err + } + cache.Set(key, th, cacheCommit(transactionID), transactionID) + return +} diff --git a/engine/onstor_it_test.go b/engine/onstor_it_test.go index a705959eb..546049fac 100644 --- a/engine/onstor_it_test.go +++ b/engine/onstor_it_test.go @@ -2096,18 +2096,18 @@ func testOnStorITCRUDThreshold(t *testing.T) { ID: "TH1", Snooze: time.Date(2016, 10, 1, 0, 0, 0, 0, time.UTC).Local(), } - if _, rcvErr := onStor.DataDB().GetThreshold("cgrates.org", "TH1", true, utils.NonTransactional); rcvErr != nil && rcvErr != utils.ErrNotFound { + if _, rcvErr := onStor.GetThreshold("cgrates.org", "TH1", true, utils.NonTransactional); rcvErr != nil && rcvErr != utils.ErrNotFound { t.Error(rcvErr) } if err := onStor.DataDB().SetThreshold(res); err != nil { t.Error(err) } - if rcv, err := onStor.DataDB().GetThreshold("cgrates.org", "TH1", true, utils.NonTransactional); err != nil { + if rcv, err := onStor.GetThreshold("cgrates.org", "TH1", true, utils.NonTransactional); err != nil { t.Error(err) } else if !(reflect.DeepEqual(res, rcv)) { t.Errorf("Expecting: %v, received: %v", res, rcv) } - if rcv, err := onStor.DataDB().GetThreshold("cgrates.org", "TH1", false, utils.NonTransactional); err != nil { + if rcv, err := onStor.GetThreshold("cgrates.org", "TH1", false, utils.NonTransactional); err != nil { t.Error(err) } else if !reflect.DeepEqual(res, rcv) { t.Errorf("Expecting: %v, received: %v", res, rcv) @@ -2115,7 +2115,7 @@ func testOnStorITCRUDThreshold(t *testing.T) { if err := onStor.DataDB().RemoveThreshold(res.Tenant, res.ID, utils.NonTransactional); err != nil { t.Error(err) } - if _, rcvErr := onStor.DataDB().GetThreshold(res.Tenant, res.ID, true, utils.NonTransactional); rcvErr != utils.ErrNotFound { + if _, rcvErr := onStor.GetThreshold(res.Tenant, res.ID, true, utils.NonTransactional); rcvErr != utils.ErrNotFound { t.Error(rcvErr) } } diff --git a/engine/storage_interface.go b/engine/storage_interface.go index 4357739ec..deca6201e 100755 --- a/engine/storage_interface.go +++ b/engine/storage_interface.go @@ -123,7 +123,7 @@ type DataDB interface { GetThresholdProfile(tenant string, ID string, skipCache bool, transID string) (tp *ThresholdProfile, err error) SetThresholdProfile(tp *ThresholdProfile) (err error) RemThresholdProfile(tenant, id, transactionID string) (err error) - GetThreshold(string, string, bool, string) (*Threshold, error) + GetThresholdDrv(string, string) (*Threshold, error) SetThreshold(*Threshold) error RemoveThreshold(string, string, string) error GetFilterDrv(string, string) (*Filter, error) diff --git a/engine/storage_map.go b/engine/storage_map.go index a82009232..d135c16f3 100755 --- a/engine/storage_map.go +++ b/engine/storage_map.go @@ -1624,28 +1624,18 @@ func (ms *MapStorage) RemThresholdProfile(tenant, id, transactionID string) (err return } -func (ms *MapStorage) GetThreshold(tenant, id string, skipCache bool, transactionID string) (r *Threshold, err error) { +func (ms *MapStorage) GetThresholdDrv(tenant, id string) (r *Threshold, err error) { ms.mu.RLock() defer ms.mu.RUnlock() key := utils.ThresholdPrefix + utils.ConcatenatedKey(tenant, id) - if !skipCache { - if x, ok := cache.Get(key); ok { - if x != nil { - return x.(*Threshold), 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, r) if err != nil { return nil, err } - cache.Set(key, r, cacheCommit(transactionID), transactionID) return } diff --git a/engine/storage_mongo_datadb.go b/engine/storage_mongo_datadb.go index 0b91975e5..d713356c0 100755 --- a/engine/storage_mongo_datadb.go +++ b/engine/storage_mongo_datadb.go @@ -543,7 +543,7 @@ func (ms *MongoStorage) CacheDataFromDB(prfx string, ids []string, mustBeCached _, err = ms.GetThresholdProfile(tntID.Tenant, tntID.ID, true, utils.NonTransactional) case utils.ThresholdPrefix: tntID := utils.NewTenantID(dataID) - _, err = ms.GetThreshold(tntID.Tenant, tntID.ID, true, utils.NonTransactional) + _, err = ms.GetThresholdDrv(tntID.Tenant, tntID.ID) case utils.FilterPrefix: tntID := utils.NewTenantID(dataID) _, err = ms.GetFilterDrv(tntID.Tenant, tntID.ID) @@ -2211,27 +2211,15 @@ func (ms *MongoStorage) RemThresholdProfile(tenant, id, transactionID string) (e return } -func (ms *MongoStorage) GetThreshold(tenant, id string, skipCache bool, transactionID string) (r *Threshold, err error) { - key := utils.ThresholdPrefix + utils.ConcatenatedKey(tenant, id) - if !skipCache { - if x, ok := cache.Get(key); ok { - if x == nil { - return nil, utils.ErrNotFound - } - return x.(*Threshold), nil - } - } +func (ms *MongoStorage) GetThresholdDrv(tenant, id string) (r *Threshold, err error) { session, col := ms.conn(colThs) defer session.Close() - r = new(Threshold) - if err = col.Find(bson.M{"tenant": tenant, "id": id}).One(r); err != nil { + if err = col.Find(bson.M{"tenant": tenant, "id": id}).One(&r); err != nil { if err == mgo.ErrNotFound { err = utils.ErrNotFound - cache.Set(key, nil, cacheCommit(transactionID), transactionID) } return nil, err } - cache.Set(key, r, cacheCommit(transactionID), transactionID) return } diff --git a/engine/storage_redis.go b/engine/storage_redis.go index dd7f4c934..f1c223582 100755 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -1734,20 +1734,11 @@ func (rs *RedisStorage) RemThresholdProfile(tenant, id, transactionID string) (e return } -func (rs *RedisStorage) GetThreshold(tenant, id string, skipCache bool, transactionID string) (r *Threshold, err error) { +func (rs *RedisStorage) GetThresholdDrv(tenant, id string) (r *Threshold, err error) { key := utils.ThresholdPrefix + utils.ConcatenatedKey(tenant, id) - if !skipCache { - if x, ok := cache.Get(key); ok { - if x == nil { - return nil, utils.ErrNotFound - } - return x.(*Threshold), nil - } - } var values []byte if values, err = rs.Cmd("GET", key).Bytes(); err != nil { if err == redis.ErrRespNil { // did not find the destination - cache.Set(key, nil, cacheCommit(transactionID), transactionID) err = utils.ErrNotFound } return @@ -1755,7 +1746,6 @@ func (rs *RedisStorage) GetThreshold(tenant, id string, skipCache bool, transact if err = rs.ms.Unmarshal(values, &r); err != nil { return } - cache.Set(key, r, cacheCommit(transactionID), transactionID) return } diff --git a/engine/thresholds.go b/engine/thresholds.go index 42654bd45..aa53b6ca4 100644 --- a/engine/thresholds.go +++ b/engine/thresholds.go @@ -258,7 +258,7 @@ func (tS *ThresholdService) matchingThresholdsForEvent(ev *ThresholdEvent) (ts T } lockThreshold := utils.ThresholdPrefix + tPrfl.TenantID() guardian.Guardian.GuardIDs(config.CgrConfig().LockingTimeout, lockThreshold) - t, err := tS.dm.DataDB().GetThreshold(tPrfl.Tenant, tPrfl.ID, false, "") + t, err := tS.dm.GetThreshold(tPrfl.Tenant, tPrfl.ID, false, "") if err != nil { guardian.Guardian.UnguardIDs(lockThreshold) return nil, err @@ -376,7 +376,7 @@ func (tS *ThresholdService) V1GetThresholdIDs(tenant string, tIDs *[]string) (er // V1GetThreshold retrieves a Threshold func (tS *ThresholdService) V1GetThreshold(tntID *utils.TenantID, t *Threshold) (err error) { - if thd, err := tS.dm.DataDB().GetThreshold(tntID.Tenant, tntID.ID, false, ""); err != nil { + if thd, err := tS.dm.GetThreshold(tntID.Tenant, tntID.ID, false, ""); err != nil { return err } else { *t = *thd