What is the correct way to load data from a databse once for use everywhere? |
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. |
Messages In This Thread |
What is the correct way to load data from a databse once for use everywhere? - by objecttothis - 02-11-2023, 08:32 AM
RE: What is the correct way to load data from a databse once for use everywhere? - by qury - 02-11-2023, 02:22 PM
RE: What is the correct way to load data from a databse once for use everywhere? - by kenjis - 02-11-2023, 02:52 PM
RE: What is the correct way to load data from a databse once for use everywhere? - by objecttothis - 02-11-2023, 03:16 PM
RE: What is the correct way to load data from a databse once for use everywhere? - by kenjis - 02-11-2023, 05:29 PM
RE: What is the correct way to load data from a databse once for use everywhere? - by objecttothis - 02-12-2023, 09:46 AM
RE: What is the correct way to load data from a databse once for use everywhere? - by InsiteFX - 02-14-2023, 12:54 AM
|