Jump to content

Caching in php


Darla

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.