0

If I have a user model and a sessions thingy, 1. Is it better to store an object of the user model in the sessions thingy and use that or 2. just store the ID of the user in sessions and create a new user model instance everytime I need user data.

In option 2 you'd query the db quite a lot I think but someone suggested that option 2 is less resource intensive and i'm not quite sure why since you'd be loading the user data out of the db on almost every single page.

Mazzy
  • 1,901
  • 2
  • 16
  • 36

1 Answers1

1

If it's user-specific and you need it very often, it is perfectly reasonable to store it in session. Make sure the object stays small though.
Don't use session as a cache.

Have a look at these similar questions as well :

PHP: Storing 'objects' inside the $_SESSION

Store it in Session or Query DB each page load?

Community
  • 1
  • 1
Matthew
  • 10,988
  • 11
  • 54
  • 69