How do I implement a HTML cache for a PHP site?

2020-02-08 18:18发布

What is the best way of implementing a cache for a PHP site? Obviously, there are some things that shouldn't be cached (for example search queries), but I want to find a good solution that will make sure that I avoid the 'digg effect'.

I know there is WP-Cache for WordPress, but I'm writing a custom solution that isn't built on WP. I'm interested in either writing my own cache (if it's simple enough), or you could point me to a nice, light framework. I don't know much Apache though, so if it was a PHP framework then it would be a better fit.

Thanks.

标签: php html caching
8条回答
贪生不怕死
2楼-- · 2020-02-08 18:27

You seems to be looking for a PHP cache framework. I recommend you the template system TinyButStrong that comes with a very good CacheSystem plugin. It's simple, light, customizable (you can cache whatever part of the html file you want), very powerful ^^

查看更多
等我变得足够好
3楼-- · 2020-02-08 18:31

The best way to go is to use a proxy cache (Squid, Varnish) and serve appropriate Cache-Control/Expires headers, along with ETags : see Mark Nottingham's Caching Tutorial for a full description of how caches work and how you can get the most performance out of a caching proxy.

Also check out memcached, and try to cache your database queries (or better yet, pre-rendered page fragments) in there.

查看更多
祖国的老花朵
4楼-- · 2020-02-08 18:32

You can use output buffering to selectively save parts of your output (those you want to cache) and display them to the next user if it hasn't been long enough. This way you're still rendering other parts of the page on-the-fly (e.g., customizable boxes, personal information).

查看更多
该账号已被封号
5楼-- · 2020-02-08 18:33

The PHP Smarty template engine (http://www.smarty.net) includes a fairly advanced caching system.

You can find details in the caching section of the Smarty manual: http://www.smarty.net/manual/en/caching.php

查看更多
在下西门庆
6楼-- · 2020-02-08 18:47

If a proxy cache is out of the question, and you're serving complete HTML files, you'll get the best performance by bypassing PHP altogether. Study how WP Super Cache works.

Uncached pages are copied to a cache folder with similar URL structure as your site. On later requests, mod_rewrite notes the existence of the cached file and serves it instead. other RewriteCond directives are used to make sure commenters/logged in users see live PHP requests, but the majority of visitors will be served by Apache directly.

查看更多
聊天终结者
7楼-- · 2020-02-08 18:47

I would recommend Memcached or APC. Both are in-memory caching solutions with dead-simple APIs and lots of libraries.

The trouble with those 2 is you need to install them on your web server or another server if it's Memcached.

APC

Pros:
  • Simple
  • Fast
  • Speeds up PHP execution also
Cons
  • Doesn't work for distributed systems, each machine stores its cache locally

Memcached

Pros:
  • Fast(ish)
  • Can be installed on a separate server for all web servers to use
  • Highly tested, developed at LiveJournal
  • Used by all the big guys (Facebook, Yahoo, Mozilla)

    Cons:
  • Slower than APC

  • Possible network latency
  • Slightly more configuration

I wouldn't recommend writing your own, there are plenty out there. You could go with a disk-based cache if you can't install software on your webserver, but there are possible race issues to deal with. One request could be writing to the file while another is reading.

You actually could cache search queries, even for a few seconds to a minute. Unless your db is being updated more than a few times a second, some delay would be ok.

查看更多
登录 后发表回答