mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
SecureMapStorage as cache for EventCost
This commit is contained in:
@@ -35,7 +35,7 @@ func NewBareEventCost() *EventCost {
|
||||
Rates: make(ChargedRates),
|
||||
Timings: make(ChargedTimings),
|
||||
Charges: make([]*ChargingInterval, 0),
|
||||
cache: utils.MapStorage{},
|
||||
cache: utils.NewSecureMapStorage(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,12 +160,12 @@ type EventCost struct {
|
||||
Rates ChargedRates
|
||||
Timings ChargedTimings
|
||||
|
||||
cache utils.MapStorage
|
||||
cache *utils.SecureMapStorage
|
||||
}
|
||||
|
||||
func (ec *EventCost) initCache() {
|
||||
if ec != nil {
|
||||
ec.cache = utils.MapStorage{}
|
||||
ec.cache = utils.NewSecureMapStorage()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -952,7 +952,7 @@ func (ec *EventCost) Trim(atUsage time.Duration) (srplusEC *EventCost, err error
|
||||
// FieldAsInterface func to implement DataProvider
|
||||
func (ec *EventCost) FieldAsInterface(fldPath []string) (val interface{}, err error) {
|
||||
if ec.cache == nil {
|
||||
ec.cache = utils.MapStorage{} // fix gob deserialization
|
||||
ec.initCache() // fix gob deserialization
|
||||
}
|
||||
if val, err = ec.cache.FieldAsInterface(fldPath); err != nil {
|
||||
if err != utils.ErrNotFound { // item found in cache
|
||||
|
||||
@@ -681,6 +681,6 @@ func NewFreeEventCost(cgrID, runID, account string, tStart time.Time, usage time
|
||||
StartTime: "00:00:00",
|
||||
},
|
||||
},
|
||||
cache: utils.MapStorage{},
|
||||
cache: utils.NewSecureMapStorage(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import (
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -326,6 +327,77 @@ func (ms MapStorage) Clone() (msClone MapStorage) {
|
||||
return
|
||||
}
|
||||
|
||||
// NewSecureMapStorage constructs a new SecureMapStorage
|
||||
func NewSecureMapStorage() *SecureMapStorage {
|
||||
return &SecureMapStorage{
|
||||
ms: make(MapStorage),
|
||||
}
|
||||
}
|
||||
|
||||
// SecureMapStorage is a MapStorage with secure read/writes
|
||||
// useful for example as cache for various parts
|
||||
type SecureMapStorage struct {
|
||||
sync.RWMutex
|
||||
ms MapStorage
|
||||
}
|
||||
|
||||
// String returns the JNSON representation of MapStorage
|
||||
func (sm *SecureMapStorage) String() (s string) {
|
||||
sm.RLock()
|
||||
s = sm.ms.String()
|
||||
sm.RUnlock()
|
||||
return
|
||||
}
|
||||
|
||||
// FieldAsInterface returns the value at the path specified
|
||||
func (sm *SecureMapStorage) FieldAsInterface(fldPath []string) (val interface{}, err error) {
|
||||
sm.RLock()
|
||||
val, err = sm.ms.FieldAsInterface(fldPath)
|
||||
sm.RUnlock()
|
||||
return
|
||||
}
|
||||
|
||||
// FieldAsString returns the value at the path specified casted as string
|
||||
func (sm *SecureMapStorage) FieldAsString(fldPath []string) (s string, err error) {
|
||||
sm.RLock()
|
||||
s, err = sm.ms.FieldAsString(fldPath)
|
||||
sm.RUnlock()
|
||||
return
|
||||
}
|
||||
|
||||
// Set will set the value at path
|
||||
func (sm *SecureMapStorage) Set(fldPath []string, val interface{}) (err error) {
|
||||
sm.Lock()
|
||||
err = sm.ms.Set(fldPath, val)
|
||||
sm.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
// GetKeys returns a list of keys for the prefix
|
||||
func (sm *SecureMapStorage) GetKeys(nested bool, nestedLimit int, prefix string) (keys []string) {
|
||||
sm.RLock()
|
||||
keys = sm.ms.GetKeys(nested, nestedLimit, prefix)
|
||||
sm.RUnlock()
|
||||
return
|
||||
}
|
||||
|
||||
// Remove will remove based on field path
|
||||
func (sm *SecureMapStorage) Remove(fldPath []string) (err error) {
|
||||
sm.Lock()
|
||||
err = sm.ms.Remove(fldPath)
|
||||
sm.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
// Clone returns a clone of sm
|
||||
func (sm *SecureMapStorage) Clone() (smClone *SecureMapStorage) {
|
||||
sm.RLock()
|
||||
smClone = new(SecureMapStorage)
|
||||
smClone.ms = smClone.ms.Clone()
|
||||
sm.RUnlock()
|
||||
return
|
||||
}
|
||||
|
||||
// used only in extreme cases where the dataprovider is an object that doesn't implement the dataStorage interface
|
||||
func getPathFromValue(in reflect.Value, prefix string) (out []string) {
|
||||
switch in.Kind() {
|
||||
|
||||
Reference in New Issue
Block a user