Darla Posted February 14, 2007 Share Posted February 14, 2007 Hello I am wondering what is the best method for caching variables or arrays in php (for 3 minutes)? Or is it better to cache the whole page where these variables are? I see there are a few methods out there and I am uncertain what to use. Therefore I am wondering if someone here knows a simple/effective way of doing this as it is kind of a minor application I am building. Have a nice day! Darla Quote Link to comment Share on other sites More sharing options...
Darla Posted February 14, 2007 Author Share Posted February 14, 2007 One more thing: I can use a mysql table for temporary storage, will this be considered good caching even though it is not the traditional way of caching? Darla Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted February 15, 2007 Share Posted February 15, 2007 PHP doesn't store variables beyond the execution of the page, so once it is done and has sent the result to the browser, all data is lost. The easiest way is to store the data in a user session. Alternatively, store it in a database. Quote Link to comment Share on other sites More sharing options...
Guest Posted February 15, 2007 Share Posted February 15, 2007 Caching can take several forms, and how good or bad it is depends what kind of resources you are trying to save the webserver from using. But also consider who'll have access to the cached info. I'll work by example. I developed an application once to perform complex SQL on a MsSQL database to load all the important information, and of course, this would be the same for hundreds if not thousands of users. Of course, these details don't change very often, so I decided to cache the result to avoid stressing the database. After first use in the web app, important variables were put into an array and serialized. I could have either stored them onto a temporary flat file (say, a txt file or an extension I made up) or put the serialized array into the database itself in a temporary or transitory table. I decided to put it onto a database because I prefer to avoid using unlink dynamically, even if the possibility of it going wrong is 0 -- just personal preference At the top of each of the cached pages, I check if the cache exists, if it does and it's timestamp isn't beyond its expiry date, then use them rather than address the database. If it's expired, the script deletes it. It's no problem caching onto a database. In fact, sometimes its preferred, but just balance it out yourself before deciding to cache. The MsSQL database at the time was a heavily fragmented OLTP database, so select statements were at a disadvantage in terms of speed--and records ranged into the ten thousands. Therefore it was obvious that caching would benefit the system. However, if it had been fully defragmented (~90% fillfactor OLAP db) with only a hundred records and ten users, then it may stress your webserver more just to do the little more to cache the data, than it will to rely on retrieving it each and every time. Although that's a pretty extreme example... somebody correct me if I'm wrong. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.