No matter how your website may be fast, it’s always good idea to find some new ways to speed it up. This time, speed will be gain by using very simple cache method that will allow you to cache some parts of the page: menus, widgets and other elements that don’t change that often.
Intro
Many parts of the page don’t change often, but require many SQL queries to be executed. If you have complex menu that combines categories, pages or posts and it can take more than 20 or even 50 SQL queries to make. For instance, all 3 menus on Dev4Press take about 30 SQL queries to get URL’s or structure. Similar situation is with widgets that don’t change every day, but can take a lot of SQL queries.
Cache method that I want to describe uses database to manually store rendered HTML into the wp_options table using transient records that can have expiry time, allowing you to set how long the cached data will be valid. In multisite mode, for rendering that is related to all websites you can use wp_sitemeta table with transient records. I will note the different functions for this later.
Cache Functions
We need 2 functions, one to store and other to get data from cache. You can store: objects, arrays, rendered HTML, anything really.
function dev4press_get_cached_data($name) {
return get_transient($name);
}
function dev4press_set_cached_data($name, $value, $ttl = 43200) {
set_transient($name, $value, $ttl);
}
- $name: name for cached object, this needs to be max 45 characters in length due to the limit of database table structure.
- $value: object/array/string to store.
- $ttl: time-to-live, how long the cached value will be valid, in seconds
$name must be unique for the element you are storing. After the $ttl time expires, stored element will be invalidated. Also, functions up there are very basic, but they allow you to expand with calculating stats about the hits or misses, or whatever you might need.
Best way to use this cache is for caching complex operations that require more than couple of SQL queries or uses one or more very complex SQL queries. Transient cache requires 2 simple SELECT queries that are very fast. You can always measure operations to determine if the cache will give you performance boost or not.
Example: WP Menu Cache
Here is the example how to use this cache method for WordPress menu. WP theme to display menu is wp_nav_menu(). So, if you have menu named “my_menu”, it’s usually added to theme like this:
wp_nav_menu(array("menu" => "my_menu"));
To use cache, code will change to this:
$menu = dev4press_get_cached_data("cached_my_menu");
if ($menu === false) {
$menu = wp_nav_menu(array("echo" => false,"menu" => "my_menu"));
dev4press_set_cached_data("cached_my_menu", $menu, 86400);
}
echo $menu;
Line 1 attempts to get stored menu from transient cache by key “cached_my_menu”. If the result is false, than we build the menu as before (using echo parameter set to false, so that menu is not displayed) on line 3. And when the menu is built, we need to store rendered HTML to transient cache using sameĀ ”cached_my_menu” key on line 4. And than display menu on last line.
First time the menu needs to be displayed, cache will be empty, and lines 3 and 4 will execute making menu and caching it. Next time, line 1 will get the menu from cache skipping rebuild part. After 24 hours (86400), menu will be invalidated, and first line will again return false, and menu will be rebuilt again.
Same principle can be used to handle any type of data to cache.
gdr2 Library
My gdr2 Library used in all my premium plugins and themes has own Cache class that is based on this method. Cache class supports statistics, unique name building and other things. It’s used to cache widgets in plugins and to cache widgets and menus in xScape themes. On Dev4Press website, powered by lightScape theme, widgets are cached and also main menu and the footer links map. Top menu items depend on the user, so it’s not the same for everyone, and caching it is not good idea.
Conclusion
I will search further for more things you can do to optimize websites, so more posts like this will be added. If you have some more suggestions on WordPress speed improving, please leave a comment.






Comment Link
Have you ever tried a caching plugin like W3 Total Cache? It is incredible at caching all aspects of your site, including pages, objects and your database. I think in some cases, where a plugin is so comprehensive and useful, it’s easier to go for the plugin than to add functions to solve the issues. Great post though and certainly a good use of functions to help speed up sites.
Comment Link
I use that plugin on many websites, but its not that good with caching object or database queries because its too general, its caching or all none. With objects caching in W3 active, WP cron jobs will most likely all fail (or most), and there are many other things that will cause problems. I use W3 on this website only for CDN, this is very dynamic website where problems due to overuse of cache can cause problems during purchase or upgrade processing, so if I want to improve speed, I have to do it on my own with safe to use code and methods.