Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a two-pass PHP cache system with mutable items

Tags:

php

caching

I want to implement a two-pass cache system:

  • The first pass generates a PHP file, with all of the common stuff (e.g. news items), hardcoded. The database then has a cache table to link these with the pages (eg "index.php page=1 style=default"), the database also stores an uptodate field, which if false causes the first pass to rerun the next time the page is viewed.

  • The second pass fills in the minor details, such as how long ago something(?) was, and mutable items like "You are logged in as...".

However I'm not sure on a efficient implementation, that supports both cached and non-cached (e.g., search) pages, without a lot of code and several queries.

Right now each time the page is loaded the PHP script is run regenerating the page. For pages like search this is fine, because most searches are different, but for other pages such as the index this is virtually the same for each hit, yet generates a large number of queries and is quite a long script.

The problem is some parts of the page do change on a per-user basis, such as the "You are logged in as..." section, so simply saving the generated pages would still result in 10,000's of nearly identical pages.

The main concern is with reducing the load on the server, since I'm on shared hosting and at this point can't afford to upgrade, but the site is using a sizeable portion of the servers CPU + putting a fair load on the MySQL server.

So basically minimising how much has to be done for each page request, and not regenerating stuff like the news items on the index all the time seems a good start, compared to say search which is a far less static page.

I actually considered hard coding the news items as plain HTML, but then that means maintaining them in several places (since they may be used for searches and the comments are on a page dedicated to that news item (i.e. news.php), etc).

like image 958
Fire Lancer Avatar asked Oct 01 '08 07:10

Fire Lancer


3 Answers

I second Ken's rec of PEAR's Cache_Lite library, you can use it to easily cache either parts of pages or entire pages.

If you're running your own server(s), I'd strongly recommend memcached instead. It's much faster since it runs entirely in memory and is used extensively by a lot of high-volume sites. It's a very easy, stable, trouble-free daemon to run. In terms of your PHP code, you'd use it much the same way as Cache_Lite, to cache various page sections or full pages (or other arbitrary blobs of data), and it's very easy to use since PHP has a memcache interface built in.

For super high-traffic full-page caching, take a look at doing Varnish or Squid as a caching reverse proxy server. (Pages that get served by Varnish are going to come out easily 100x faster than anything that hits the PHP interpreter.)

Keep in mind with caching, you really only need to cache things that are being frequently accessed. Sometimes it can be a trap to develop a really sophisticated caching strategy when you don't really need it. For a page like your home page that's getting hit several times a second, you definitely want to optimize it for speed; for a page that gets maybe a few hits an hour, like a month-old blog post, it's a bad idea to cache it, you only waste your time and make things more complicated and bug-prone.

like image 55
joelhardi Avatar answered Nov 02 '22 23:11

joelhardi


I recommend to don't reinvent the wheel... there are some template engines that support caching, like Smarty

like image 41
jochil Avatar answered Nov 02 '22 23:11

jochil


For server side caching use something like Cache_Lite (and let someone else worry about file locking, expiry dates, file corruption)

like image 30
Ken Avatar answered Nov 03 '22 00:11

Ken