Welcome Guest, Not a member yet? Register   Sign In
What is the correct way to load data from a databse once for use everywhere?
#2

There could be multiple different ways to do this.

Cache
You canĀ  fetch the required data and store it in cache (say in your BaseController) and give it a reasonable expiration time.
This would have the added benefit of being available for a multiple different requests too.

Factories
Factories is something that sounds like an exact match for you.


Of course you can also combine the above two... have a Factory instantiate a configuration object for you and then use a setter method in the object to set it's internal properties form cache/database call.
This way you will have the config loaded from cache and also you can access your object in all classes/models/etc.. where you need it.

Personally, in one of my projects I use factories to pass around the user. When a request comes in a filter checks the JWT token (from Auth header/cookie) and after validation it instantiates a user object using factories.
This way i can access the user details in my controllers/models and other libraries where i need it.
Reply


Messages In This Thread
RE: What is the correct way to load data from a databse once for use everywhere? - by qury - 02-11-2023, 02:22 PM



Theme © iAndrew 2016 - Forum software by © MyBB