Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To use sessions or db queries when doing check-up on every page?

I have application that has to check multiple data from user table, in order to create page.

Not just id and password, but other data as well, about 7 parts of information at this moment.

This check-up is done on every page.

Do I load all this data in session, and check it from there, or do I fire query on every page request? Needless to say, there are also other queries going on.

What's better practice, regarding optimization?

like image 611
Marko Avatar asked Oct 17 '25 15:10

Marko


1 Answers

You should call the data once from the database and store it in a Session Variable. I generally put my user_id, username, email, user firstname & access_levels in a session when the user first logs in, Then I can call them anywhere in my application whilst the user is logged in.

Do not store the password in the session as it would not be required.

You an also create a variable called logged_in and set it to true or false to test against, (i.e: Show the "Account menu if the user is logged in, else show "Register" Menu).


[Update]

Here is a link to the Pro's/Con's of MySQL caching.
Scroll down to the bottom before the comments.

It kinda depends on your site, for a small site query caching would be fine, but if you wanted to develop more scalable applications, You have to keep speed in mind.


How to tell is mysql query cache is enabled: [LINK]
like image 199
Anil Avatar answered Oct 20 '25 03:10

Anil