On April 27, 2021, the fifth meetup of the technical series of the Latvian WordPress group took place. The meetup focused on the topic “Object caching practices in WordPress”. The talk covered various kinds of caching and the theory of object caching, with examples of practical use in WordPress. The proceedings of the meeting are available for download via the links at the bottom of this article.
The presentation starts with a brief discussion of existing caching methods: page caching, CDN, Opcode cache, object cache. Further, the speaker explained the essence of object caching and its purpose – storing results of resource-intensive operations. Two ways to store such results are specified – as a simple key-value array in PHP memory, or in-memory database. Particular emphasis is placed on the fact that almost all entities in the WordPress core are cached.
The speaker showed how the object cache inside the WordPress core works and demonstrated that the loading speed of a simple blog page decreases by a factor of about five upon disabling the object cache completely.
The following is an explanation of the differences between non-permanent and persistent object caching. The speaker pointed out that in-memory databases such as Memcached and Redis are useful for persistent object caching. He has explained a comparative table of Redis and Memcached characteristics, having emphasised that the decisive advantage of Redis for WordPress is the presence of specialised plugins.
Additionally, the speaker looked at installing the Redis package on the server and setting up the plugin for WordPress. Also, he explained how the plugin substitutes the object cache functionality in the WordPress core.
Object caching practices in WordPress – case studies
There are 4 case studies – two of medium complexity and two for advanced developers. For the first and second case studies, the speaker prepared a demonstration plugin, the link is available in the presentation.
The first example looks at caching slow queries to the database. It used an example page with two heavy database requests. The non-permanent caching results in only one database query remaining. Upon enabling permanent caching, we have no slow database requests at all. However, this creates difficulties when updating the requested entities (products). The speaker has made the main accent that when applying the persistent object cache, it is necessary to invalidate the cache in time (when updating the goods). He showed an example of how it can be done – and the result on a test site.
The second example deals with caching remote API requests. It shows that the most suitable caching method is transient. The speaker emphasized that WordPress stores such transients not only in the database but also in the object cache, which significantly speeds up the work of the site in the presence of a permanent object cache.
The third example showed a problem encountered when caching internal WPML plugin objects. The rapidly growing size of the cache required the creation of code to limit the size of data stored in the cache, so as not to cause a lack of memory problems.
The fourth example is about optimizing WordPress core – caching user information requests through WP_User_Query. It is shown that on a site with a large number of users (250,000 in the example), core requests to WP_User_Query significantly slow down some pages in the site console. A caching plugin is shown that reduces All Posts page load time from 40 seconds to 0.5 seconds. A link to the plugin repository is available in the presentation.
Finally (as during the report), questions were answered.
Download the presentation of the “Practising object caching in WordPress” meetup here. A recording of the meetup is available below.