tags:

views:

84

answers:

3

I have a classifieds website, which uses PHP and MYSQL.

I have several pages which uses javascript also.

I need to know what type of caching to use to increase performance on my site... There is alot of 'constant' images like menues, sidebars, background-images which probably can be cached in a way.

Do you know if web-browsers cache these images automatically or if I have to write a piece of code to do it?

I need guidance in the right direction...

Also, is there a way to somehow cache large php functions? (like when you open an application in windows, the app gets cached somehow, and next time you open it it will open alot faster, if you haven't rstarted your computer that is.)

Thanks

A: 

You can use mod_expire (if you are using apache as webserver) to set the expire HTTP header on your static content(js,images,favicon,plain HTML), so the browser won't request this object until it expires. Depending on your hosting and your audience it might be a good idea to use service as akamai to host your static content (images, css, javascript).

For starting to improve the performance of the server side (PHP), you have to identify bottlenecks. A good approach for doing that would be to implement some logging on your website (SQL queries and how many seconds to get results, what page are most viewed, what function take the most time). You will let this run few weeks/days. Analyze that and you would know what SQL queries to cache, what function to refactor.

If you are in hurry a quick and dirty approach is to get the top 10 most viewed page and cache them on disk. It would work but if your website is really dynamic and need information almost in real time you going to have invalidate that cache often. Also it can create problem if there is some login/logout process in your website. Another approach is to cache some part of those page, usually the more expensive to produce (DB/access, complicate processing).

In term of tools you can have on PHP to do such cache handling:

  • APC: that tool have some caching feature, plus PHP precompilation
  • memcached: a distibuted caching system
  • eAccelator: pre compilation
  • xcache: pre compilation
RageZ
-1 for the client-side caching and performance advice. Client caching rules need to be set explicitly for files, and there are tradeoffs involved (far-future caching requires changing the resource URL to refresh). Use a CDN (e.g. CloudFront) vs. storage like S3. To really speed thing up, read http://developer.yahoo.com/performance/rules.html
orip
@Orip: thanks for your advices, I have edited my answer to reflect those
RageZ
A: 

You could use a caching proxy like Squid or some sort of PHP accelerator. Additionally, caching MySQL query results might be a good idea if the data you're querying doesn't change much.

As another answer noted, static content will usually be cached by the users' browsers if the timestamps on the files don't change.

Conrad Meyer
A: 

For a site I recently launched, I wrote some code using ob_start() to cache my PHP files to flat HTML. Here's an example:

$cacheFile = 'cache/home.html';
$cacheTime = 600;

if (file_exists($cacheFile) && time() - $cacheTime < filemtime($cacheFile)) {
    require $cacheFile;
} else {
    ob_start();

    // Your dynamic code

    $fp = fopen($indexCacheFile, 'w');
    fwrite($fp, ob_get_contents());
    fclose($fp);
    ob_end_flush();
}

The site has been around nearly a month, and has done a lot of traffic. The above code has saved my server multiple times.

Jordan Satok