mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-25 00:58:45 +05:00
LCR in CacheDataFromDB
This commit is contained in:
@@ -52,6 +52,7 @@ var sTestsOnStorIT = []func(t *testing.T){
|
||||
testOnStorITCacheActionPlan,
|
||||
testOnStorITCacheSharedGroup,
|
||||
testOnStorITCacheDerivedChargers,
|
||||
testOnStorITCacheLCR,
|
||||
}
|
||||
|
||||
func TestOnStorITRedisConnect(t *testing.T) {
|
||||
@@ -504,3 +505,48 @@ func testOnStorITCacheDerivedChargers(t *testing.T) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", dcs, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func testOnStorITCacheLCR(t *testing.T) {
|
||||
lcr := &LCR{
|
||||
Tenant: "cgrates.org",
|
||||
Category: "call",
|
||||
Direction: "*out",
|
||||
Account: "*any",
|
||||
Subject: "*any",
|
||||
Activations: []*LCRActivation{
|
||||
&LCRActivation{
|
||||
ActivationTime: time.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC).Local(),
|
||||
Entries: []*LCREntry{
|
||||
&LCREntry{
|
||||
DestinationId: "EU_LANDLINE",
|
||||
RPCategory: "LCR_STANDARD",
|
||||
Strategy: "*static",
|
||||
StrategyParams: "ivo;dan;rif",
|
||||
Weight: 10,
|
||||
},
|
||||
&LCREntry{
|
||||
DestinationId: "*any",
|
||||
RPCategory: "LCR_STANDARD",
|
||||
Strategy: "*lowest_cost",
|
||||
StrategyParams: "",
|
||||
Weight: 20,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
if err := onStor.SetLCR(lcr, utils.NonTransactional); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if _, hasIt := cache.Get(utils.LCR_PREFIX + lcr.GetId()); hasIt {
|
||||
t.Error("Already in cache")
|
||||
}
|
||||
if err := onStor.CacheDataFromDB(utils.LCR_PREFIX, []string{lcr.GetId()}, false); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if itm, hasIt := cache.Get(utils.LCR_PREFIX + lcr.GetId()); !hasIt {
|
||||
t.Error("Did not cache")
|
||||
} else if rcv := itm.(*LCR); !reflect.DeepEqual(lcr, rcv) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", lcr, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -480,7 +480,8 @@ func (ms *MongoStorage) CacheDataFromDB(prfx string, ids []string, mustBeCached
|
||||
utils.ACTION_PREFIX,
|
||||
utils.ACTION_PLAN_PREFIX,
|
||||
utils.SHARED_GROUP_PREFIX,
|
||||
utils.DERIVEDCHARGERS_PREFIX}, prfx) {
|
||||
utils.DERIVEDCHARGERS_PREFIX,
|
||||
utils.LCR_PREFIX}, prfx) {
|
||||
return utils.NewCGRError(utils.REDIS,
|
||||
utils.MandatoryIEMissingCaps,
|
||||
utils.UnsupportedCachePrefix,
|
||||
@@ -517,6 +518,8 @@ func (ms *MongoStorage) CacheDataFromDB(prfx string, ids []string, mustBeCached
|
||||
_, err = ms.GetSharedGroup(dataID, false, utils.NonTransactional)
|
||||
case utils.DERIVEDCHARGERS_PREFIX:
|
||||
_, err = ms.GetDerivedChargers(dataID, false, utils.NonTransactional)
|
||||
case utils.LCR_PREFIX:
|
||||
_, err = ms.GetLCR(dataID, false, utils.NonTransactional)
|
||||
}
|
||||
if err != nil {
|
||||
return utils.NewCGRError(utils.MONGO,
|
||||
@@ -747,12 +750,13 @@ func (ms *MongoStorage) RemoveRatingProfile(key, transactionID string) error {
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) GetLCR(key string, skipCache bool, transactionID string) (lcr *LCR, err error) {
|
||||
cacheKey := utils.LCR_PREFIX + key
|
||||
if !skipCache {
|
||||
if x, ok := cache.Get(utils.LCR_PREFIX + key); ok {
|
||||
if x != nil {
|
||||
return x.(*LCR), nil
|
||||
if x, ok := cache.Get(cacheKey); ok {
|
||||
if x == nil {
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
return nil, utils.ErrNotFound
|
||||
return x.(*LCR), nil
|
||||
}
|
||||
}
|
||||
var result struct {
|
||||
@@ -762,25 +766,28 @@ func (ms *MongoStorage) GetLCR(key string, skipCache bool, transactionID string)
|
||||
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
|
||||
if err = col.Find(bson.M{"key": key}).One(&result); err != nil {
|
||||
if err == mgo.ErrNotFound {
|
||||
cache.Set(cacheKey, nil, cacheCommit(transactionID), transactionID)
|
||||
err = utils.ErrNotFound
|
||||
}
|
||||
return
|
||||
}
|
||||
cache.Set(utils.LCR_PREFIX+key, lcr, cCommit, transactionID)
|
||||
cache.Set(cacheKey, result.Value, cCommit, transactionID)
|
||||
return
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) SetLCR(lcr *LCR, transactionID string) error {
|
||||
func (ms *MongoStorage) SetLCR(lcr *LCR, transactionID string) (err error) {
|
||||
session, col := ms.conn(colLcr)
|
||||
defer session.Close()
|
||||
_, err := col.Upsert(bson.M{"key": lcr.GetId()}, &struct {
|
||||
if _, err = col.Upsert(bson.M{"key": lcr.GetId()}, &struct {
|
||||
Key string
|
||||
Value *LCR
|
||||
}{lcr.GetId(), lcr})
|
||||
}{lcr.GetId(), lcr}); err != nil {
|
||||
return
|
||||
}
|
||||
cache.RemKey(utils.LCR_PREFIX+lcr.GetId(), cacheCommit(transactionID), transactionID)
|
||||
return err
|
||||
return
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) GetDestination(key string, skipCache bool, transactionID string) (result *Destination, err error) {
|
||||
|
||||
@@ -280,7 +280,8 @@ func (rs *RedisStorage) CacheDataFromDB(prfx string, ids []string, mustBeCached
|
||||
utils.ACTION_PREFIX,
|
||||
utils.ACTION_PLAN_PREFIX,
|
||||
utils.SHARED_GROUP_PREFIX,
|
||||
utils.DERIVEDCHARGERS_PREFIX}, prfx) {
|
||||
utils.DERIVEDCHARGERS_PREFIX,
|
||||
utils.LCR_PREFIX}, prfx) {
|
||||
return utils.NewCGRError(utils.REDIS,
|
||||
utils.MandatoryIEMissingCaps,
|
||||
utils.UnsupportedCachePrefix,
|
||||
@@ -317,6 +318,8 @@ func (rs *RedisStorage) CacheDataFromDB(prfx string, ids []string, mustBeCached
|
||||
_, err = rs.GetSharedGroup(dataID, false, utils.NonTransactional)
|
||||
case utils.DERIVEDCHARGERS_PREFIX:
|
||||
_, err = rs.GetDerivedChargers(dataID, false, utils.NonTransactional)
|
||||
case utils.LCR_PREFIX:
|
||||
_, err = rs.GetLCR(dataID, false, utils.NonTransactional)
|
||||
}
|
||||
if err != nil {
|
||||
return utils.NewCGRError(utils.REDIS,
|
||||
@@ -463,28 +466,36 @@ func (rs *RedisStorage) GetLCR(key string, skipCache bool, transactionID string)
|
||||
key = utils.LCR_PREFIX + key
|
||||
if !skipCache {
|
||||
if x, ok := cache.Get(key); ok {
|
||||
|
||||
if x != nil {
|
||||
return x.(*LCR), nil
|
||||
if x == nil {
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
return nil, utils.ErrNotFound
|
||||
return x.(*LCR), nil
|
||||
}
|
||||
}
|
||||
var values []byte
|
||||
if values, err = rs.Cmd("GET", key).Bytes(); err == nil {
|
||||
err = rs.ms.Unmarshal(values, &lcr)
|
||||
} else {
|
||||
cache.Set(key, nil, cacheCommit(transactionID), transactionID)
|
||||
return nil, utils.ErrNotFound
|
||||
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, &lcr); err != nil {
|
||||
return
|
||||
}
|
||||
cache.Set(key, lcr, cacheCommit(transactionID), transactionID)
|
||||
return
|
||||
}
|
||||
|
||||
func (rs *RedisStorage) SetLCR(lcr *LCR, transactionID string) (err error) {
|
||||
result, err := rs.ms.Marshal(lcr)
|
||||
var result []byte
|
||||
if result, err = rs.ms.Marshal(lcr); err != nil {
|
||||
return
|
||||
}
|
||||
key := utils.LCR_PREFIX + lcr.GetId()
|
||||
err = rs.Cmd("SET", key, result).Err
|
||||
if err = rs.Cmd("SET", key, result).Err; err != nil {
|
||||
return
|
||||
}
|
||||
cache.RemKey(key, cacheCommit(transactionID), transactionID)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user