Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cache data in PHP SESSION, or query from db each time?

Is it "better" (more efficient, faster, more secure, etc) to (A) cache data that is used on every page load in the $_SESSION array (but still querying a table for a flag to reload the data fresh), or (B) to load it from the database each time?

I'm using the cache method (A), but I'm worried that with hundreds of users, memory could become an issue? It's just simple data, like firstname, lastname, birthday, etc.

With either method, there's still a query being run. Thoughts?

like image 864
Andrew Avatar asked Sep 07 '09 04:09

Andrew


People also ask

How does PHP store data in cache?

The easiest is to serialize() the data and store it in your database. When you need to retrieve the database, query it from the database, unserialize() it, and use it as before. As second approach is to add memcache to your PHP installation and access your data via the memcache functions.

How does PHP keep track of sessions?

The session functions keep track of users by issuing them cookies with a randomly generated session IDs. If PHP detects that a user doesn't accept the session ID cookie, it automatically adds the session ID to URLs and forms.

Can we store session in cache?

Session data is stored at the user level but caching data is stored at the application level and shared by all the users. Sessions may not improve performance whereas Cache will improve site performance. Items in cache can expire after given time to cache while items in session will stay till session expires.

What is session data in PHP?

A session is a way to store information (in variables) to be used across multiple pages. Unlike a cookie, the information is not stored on the users computer.


1 Answers

If your data is used on every pages, and is the same for all users, I wouldn't cache it in $_SESSION (which means having a different copy of that data for each user), but with another mecanism, like :

  • file
  • In memory, with APC for instance (if only 1 server)
  • In memory, with memcached, for instance (if you have several servers)
  • If your data requires long calculations or several DB queries to be obtained, caching it in database could be another possibility (would mean only 1 query to fetch back, and less calculations)


If your data is not the same for each user (which seems to be the case in your situation, as you are caching names, birthdates, ...) :

  • I would make sure I only cache what is necessary
  • Once you only have a few data to cache, putting it in session should be quite OK
  • If you really have that many users, you'll probably have some other scalability problems, and will most likely come to use something like memcached anyway ; which means you'll have some other way of caching ;-)

As a sidenote : if you are doing the same query over and over again, you DB server should cache it by itself (for MySQL, it would go into the "query cache") ; so, it would not be as bad as you think, I suppose -- even if not that much optimized ^^

like image 195
Pascal MARTIN Avatar answered Nov 01 '22 00:11

Pascal MARTIN