mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
clean reverse aliases on reload
This commit is contained in:
@@ -115,6 +115,29 @@ func (al *Alias) SetId(id string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (al *Alias) SetReverseCache() {
|
||||
for _, value := range al.Values {
|
||||
for target, pairs := range value.Pairs {
|
||||
for _, alias := range pairs {
|
||||
rKey := strings.Join([]string{utils.REVERSE_ALIASES_PREFIX, alias, target, al.Context}, "")
|
||||
cache2go.Push(rKey, utils.ConcatenatedKey(al.GetId(), value.DestinationId))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (al *Alias) RemoveReverseCache() {
|
||||
for _, value := range al.Values {
|
||||
tmpKey := utils.ConcatenatedKey(al.GetId(), value.DestinationId)
|
||||
for target, pairs := range value.Pairs {
|
||||
for _, alias := range pairs {
|
||||
rKey := utils.REVERSE_ALIASES_PREFIX + alias + target + al.Context
|
||||
cache2go.Pop(rKey, tmpKey)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type AttrMatchingAlias struct {
|
||||
Destination string
|
||||
Direction string
|
||||
|
||||
@@ -232,6 +232,13 @@ func (ms *MapStorage) cacheAccounting(alsKeys []string) error {
|
||||
}
|
||||
for k, _ := range ms.dict {
|
||||
if strings.HasPrefix(k, utils.ALIASES_PREFIX) {
|
||||
// check if it already exists
|
||||
// to remove reverse cache keys
|
||||
if avs, err := cache2go.Get(k); err == nil && avs != nil {
|
||||
al := &Alias{Values: avs.(AliasValues)}
|
||||
al.SetId(k[len(utils.ALIASES_PREFIX):])
|
||||
al.RemoveReverseCache()
|
||||
}
|
||||
cache2go.RemKey(k)
|
||||
if _, err := ms.GetAlias(k[len(utils.ALIASES_PREFIX):], true); err != nil {
|
||||
cache2go.RollbackTransaction()
|
||||
@@ -574,7 +581,6 @@ func (ms *MapStorage) SetAlias(al *Alias) error {
|
||||
}
|
||||
|
||||
func (ms *MapStorage) GetAlias(key string, skipCache bool) (al *Alias, err error) {
|
||||
origKey := key
|
||||
key = utils.ALIASES_PREFIX + key
|
||||
if !skipCache {
|
||||
if x, err := cache2go.Get(key); err == nil {
|
||||
@@ -591,14 +597,7 @@ func (ms *MapStorage) GetAlias(key string, skipCache bool) (al *Alias, err error
|
||||
err = ms.ms.Unmarshal(values, &al.Values)
|
||||
if err == nil {
|
||||
cache2go.Cache(key, al.Values)
|
||||
for _, value := range al.Values {
|
||||
for target, pairs := range value.Pairs {
|
||||
for _, alias := range pairs {
|
||||
rKey := strings.Join([]string{utils.REVERSE_ALIASES_PREFIX, alias, target, al.Context}, "")
|
||||
cache2go.Push(rKey, utils.ConcatenatedKey(origKey, value.DestinationId))
|
||||
}
|
||||
}
|
||||
}
|
||||
al.SetReverseCache()
|
||||
}
|
||||
} else {
|
||||
return nil, utils.ErrNotFound
|
||||
@@ -609,23 +608,15 @@ func (ms *MapStorage) GetAlias(key string, skipCache bool) (al *Alias, err error
|
||||
func (ms *MapStorage) RemoveAlias(key string) error {
|
||||
al := &Alias{}
|
||||
al.SetId(key)
|
||||
origKey := key
|
||||
key = utils.ALIASES_PREFIX + key
|
||||
aliasValues := make(AliasValues, 0)
|
||||
if values, ok := ms.dict[key]; ok {
|
||||
ms.ms.Unmarshal(values, &aliasValues)
|
||||
}
|
||||
al.Values = aliasValues
|
||||
delete(ms.dict, key)
|
||||
for _, value := range aliasValues {
|
||||
tmpKey := utils.ConcatenatedKey(origKey, value.DestinationId)
|
||||
for target, pairs := range value.Pairs {
|
||||
for _, alias := range pairs {
|
||||
rKey := utils.REVERSE_ALIASES_PREFIX + alias + target + al.Context
|
||||
cache2go.Pop(rKey, tmpKey)
|
||||
}
|
||||
cache2go.RemKey(key)
|
||||
}
|
||||
}
|
||||
al.RemoveReverseCache()
|
||||
cache2go.RemKey(key)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,6 @@ package engine
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/cgrates/cgrates/cache2go"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
@@ -543,6 +542,13 @@ func (ms *MongoStorage) cacheAccounting(alsKeys []string) (err error) {
|
||||
utils.Logger.Info(fmt.Sprintf("Caching aliases: %v", alsKeys))
|
||||
}
|
||||
for _, key := range alsKeys {
|
||||
// check if it already exists
|
||||
// to remove reverse cache keys
|
||||
if avs, err := cache2go.Get(key); err == nil && avs != nil {
|
||||
al := &Alias{Values: avs.(AliasValues)}
|
||||
al.SetId(key[len(utils.ALIASES_PREFIX):])
|
||||
al.RemoveReverseCache()
|
||||
}
|
||||
cache2go.RemKey(key)
|
||||
if _, err = ms.GetAlias(key[len(utils.ALIASES_PREFIX):], true); err != nil {
|
||||
cache2go.RollbackTransaction()
|
||||
@@ -897,14 +903,7 @@ func (ms *MongoStorage) GetAlias(key string, skipCache bool) (al *Alias, err err
|
||||
if err == nil {
|
||||
cache2go.Cache(key, al.Values)
|
||||
// cache reverse alias
|
||||
for _, value := range al.Values {
|
||||
for target, pairs := range value.Pairs {
|
||||
for _, alias := range pairs {
|
||||
rKey := strings.Join([]string{utils.REVERSE_ALIASES_PREFIX, alias, target, al.Context}, "")
|
||||
cache2go.Push(rKey, utils.ConcatenatedKey(origKey, value.DestinationId))
|
||||
}
|
||||
}
|
||||
}
|
||||
al.SetReverseCache()
|
||||
}
|
||||
}
|
||||
return
|
||||
@@ -915,27 +914,17 @@ func (ms *MongoStorage) RemoveAlias(key string) (err error) {
|
||||
al.SetId(key)
|
||||
origKey := key
|
||||
key = utils.ALIASES_PREFIX + key
|
||||
var aliasValues AliasValues
|
||||
|
||||
var kv struct {
|
||||
Key string
|
||||
Value AliasValues
|
||||
}
|
||||
if err := ms.db.C(colAls).Find(bson.M{"key": origKey}).One(&kv); err == nil {
|
||||
aliasValues = kv.Value
|
||||
al.Values = kv.Value
|
||||
}
|
||||
err = ms.db.C(colAls).Remove(bson.M{"key": origKey})
|
||||
if err == nil {
|
||||
for _, value := range aliasValues {
|
||||
tmpKey := utils.ConcatenatedKey(origKey, value.DestinationId)
|
||||
for target, pairs := range value.Pairs {
|
||||
for _, alias := range pairs {
|
||||
rKey := utils.REVERSE_ALIASES_PREFIX + alias + target + al.Context
|
||||
cache2go.Pop(rKey, tmpKey)
|
||||
}
|
||||
cache2go.RemKey(key)
|
||||
}
|
||||
}
|
||||
al.RemoveReverseCache()
|
||||
cache2go.RemKey(key)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ import (
|
||||
"compress/zlib"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/cgrates/cgrates/cache2go"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
@@ -363,6 +362,13 @@ func (rs *RedisStorage) cacheAccounting(alsKeys []string) (err error) {
|
||||
utils.Logger.Info(fmt.Sprintf("Caching aliases: %v", alsKeys))
|
||||
}
|
||||
for _, key := range alsKeys {
|
||||
// check if it already exists
|
||||
// to remove reverse cache keys
|
||||
if avs, err := cache2go.Get(key); err == nil && avs != nil {
|
||||
al := &Alias{Values: avs.(AliasValues)}
|
||||
al.SetId(key[len(utils.ALIASES_PREFIX):])
|
||||
al.RemoveReverseCache()
|
||||
}
|
||||
cache2go.RemKey(key)
|
||||
if _, err = rs.GetAlias(key[len(utils.ALIASES_PREFIX):], true); err != nil {
|
||||
cache2go.RollbackTransaction()
|
||||
@@ -755,14 +761,7 @@ func (rs *RedisStorage) GetAlias(key string, skipCache bool) (al *Alias, err err
|
||||
if err == nil {
|
||||
cache2go.Cache(key, al.Values)
|
||||
// cache reverse alias
|
||||
for _, value := range al.Values {
|
||||
for target, pairs := range value.Pairs {
|
||||
for _, alias := range pairs {
|
||||
rKey := strings.Join([]string{utils.REVERSE_ALIASES_PREFIX, alias, target, al.Context}, "")
|
||||
cache2go.Push(rKey, utils.ConcatenatedKey(origKey, value.DestinationId))
|
||||
}
|
||||
}
|
||||
}
|
||||
al.SetReverseCache()
|
||||
}
|
||||
}
|
||||
return
|
||||
@@ -776,24 +775,16 @@ func (rs *RedisStorage) RemoveAlias(key string) (err error) {
|
||||
defer rs.db.Put(conn)
|
||||
al := &Alias{}
|
||||
al.SetId(key)
|
||||
origKey := key
|
||||
key = utils.ALIASES_PREFIX + key
|
||||
aliasValues := make(AliasValues, 0)
|
||||
if values, err := conn.Cmd("GET", key).Bytes(); err == nil {
|
||||
rs.ms.Unmarshal(values, &aliasValues)
|
||||
}
|
||||
al.Values = aliasValues
|
||||
err = conn.Cmd("DEL", key).Err
|
||||
if err == nil {
|
||||
for _, value := range aliasValues {
|
||||
tmpKey := utils.ConcatenatedKey(origKey, value.DestinationId)
|
||||
for target, pairs := range value.Pairs {
|
||||
for _, alias := range pairs {
|
||||
rKey := utils.REVERSE_ALIASES_PREFIX + alias + target + al.Context
|
||||
cache2go.Pop(rKey, tmpKey)
|
||||
}
|
||||
cache2go.RemKey(key)
|
||||
}
|
||||
}
|
||||
al.RemoveReverseCache()
|
||||
cache2go.RemKey(key)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user