Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Global Variable in elixir Module

Tags:

elixir

I have the following code in elixir:

def get_trackerid(imei) do
  client = get_new_client()
  {:ok, result} = :cqerl.run_query(client, "SELECT * FROM trackers_by_imei where imei = \'#{imei}\';")
  row = :cqerl.all_rows(result)
end

Now, now many functions are calling get_trackerid function and everytime the function is called, a call to database is made.

Is there a way to write a function in elixir such the result is stored in a local variable. So, when the next time trackerid for the same imei is requested, I can get the data from the local variable itself.

I think there is no concept of global variable in elixir, so that is not an option, right?

like image 599
Kshitij Mittal Avatar asked Feb 17 '16 18:02

Kshitij Mittal


People also ask

How do you make a variable in Elixir?

Naming variables follow a snake_case convention in Elixir, i.e., all variables must start with a lowercase letter, followed by 0 or more letters(both upper and lower case), followed at the end by an optional '?' OR '!' .

How do you create a global variable?

The global Keyword Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function. To create a global variable inside a function, you can use the global keyword.

Can you create your own global variables?

To set up a global variable, we need to create it on the global object. The global object is what gives us the scope of the entire project, rather than just the file (module) the variable was created in. In the code block below, we create a global variable called globalString and we give it a value.

What is __ module __ In Elixir?

__MODULE__ is a compilation environment macros which is the current module name as an atom. Now you know alias __MODULE__ just defines an alias for our Elixir module. This is very useful when used with defstruct which we will talk about next.


Video Answer


2 Answers

What you're asking about is called "memoization". I mention that if you care to Google and see if you can find more on the subject.

This is a great blog posting discussing a memoization technique in Elixir: https://web.archive.org/web/20161116091132/http://ineverfinishanyth.in/2014/01/20/memoization-in-elixir

TL;DR

Construct a cache and check the cache when you try to do your computation to see if the answer is already there. In the case of that blog posting he constructs a key-value store as a cache but obviously what sort of cache you should construct will be highly dependent on the data you're caching.

like image 101
Onorio Catenacci Avatar answered Sep 23 '22 02:09

Onorio Catenacci


You have a few options for saving state within Elixir.

If this method is part of a module that is running a GenServer, then you can use the state parameter to cache values.

You could also use an ets table to cache the values. This would work both inside and outside a GenServer.

like image 25
CoderDennis Avatar answered Sep 24 '22 02:09

CoderDennis