diff --git a/config/cacheconfig.go b/config/cacheconfig.go new file mode 100644 index 000000000..69475afb5 --- /dev/null +++ b/config/cacheconfig.go @@ -0,0 +1,55 @@ +package config + +import ( + "time" + + "github.com/cgrates/cgrates/utils" +) + +type CacheParamConfig struct { + Limit int + TTL time.Duration + Precache bool +} + +func (self *CacheParamConfig) loadFromJsonCfg(jsnCfg *CacheParamJsonCfg) error { + if jsnCfg == nil { + return nil + } + var err error + if jsnCfg.Limit != nil { + self.Limit = *jsnCfg.Limit + } + if jsnCfg.Ttl != nil { + if self.TTL, err = utils.ParseDurationWithSecs(*jsnCfg.Ttl); err != nil { + return err + } + } + if jsnCfg.Precache != nil { + self.Precache = *jsnCfg.Precache + } + return nil +} + +type CacheConfig struct { + Destinations *CacheParamConfig + ReverseDestinations *CacheParamConfig + RatingPlans *CacheParamConfig + RatingProfiles *CacheParamConfig + Lcr *CacheParamConfig + CdrStats *CacheParamConfig + Actions *CacheParamConfig + ActionPlans *CacheParamConfig + ActionTriggers *CacheParamConfig + SharedGroups *CacheParamConfig + Aliases *CacheParamConfig + ReverseAliases *CacheParamConfig +} + +func (self *CacheConfig) loadFromJsonCfg(jsnCfg *CacheJsonCfg) error { + if jsnCfg.Destinations != nil { + self.Destinations = &CacheParamConfig{} + self.Destinations.loadFromJsonCfg(jsnCfg.Destinations) + } + return nil +} diff --git a/config/config_defaults.go b/config/config_defaults.go index 2ff17f04a..33e1f06a7 100644 --- a/config/config_defaults.go +++ b/config/config_defaults.go @@ -49,6 +49,20 @@ const CGRATES_CFG_JSON = ` }, +"cache":{ + "destinations": {"limit": 10000, "ttl":"0s", "precache": false}, + "reverse_destinations": {"limit": 10000, "ttl":"0s", "precache": false}, + "rating_plans": {"limit": 10000, "ttl":"0s","precache": true}, + "rating_profiles": {"limit": 10000, "ttl":"0s", "precache": false}, + "lcr": {"limit": 10000, "ttl":"0s", "precache": false}, + "cdr_stats": {"limit": 10000, "ttl":"0s", "precache": false}, + "actions": {"limit": 10000, "ttl":"0s", "precache": false}, + "action_plans": {"limit": 10000, "ttl":"0s", "precache": false}, + "action_triggers": {"limit": 10000, "ttl":"0s", "precache": false}, + "shared_groups": {"limit": 10000, "ttl":"0s", "precache": false}, + "aliases": {"limit": 10000, "ttl":"0s", "precache": false}, + "reverse_aliases": {"limit": 10000, "ttl":"0s", "precache": false}, +}, "listen": { "rpc_json": "127.0.0.1:2012", // RPC JSON listening address @@ -386,7 +400,7 @@ const CGRATES_CFG_JSON = ` "rls": { "enabled": false, // starts ResourceLimiter service: . - "cdrstats_conns": [], // address where to reach the cdrstats service, empty to disable stats functionality: <""|*internal|x.y.z.y:1234> + "cdrstats_conns": [], // address where to reach the cdrstats service, empty to disable stats functionality: <""|*internal|x.y.z.y:1234> "cache_dump_interval": "0s", // dump cache regularly to dataDB, 0 - dump at start/shutdown: <""|*never|$dur> "usage_ttl": "3h", // expire usage records if older than this duration <""|*never|$dur> }, diff --git a/config/config_json.go b/config/config_json.go index 28852b08b..5a189e5ff 100644 --- a/config/config_json.go +++ b/config/config_json.go @@ -28,6 +28,7 @@ import ( const ( GENERAL_JSN = "general" + CACHE_JSN = "cache" LISTEN_JSN = "listen" TPDB_JSN = "tariffplan_db" DATADB_JSN = "data_db" @@ -93,6 +94,18 @@ func (self CgrJsonCfg) GeneralJsonCfg() (*GeneralJsonCfg, error) { return cfg, nil } +func (self CgrJsonCfg) CacheJsonCfg() (*CacheJsonCfg, error) { + rawCfg, hasKey := self[CACHE_JSN] + if !hasKey { + return nil, nil + } + cfg := new(CacheJsonCfg) + if err := json.Unmarshal(*rawCfg, cfg); err != nil { + return nil, err + } + return cfg, nil +} + func (self CgrJsonCfg) ListenJsonCfg() (*ListenJsonCfg, error) { rawCfg, hasKey := self[LISTEN_JSN] if !hasKey { diff --git a/config/config_json_test.go b/config/config_json_test.go index 3c48253c2..0de8e9454 100644 --- a/config/config_json_test.go +++ b/config/config_json_test.go @@ -64,6 +64,28 @@ func TestDfGeneralJsonCfg(t *testing.T) { } } +func TestCacheJsonCfg(t *testing.T) { + eCfg := &CacheJsonCfg{ + Destinations: &CacheParamJsonCfg{Limit: utils.IntPointer(10000), Ttl: utils.StringPointer("0s"), Precache: utils.BoolPointer(false)}, + Reverse_destinations: &CacheParamJsonCfg{Limit: utils.IntPointer(10000), Ttl: utils.StringPointer("0s"), Precache: utils.BoolPointer(false)}, + Rating_plans: &CacheParamJsonCfg{Limit: utils.IntPointer(10000), Ttl: utils.StringPointer("0s"), Precache: utils.BoolPointer(true)}, + Rating_profiles: &CacheParamJsonCfg{Limit: utils.IntPointer(10000), Ttl: utils.StringPointer("0s"), Precache: utils.BoolPointer(false)}, + Lcr: &CacheParamJsonCfg{Limit: utils.IntPointer(10000), Ttl: utils.StringPointer("0s"), Precache: utils.BoolPointer(false)}, + Cdr_stats: &CacheParamJsonCfg{Limit: utils.IntPointer(10000), Ttl: utils.StringPointer("0s"), Precache: utils.BoolPointer(false)}, + Actions: &CacheParamJsonCfg{Limit: utils.IntPointer(10000), Ttl: utils.StringPointer("0s"), Precache: utils.BoolPointer(false)}, + Action_plans: &CacheParamJsonCfg{Limit: utils.IntPointer(10000), Ttl: utils.StringPointer("0s"), Precache: utils.BoolPointer(false)}, + Action_triggers: &CacheParamJsonCfg{Limit: utils.IntPointer(10000), Ttl: utils.StringPointer("0s"), Precache: utils.BoolPointer(false)}, + Shared_groups: &CacheParamJsonCfg{Limit: utils.IntPointer(10000), Ttl: utils.StringPointer("0s"), Precache: utils.BoolPointer(false)}, + Aliases: &CacheParamJsonCfg{Limit: utils.IntPointer(10000), Ttl: utils.StringPointer("0s"), Precache: utils.BoolPointer(false)}, + Reverse_aliases: &CacheParamJsonCfg{Limit: utils.IntPointer(10000), Ttl: utils.StringPointer("0s"), Precache: utils.BoolPointer(false)}, + } + if gCfg, err := dfCgrJsonCfg.CacheJsonCfg(); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(eCfg, gCfg) { + t.Error("Received: ", utils.ToIJSON(gCfg)) + } +} + func TestDfListenJsonCfg(t *testing.T) { eCfg := &ListenJsonCfg{ Rpc_json: utils.StringPointer("127.0.0.1:2012"), diff --git a/config/libconfig_json.go b/config/libconfig_json.go index b3a1aadc6..a730600ba 100644 --- a/config/libconfig_json.go +++ b/config/libconfig_json.go @@ -214,6 +214,27 @@ type HaPoolJsonCfg struct { Transport *string } +type CacheParamJsonCfg struct { + Limit *int + Ttl *string + Precache *bool +} + +type CacheJsonCfg struct { + Destinations *CacheParamJsonCfg + Reverse_destinations *CacheParamJsonCfg + Rating_plans *CacheParamJsonCfg + Rating_profiles *CacheParamJsonCfg + Lcr *CacheParamJsonCfg + Cdr_stats *CacheParamJsonCfg + Actions *CacheParamJsonCfg + Action_plans *CacheParamJsonCfg + Action_triggers *CacheParamJsonCfg + Shared_groups *CacheParamJsonCfg + Aliases *CacheParamJsonCfg + Reverse_aliases *CacheParamJsonCfg +} + // Represents one connection instance towards FreeSWITCH type FsConnJsonCfg struct { Address *string diff --git a/data/conf/cgrates/cgrates.json b/data/conf/cgrates/cgrates.json index fd902ab0b..e31b3f515 100644 --- a/data/conf/cgrates/cgrates.json +++ b/data/conf/cgrates/cgrates.json @@ -27,6 +27,20 @@ // "cache_dump_dir": "/var/lib/cgrates/cache_dump" // cache dump for faster start (leave empty to disable) // }, + "cache":{ + "destinations": {"limit": 10000, "ttl":0, "precache": false}, + "reverse_destinations": {"limit": 10000, "ttl":0, "precache": false}, + "rating_plans": {"limit": 10000, "ttl":0,"precache": true}, + "rating_profiles": {"limit": 10000, "ttl":0, "precache": false}, + "lcr": {"limit": 10000, "ttl":0, "precache": false}, + "cdr_stats": {"limit": 10000, "ttl":0, "precache": false}, + "actions": {"limit": 10000, "ttl":0, "precache": false}, + "action_plans": {"limit": 10000, "ttl":0, "precache": false}, + "action_triggers": {"limit": 10000, "ttl":0, "precache": false}, + "shared_groups": {"limit": 10000, "ttl":0, "precache": false}, + "aliases": {"limit": 10000, "ttl":0, "precache": false}, + "reverse_aliases": {"limit": 10000, "ttl":0, "precache": false}, + }, // "listen": { // "rpc_json": "127.0.0.1:2012", // RPC JSON listening address