1

I wonder if there is any general rule to when one should consider assigning data to the session array instead of querying the database each time a new page is loaded knowing that some of the data in the result set has already been retrieved once on a previous page.

3 Answers 3

1

Not sure of your question exactly, but if you know you'll use the data in many pages (like user infos / permissions etc...), you should keep it in Session, else you can query the data.

Sign up to request clarification or add additional context in comments.

Comments

0

My rule of thumb is to only store things in session that are strictly related to the user's session such as their unique ID. Having information in session about an action that is in progress can lead to some odd behaviors. Consider an app with a two step form. Opening the first form sets a value in session that is read on the second form (such as a reference to the last accessed value). This is fine if I only have one tab open, but depending on the use case it might be beneficial for me to have two tabs open. I might open form one against value A, open form one in another tab against value B, then submit the first tab against value A. Depending on the logic I could get erroneous results where data from value B overwrites what should have been in value A.

Comments

0

There are no real rules for this. It always depends on the situation and requirements. Having the current user information in the session means the data in the session can get outdated because of edits done in the database. Commonly almost everything gets written to the database as soon as it is "complete" and loaded with every page request. When dealing with a multi page form, the parial filled form information is often stored in session as long as it hasn't been finished to prevent unfinished data filling up the database.

As said, it all depends on the requirements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.