简体   繁体   中英

Best practice for storing global data in PHP?

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

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'.

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.

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

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.

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.

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM