What type of caching should I use?

2019-08-07 12:06发布

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

标签: php html caching
3条回答
Root(大扎)
2楼-- · 2019-08-07 12:55

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.

查看更多
beautiful°
3楼-- · 2019-08-07 12:57

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.

查看更多
成全新的幸福
4楼-- · 2019-08-07 13:00

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
查看更多
登录 后发表回答