Accelerated Optimization of WordPress Object Cache with Memcached or Redis
WordPress Object Cache is a method of caching time-consuming database query results in WordPress. By default, the Object Cache is non-persistent and remains in memory only during the request period, and when the request expires, the cache is invalidated and needs to be regenerated for the next request.
WordPress Default Object Cache Efficiency Issues
熟悉缓存原理的朋友会发现,这种机制的效率并不高,为了提高对象缓存的效率,我们需要持久化对象缓存,也就是说,把对象缓存的结果保留下来,不让他们在页面请求结束时失效,这样在下次页面请求时就可以直接使用保存下来的缓存结果,而不用再次查询数据库了。
Caching Data with Redis or Memcached Persistent Objects
Redis and Memcached are both well-known in-memory databases because they can store data directly in memory, which can greatly increase the speed of data access, and they are often used as a caching database for MySQL.
Installing the Redis service and the PHP Redis extension
Ubuntu, Debian, and CentOS distributions all have Redis servers, so we can install them directly by executing the corresponding commands. Of course, if you want more control, you can also download the source code and compile the Redis server yourself.
apt-get install redisAfter installing the Redis server, if the startup is unsuccessful and reports "MISCONF Redis is configured to save RDB snapshots" error, we can use redis-cli to disable the database hard disk snapshots function, because we only use Redis as a caching service, and we don't need to save the data to the hard disk.
127.0.0.1:6379> config set stop-writes-on-bgsave-error noAfter installing the Redis service, we also need to install the PHP Redis extension in order for WordPress to cache data into Redis.
pecl install redisMemcached is installed in a similar way to Redis, so I won't go into that here. After installing and making sure that the Redis or Memcached services we need are working, we also need to enable Redis or Memcached object caching in WordPress in order to take advantage of these two services to persist object caches for us.
Enabling Redis Object Caching in WordPress
The way to enable persistent object caching in WordPress is to replace the default cache function in WordPress with a custom cache function by adding an “object-cache.php” to the "wp-content/" directory. The "object-cache.php" is essentially a "Drop-in" type WordPress plugin that replaces the WordPress built-in functions.
In order to cache objects in Redis, we need to enable Redis object caching in WordPress, download "object-cache.php" from the address below and upload it to the "wp-content/" directory of your WordPress site.
https://github.com/pressjitsu/pj-object-cache-red
Memcached object caching plugin
If you are using Memcached to persist WordPress object caches, both of the following caching plugins can be used, just choose any one.
Some plugins automatically add "object-cache.php" to the wp-content/ directory, such as the popular WP Super Cache plugin, so if you encounter this situation, you can just replace this file.
If after adding "object-cache.php", the website fails to open with 500 errors, it means that our Redis service or extension is not installed properly, check the following to make sure they are available, and then add "object-cache.php".
Using object caching when developing WordPress themes or plugins
WordPress provides us with functions that make it easy to use object caching.
- wp_cache_add() : add data to the cache, return flase if the data already exists.
- wp_cache_set() : add data to cache, overwrite data if it already exists
- wp_cache_get() : get the data in the cache, if the data does not exist, return false
- wp_cache_delete() : delete data from cache
- wp_cache_replace(): replaces data in the cache, similar to wp_cache_set, but does not automatically add data if it does not exist.
- wp_cache_flush(): flush all caches
WordPress Object Cache Usage Usage Example
$result = wp_cache_get( 'my_result' );
if ( false === $result ) {
$result = $wpdb->get_results( $query );
wp_cache_set( 'my_result', $result );
} 对象缓存和页面缓存的区别
本站之前介绍过使用 Cachify Caching WordPress Pages to increase page load speed, this method of caching WordPress-generated pages directly is called “page caching”, caching directly WordPress-generated HTML pageIt caches not only database queries, but also the results of PHP logical operations in the page templates.
而对象缓存缓存的只是 MySQL Database Query ResultsWordPress object caching can cache other types of PHP operations, which are more low-level, finer-grained, and easier to manipulate than page caching. On the other hand, WordPress object caching can cache not only frontend database queries, but also dashboard data query results, which can improve the opening speed of WordPress backend to some extent.
两种缓存方法都可以大幅Improve WordPress Page Loading Speed,页面缓存适合在没用用户登录的网站上使用;而对象缓存适合在需要用户登录操作的网站上面使用,以避免页面缓存缓存了需要用户登录才可以查看的内容。
总结
Whether it is page caching, or object caching, are used to improve the efficiency of data acquisition, speed up the site page open speed, object caching and page caching is not a conflict, can be used at the same time, with each other. Although caching can improve the page open speed, but it does not mean that we do not have to consider the performance and efficiency issues in the development. Always put the data access efficiency in mind, in order to develop a high-performance WordPress application, improve the user experience.
I also used this optimized and the results are still noticeable.
Caching still helps a lot with site speeds