mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-23 08:08:45 +05:00
SharedGroups in CacheDataFromDB
This commit is contained in:
@@ -50,6 +50,7 @@ var sTestsOnStorIT = []func(t *testing.T){
|
||||
testOnStorITCacheRatingProfile,
|
||||
testOnStorITCacheActions,
|
||||
testOnStorITCacheActionPlan,
|
||||
testOnStorITCacheSharedGroup,
|
||||
}
|
||||
|
||||
func TestOnStorITRedisConnect(t *testing.T) {
|
||||
@@ -445,3 +446,30 @@ func testOnStorITCacheActionPlan(t *testing.T) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", ap, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -478,7 +478,8 @@ func (ms *MongoStorage) CacheDataFromDB(prfx string, ids []string, mustBeCached
|
||||
utils.RATING_PLAN_PREFIX,
|
||||
utils.RATING_PROFILE_PREFIX,
|
||||
utils.ACTION_PREFIX,
|
||||
utils.ACTION_PLAN_PREFIX}, prfx) {
|
||||
utils.ACTION_PLAN_PREFIX,
|
||||
utils.SHARED_GROUP_PREFIX}, prfx) {
|
||||
return utils.NewCGRError(utils.REDIS,
|
||||
utils.MandatoryIEMissingCaps,
|
||||
utils.UnsupportedCachePrefix,
|
||||
@@ -511,6 +512,8 @@ func (ms *MongoStorage) CacheDataFromDB(prfx string, ids []string, mustBeCached
|
||||
_, err = ms.GetActions(dataID, false, utils.NonTransactional)
|
||||
case utils.ACTION_PLAN_PREFIX:
|
||||
_, err = ms.GetActionPlan(dataID, false, utils.NonTransactional)
|
||||
case utils.SHARED_GROUP_PREFIX:
|
||||
_, err = ms.GetSharedGroup(dataID, false, utils.NonTransactional)
|
||||
}
|
||||
if err != nil {
|
||||
return utils.NewCGRError(utils.MONGO,
|
||||
@@ -1018,30 +1021,35 @@ func (ms *MongoStorage) RemoveActions(key string, transactionID string) error {
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) GetSharedGroup(key string, skipCache bool, transactionID string) (sg *SharedGroup, err error) {
|
||||
cacheKey := utils.SHARED_GROUP_PREFIX + key
|
||||
if !skipCache {
|
||||
if x, ok := cache.Get(utils.SHARED_GROUP_PREFIX + key); ok {
|
||||
if x != nil {
|
||||
return x.(*SharedGroup), nil
|
||||
if x, ok := cache.Get(cacheKey); ok {
|
||||
if x == nil {
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
return nil, utils.ErrNotFound
|
||||
return x.(*SharedGroup), nil
|
||||
}
|
||||
}
|
||||
session, col := ms.conn(colShg)
|
||||
defer session.Close()
|
||||
sg = &SharedGroup{}
|
||||
err = col.Find(bson.M{"id": key}).One(sg)
|
||||
if err == nil {
|
||||
cache.Set(utils.SHARED_GROUP_PREFIX+key, sg, cacheCommit(transactionID), transactionID)
|
||||
} else {
|
||||
cache.Set(utils.SHARED_GROUP_PREFIX+key, nil, cacheCommit(transactionID), transactionID)
|
||||
if err = col.Find(bson.M{"id": key}).One(sg); err != nil {
|
||||
if err == mgo.ErrNotFound {
|
||||
cache.Set(cacheKey, nil, cacheCommit(transactionID), transactionID)
|
||||
err = utils.ErrNotFound
|
||||
}
|
||||
return
|
||||
}
|
||||
cache.Set(cacheKey, sg, cacheCommit(transactionID), transactionID)
|
||||
return
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) SetSharedGroup(sg *SharedGroup, transactionID string) (err error) {
|
||||
session, col := ms.conn(colShg)
|
||||
defer session.Close()
|
||||
_, err = col.Upsert(bson.M{"id": sg.Id}, sg)
|
||||
if _, err = col.Upsert(bson.M{"id": sg.Id}, sg); err != nil {
|
||||
return
|
||||
}
|
||||
cache.RemKey(utils.SHARED_GROUP_PREFIX+sg.Id, cacheCommit(transactionID), transactionID)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -278,7 +278,8 @@ func (rs *RedisStorage) CacheDataFromDB(prfx string, ids []string, mustBeCached
|
||||
utils.RATING_PLAN_PREFIX,
|
||||
utils.RATING_PROFILE_PREFIX,
|
||||
utils.ACTION_PREFIX,
|
||||
utils.ACTION_PLAN_PREFIX}, prfx) {
|
||||
utils.ACTION_PLAN_PREFIX,
|
||||
utils.SHARED_GROUP_PREFIX}, prfx) {
|
||||
return utils.NewCGRError(utils.REDIS,
|
||||
utils.MandatoryIEMissingCaps,
|
||||
utils.UnsupportedCachePrefix,
|
||||
@@ -311,6 +312,8 @@ func (rs *RedisStorage) CacheDataFromDB(prfx string, ids []string, mustBeCached
|
||||
_, err = rs.GetActions(dataID, false, utils.NonTransactional)
|
||||
case utils.ACTION_PLAN_PREFIX:
|
||||
_, err = rs.GetActionPlan(dataID, false, utils.NonTransactional)
|
||||
case utils.SHARED_GROUP_PREFIX:
|
||||
_, err = rs.GetSharedGroup(dataID, false, utils.NonTransactional)
|
||||
}
|
||||
if err != nil {
|
||||
return utils.NewCGRError(utils.REDIS,
|
||||
@@ -694,22 +697,32 @@ func (rs *RedisStorage) GetSharedGroup(key string, skipCache bool, transactionID
|
||||
key = utils.SHARED_GROUP_PREFIX + key
|
||||
if !skipCache {
|
||||
if x, ok := cache.Get(key); ok {
|
||||
if x != nil {
|
||||
return x.(*SharedGroup), nil
|
||||
if x == nil {
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
return nil, utils.ErrNotFound
|
||||
return x.(*SharedGroup), nil
|
||||
}
|
||||
}
|
||||
var values []byte
|
||||
if values, err = rs.Cmd("GET", key).Bytes(); err == nil {
|
||||
err = rs.ms.Unmarshal(values, &sg)
|
||||
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, &sg); err != nil {
|
||||
return
|
||||
}
|
||||
cache.Set(key, sg, cacheCommit(transactionID), transactionID)
|
||||
return
|
||||
}
|
||||
|
||||
func (rs *RedisStorage) SetSharedGroup(sg *SharedGroup, transactionID string) (err error) {
|
||||
result, err := rs.ms.Marshal(sg)
|
||||
var result []byte
|
||||
if result, err = rs.ms.Marshal(sg); err != nil {
|
||||
return
|
||||
}
|
||||
err = rs.Cmd("SET", utils.SHARED_GROUP_PREFIX+sg.Id, result).Err
|
||||
cache.RemKey(utils.SHARED_GROUP_PREFIX+sg.Id, cacheCommit(transactionID), transactionID)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user