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, after the request expires, the cache is invalidated and needs to be regenerated for the next request.
WordPress Default Object Cache Efficiency Issues
Those who are familiar with caching principles will find that this mechanism is not very efficient, and in order to improve the efficiency of object caching, we need theobjectificationObject caching, that is, keeping the results of object caching and not letting them expire at the end of a page request, so that the saved cached results can be used directly in the next page request without having to query the database again.
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 redis
After 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 no
After installing the Redis service, we also need to install the PHP Redis extension in order for WordPress to cache data into Redis.
pecl install redis
Memcached 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 );
}
Difference between object caching and page caching
This site has previously introduced the use of 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.
Whereas the object cache caches just MySQL Database Query ResultsWordPress object caching can cache other types of PHP operations, which are more low-level, finer-grained, and easier to work with 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.
Both caching methods can be substantiallyImprove WordPress Page Loading SpeedPage caching is suitable for websites that do not use user logins, while object caching is suitable for websites that require user logins to avoid page caching that caches content that requires user logins to view.
summarize
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.
2 thoughts on “使用 Memcached 或 Redis 加速优化 WordPress 对象缓存(Object Cache)”
I also used this optimized and the results are still noticeable.
Caching still helps a lot with site speeds