Initializing view data at application startup time


I have a Phoenix (1.4.12) umbrella project with LiveView (0.6.0). My live views needs to render floating point numbers rounded to a predefined number of decimals. That number depends on the type of value displayed. The associations between the type of values (elec power, …) and the number of decimals (0, 1, 2, 3, …) is kept in a database and is supposed to be retrieved once at the start of the system. It could be updated at a later time in the database and refreshed in the system but it is a rare event.

I have currently three umbrella applications : (1) one for database access, (2) one for the web (Phoenix) and (3) the last one to get data from remote sites, take decisions, persist data through app 1, present data through app 2, etc.

The live views get the data through a PubSub subscription made inside mount/3. Thanks to that, app 3 sends data to app 2.

The question is : how could I let my views (live or not) get the number of decimals through a map which is produced just once, for the whole web part, at startup ? Is it possible ? Or do I have to fetch them inside mount/3 each time a live view is instantiated (using app 1) which I think is costly.


Seems like a good use case for :persistent_term. You could add a GenServer to your app that, on boot, hits the database, looks up the values, and then places them in :persistent_term which provides extremely fast lookups.

1 Like