Forum Moderators: coopster

Message Too Old, No Replies

Caching php pages

Is it better?

         

twist

10:19 pm on Dec 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have seen some solutions but want to get the latest opinions to make sure I don't use a out of date method.

I have a large section of my website that is created using php/mysql. Some pages require multiple queries to the database. This section of my website is entirely static, besides the occassional update.

It only makes sense that I should cache these pages and deliver them as static pages. Is there a built in method in php to do this? I am on a shared server and am limited on what I can use to do this.

On the flip side, am I really taxing the server by making these queries or is it not really a big deal these days. For example, my pages are very simple next to the complexity of phpbb. Even my worst script only calls about 10 times to the database to pull the information for the page.

StupidScript

12:51 am on Dec 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think the primary problem will be that PHP has absolutely no idea what is in the client's cache. Since its pages are compiled at the server without checking what's in the cache, there is little hope for asking it to check the cache and use what it finds there.

Perhaps you could lighten the load by using an iframe within an HTML page. The HTML page would be cached, and it could then draw a PHP file containing your query results from the server into its iframe.

The server load would be determined by the elegance and scope of your queries, not necessarily by the number of queries. Are they exceedingly well-formed? Do they contain sub-queries? That kind of stuff.

Just thinking ...

jollymcfats

1:25 am on Dec 21, 2004 (gmt 0)

10+ Year Member



Normally I'd say to investigate caching only when your site seems slow- put your efforts elsewhere until then. But in shared hosting, it's hard to measure the baseline performance of your pages, because who knows what load the server is already under. Even an entirely static site can suffer in shared hosting if another site is hogging all of the resources.

But there are some easy ways to cache your output, if you do want to do that. There is at least one caching module in PEAR (see PHP site), and lots of add-on software. They all will require access to a webserver-writable directory to cache the files to.

If you can't or won't write to the filesystem on your shared server, you could pre-build the cache files in your development environment and upload them to the server. You'd probably only want to do that if updates are quite infrequent.

bobnew32

3:24 am on Dec 21, 2004 (gmt 0)

10+ Year Member



Just a side note, I personally recomend Smarty for caching; its pretty easy when you get the hang of it and seems to work pretty well.

twist

6:06 am on Dec 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I looked at both Smarty and PEAR, need to do more reading on both. I am wondering if there is a downside to writing the files to the server for caching. Could this be exploited. Anything I should watch out for when leaving a directory open for writing?

lajkonik86

10:18 pm on Dec 21, 2004 (gmt 0)

10+ Year Member



Not sure if i understanded your post corectly

You got php/mysql driven pages but don't want to run the php/mysql queries every time but let a visitor see the 30minuten(or whatever) old version?

If that's the case simply write the php page to a html file. Like it does when a visitor visits your page.

And about the load. Don't worry about it unless it becoms a problem. Simple database queries are no load at all for a good server.
I did once load a double 3.0 xeon server to 47%.
But that was because of some miscoding:)

Goodluck

jamie

2:02 pm on Dec 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi twist

caching can mean 2 things

1) clientside caching - whereby you send headers (expires or last-modified) allowing the browser to cache the page instead of loading it again from your server. for instance to send last-modified headers correctly for a php/mysql page, you need to find the last modified times of all data which makes up the page and send the most recent.

i do this for one of my sites, and it means comparing up to 10 database tables, several templates and several included scripts and then sending the browser a last modified header. it is rather complicated, although once it is in place it works very well. clientside caching can save you bandwidth and is recommended by google [webmasterworld.com].

2) server-side caching. we use pear's Cache_Lite class which is easy to implement. this saves load on the database and greatly improves scalability. i would recommend it if you have good traffic.

if you want browser's to be able to cache your pages though, you still have to send last modified headers. (we use the internal cache_lite timestamp value and send it along with the cached data).

this site is an invaluable resource:
[web-caching.com...]

hope that helps (as you may imagine it's a subject dear to my heart ;-)