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

前端 未结 8 1023
情深已故
情深已故 2020-12-14 22:49

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 go

相关标签:
8条回答
  • 2020-12-14 23:13

    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.

    0 讨论(0)
  • 2020-12-14 23:17

    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).

    0 讨论(0)
  • 2020-12-14 23:18

    Simple caching of pages, or parts of pages - the Pear::CacheLite class. I also use APC and memcache for different things, but the other answers I've seen so far are more for more complete, and complex systems. If you just need to save some effort rebuilding a part of a page - Cache_lite with a file-backed store is entirely sufficient, and very simple to implement.

    0 讨论(0)
  • 2020-12-14 23:19

    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 ^^

    0 讨论(0)
  • 2020-12-14 23:20

    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.

    0 讨论(0)
  • 2020-12-14 23:22

    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.

    0 讨论(0)
提交回复
热议问题