高级管理文档

💡 云策文档标注

概述

本文是WordPress优化系列的一部分,介绍缓存作为提升性能的最快方式。涵盖插件缓存、浏览器缓存、对象缓存和服务器缓存等核心概念,旨在帮助开发者理解如何通过缓存技术减少服务器负载并加速网站响应。

关键要点

  • 插件缓存:使用W3 Total Cache、WP Super Cache或Cache Enabler等插件将WordPress页面缓存为静态文件,显著提升性能,尤其适合静态内容页面。
  • 浏览器缓存:通过设置Cache-Control和Expires头等HTTP头,让浏览器缓存静态资源(如图片、CSS、JavaScript),减少服务器请求,提高效率。
  • 对象缓存:将数据从慢速存储移至快速存储(如Redis、Memcached),实现持久化缓存,确保应用在缓存损坏时仍能正常运行并自动再生数据。
  • 服务器缓存:在高流量站点中使用服务器级缓存,如Opcache或Varnish,结合WordPress缓存插件可大幅提升PHP性能和整体响应速度。

注意事项

  • 动态内容较多的页面配置缓存可能更复杂,需根据具体需求调整。
  • 选择缓存引擎时应权衡性能、持久性和应用需求,确保缓存数据可替换和再生。
  • 服务器缓存方案多样,从本地缓存到反向代理服务器,需根据站点流量和架构选择合适方案。

📄 原文内容

This article is part of a series on WordPress Optimization.

WordPress caching is the fastest way to improve performance. If your site is getting hit right now install W3 Total Cache, WP Super Cache or Cache Enabler.

Caching Plugins

Plugins like W3 Total Cache, WP Super Cache and Cache Enabler can be easily installed and will cache your WordPress posts and pages as static files. These static files are then served to users, reducing the processing load on the server. This can improve performance several hundred times over for fairly static pages.

When combined with a system level page cache such as Varnish, this can be quite powerful.

If your posts/pages have a lot of dynamic content configuring caching can be more complex. Search for “WordPress cache plugin” for more info.

Browser Caching

Browser caching can help to reduce server load by reducing the number of requests per page. For example, by setting the correct file headers on files that don’t change (static files like images, CSS, JavaScript etc) browsers will then cache these files on your visitor’s computer. This technique allows the browser to check to see if files have changed, instead of simply requesting them. The result is your web server can answer many more 304 responses, confirming that a file is unchanged, instead of 200 responses, which require the file to be sent.

Look into HTTP Cache-Control (specifically max-age) and Expires headers, as well as Entity Tags for more information.

Object Caching

Object caching in WordPress is the act of moving data from a place of expensive and slow retrieval to a place of cheap and fast retrieval. An object cache is also typically persistent, meaning that data cached during one request is available during subsequent requests.

In addition to making data access much easier, cached data should always be replaceable and regenerable. If an application experiences database corruption (e.g., MySQL, Postgres, Couchbase), there will and should be severe consequences for this database (and let us hope that there is a good backup plan in place). In contrast with the main data store for the application, if a cache is corrupted, the application should continue to function as the cached data should regenerate itself. No data will be lost, although there will likely be some performance problems as the cache regenerates.

The storage engine for an object cache can be a number of technologies. Popular object caching engines include Redis, Memcached, APC, and the file system. The caching engine used should be dictated by the needs of the application. Each has its advantages and disadvantages. At a bare minimum the engine used should make accessing the data more performant than regenerating the data.

Server Caching

Web server caching is more complex but is used in very high traffic sites. A wide range of options are available, beyond the scope of this article. The simplest solutions start with the server caching locally while more complex and involved systems may use multiple caching servers (also known as reverse proxy servers) “in front” of web servers where the WordPress application is actually running.

Adding an opcode cache like Opcache, or WinCache on IIS, to your server will improve PHP’s performance by many times.

Varnish cache is very powerful when used with a WordPress caching plugin such as W3TC.

Further Reading