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

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
332 30681 154
New
Lincxx
Hello, I’m sure this has been asked a bunch, but where to start. I do prefer vodeos over books, but recently I have found books to be mor...
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
venomnert
Background I have been a backend elixir developer for about 3 years now. I have been mainly working on simple CRUD applications. Context...
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
boddhisattva
Greetings everyone, At my current workplace we’re evaluating different ways of building a microservices architecture for some parts rela...
New
pdgonzalez872
Hi! In my quest of becoming the best Elixir dev I can be, I saw one aspect in my career that I’d like to improve upon. This is language...
New
Nopp
Hey guys and girls, i am completely “new” to programming, recently played a bit with Python, Ruby and PureBasic, but i want to try somet...
New
Sujit
Hi Team From the title, I am entirely new to programming and i am interested in learning elixir but not sure where to start. Can someone...
New
jslearner
Will learning Erlang really help in being a better Phoenix or Elixir developer or is it a waste of time?
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement