Asimov

Asimov

Config file: global variables vs "config getcher" function

Hi all,

This is a general programming question, not specific to Elixir. I do my fare share of scripting in LUA. Specifically I code IVR on freeswitch / Asterisk. So a common scenario I have is that there is a bunch of configurations parameters that I have to load in memory every time I run a script (e.g. every time a call hits the IVR). I am coding a project now where the number of configuration parameters is quite large (about 200) - please do not focus on the why of this, that is not the aim of the question.

So, what I usually do is to have a config.lua somehere where I define all these parameters as global variables:

__parameter001__ = value01
__parameter001__ = value02
__parameter_N__ = value_N

Not elegant but it does the job. So, I was thinking, would it be more efficient to have a “config getcher” fuction? Something like this :

function  get_parameter(parameter)
   if(parameter == '__parameter001__')then return value01; 
     elseif(parameter == '__parameter002__')then return value02;
     elseif(parameter == '__parameter_N__')then return value_N; 
     else return 'no_value_set';
    end;
end

In my code I would simply call this function everytime I need a parameter value:

my_parameter = get_parameter('__parameter001__');

So, instead of loading a lot of global variables I would be calling a function every time to get the value of a parameter.

My question is: is this something sensible to do? Should I just simply keep loading the parameters in memory?

I know this is not an Elixir question and my example is LUA based but I believe this is language independent discussion. Also, this forum is much more professional than any LUA forum out there (that I know of), so I’d love to know your opinion.

Thank you!

Most Liked

dimitarvp

dimitarvp

I don’t see why not. Can’t comment on Lua in particular but in Elixir I’d just load these into a Map and then store it and use it like so:

defmodule MyConfig do
  @config_key :my_config

  def load(file_path) do
    # load your configuration parameters into an Elixir map.
    # ...your code here...

    # then, assuming your map with the loaded config is called `cfg`:
    :persistent_term.put(@config_key, cfg)
  end

  def get(param), do: :persistent_term.get(@config_key) |> Map.get(param)
end

That way, you are putting that 200+ keyed map in an in-memory cache that does not get copied when accessed and you have a hyper-fast config map.

(This approach assumes that you’d change that config map rather rarely, by the way.)

dimitarvp

dimitarvp

I’d advise you to do a quick read on Erlang’s :persistent_term module. It’s absolutely perfect for rarely (or never) changing data.

Glad to help.

Where Next?

Popular in Chat/Questions Top

maz
I’m getting this error: ## ** (Ecto.Query.CompileError) Tuples can only be used in comparisons with literal tuples of the same size fro...
New
loganhelms
A while back, I read a great book by Luis Atencio titled, Functional Programming in JavaScript. In section 7.3, he discusses memoization ...
New
mchean
I was wondering if anyone is using some learning tools that they can recommend? I’ve been looking at two specifically so would also be ...
New
ericdouglas
I think that would be really interesting to have official books created by the community about all kinds of development we can do with El...
New
Fl4m3Ph03n1x
GenStage and Flow resources? I have been hearing about GenStage and Flow, a tool built upon it, for quite some time. I understand it allo...
New
marciol
Hey, I have very restricted resources and time so I’m trying to understand the best way to learn Liveview in terms of cost/time. The Pra...
New
yachnytskyi
Hello everyone. I am gonna start with Elixir/Phoenix, thinking to use Stephen Grider as a start point, then elixir school and other sourc...
New
dogweather
Can anyone recommend books/courses/videos that use real-world Elixir? E.g.: Idiomatic error handling design, whether it’s {ok/error, .....
New
SavagePixie
I was wondering if there are any beginner-friendly, exercise-based resources for learning Elixir out there. I’m looking for something lik...
New
shansiddiqui94
Greetings Elixir Developers, My name is Daniel, and I am taking my first step to learn all about the Elixir language. Currently I have a...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement