tags:

views:

221

answers:

7

Hi

I'm running a web application that allows a user to log in. The user can add/remove content to his/her 'library' which is displayed on a page called "library.php". Instead of querying the database for the contents of the users library everytime they load "library.php", I want to store it globally for PHP when the user logs in, so that the query is only run once. Is there a best practice for doing this? fx. storing their library in an array in a session?

Thanks for your time

A: 

I think it would be best to store it in a Session.
It the user logs in, the Session is being created and you can save data in it using the superglobal:

$_SESSION['key'] = "value";

You can also store Arrays or everything else there and it can be cleared if the user logs out.

Tobias
+1  A: 

When the user login you can generate a xml file (USER_ID.xml for instance) that you display with xslt.

http://php.net/manual/en/book.xslt.php

remi bourgarel
+6  A: 

If you store each user's library in a $_SESSION as an array, as you suggested (which is definitely possible) you will have to make sure that any updates the user makes to the library are instantly reflected to that session variable.

Honestly, unless there is some seriously heavy querying going on to fetch a library, or you have tons of traffic, I would just stick to 'execute query whenever the user hits library.php'.

karim79
Another thing that might be better is to create a own cache that will be created when the user logs in (something like a static html site showing the library) which is deleted after a specific period of time (e.g. 30minutes or if user logs out) and which will be refreshed if it does not exist or if any UPDATE query has been done.
Tobias
True - but at the expense of added complexity. As a first step, I would add indexes and a query cache to the library tables to max out the query performance.
karim79
+1  A: 

Consider the size of the data. Multiply that by the maximum number of concurrent users.

Then compare that the to memory avaiable on your server. Also consider whether or not this is a shared server; other sites needs resources too.

Based on this, it is probably best to either create a file that can be used (as per Remi's comment), or remain in the default stateless form and read every time. I doubt that reading the data each time is creating much of an overhead.

Psychic
+1  A: 

Each PHP script dies when it completes, so data can not be kept permanentely live in a web application as you would do in a PC application.

One way could be sessions, but it depends on the amount of data you want to save. According to your example you are talking about a library, so it sounds to me like big quantity of data need to be saved, in such case the DB is the way to go, and yes you have to query it each time.

Another way could be to save them in an array inside a php file, but in the same way you have to query the DB each time, you would have to include such php file each time.

Marco Demajo
A: 

Since this is a db performance optimization, I would suggest that you take a look at memcached which matches your problem perfectly:

memcached is [..] intended for use in speeding up dynamic web applications by alleviating database load.

Martin Wickman
A: 

you care for performance; Please note:

  • Session may use database or file to store data.
  • database is here to be used instead of files, for it's performance and abilities.

use database, it is designed to be used exactly in such situations!

takpar