WordPress with Redis installed makes your website insanely fast. Redis is an advanced key-value store. It’s installed on our Byte platform. I’ve tested Redis on this WordPress installation and the results are smashing. Before Redis implementation the first GET of pages were generated in 0.4 to 1.1 sec. After Redis implementation the first GET of pages are generating in 0.003 sec. In this blogpost I will show you how Redis can be implemented on WordPress.
Installation of Redis in WordPress
- Byte has already installed Redis on the webservers.
- Just order the Building Block Redis on your Service Panel to activate Redis.
- If you are not a Byte customer yet: contact us via email@example.com
- Byte implemented the PHP library PHPRedis to communicate with Redis.
- Create a backup of index.php in the root of your WordPress installation.
- Open index.php and drop the following code in it:
This is a modified version of Jim’s script which is based on connection to another PHP Library to connect with Redis.
- Define the Redis connection information in line 72 and 73 to identify and authorize your connection. By default Redis connects to localhost on port 6379 without a password. Information about your Redis Server, Port and Password can be found on the Redis page of our Service Panel.
- Create a backup of your newly created index.php, since it will be replaced each time you upgrade WordPress.
- Deactivate all other cache plugins.
Insanely fast page generation with Redis
Allow your IP address to see the debug information and change $debug from 0 to 1. You’ll see the debug information. If you’re not logged in your WordPress site your site will be served from Redis and generated insanely fast.
Some details worthwhile to know
- Pages are not cached when you’re logged in.
(very useful when adding / changing WordPress blogposts)
- Cached pages do not expire not unless explicitly deleted or reset (deleting the entire domain cache).
- Appending a ?c=y (e.g. example.com/?c=y) to a url deletes the entire cache of the domain.
This will only work when you are logged in.
- Appending a ?r=y to a url deletes the cache of that url.
- Refreshing (F5) a page deletes the cache of that page.
- Script still works even if allow_fopen is disabled in php.
- Submitting a comment deletes the cache of that page.
- Includes a debug mode, stats are displayed at the bottom most part after .
- Won’t be deleted by CloudFlare.
- Only visible for allowed IP addresses.
Invalidatie Redis cache in WordPress
The Redis script never invalidates cached pages unless explicitly deleted or reset them. When creating, editing or deleting posts this can be a problem. In the blog post “How to Drastically Speed Up WordPress with Redis” Flynsarmy writes an idea how to solve this issue. His script is based on PHP library PRedis. I’ve modified the script to function it well on PHPRedis as installed on our platform. The connection to the Redis server is already defined in wp-config.php.
If you got some improvements on both scripts, please leave a comment on Github:
- Redis in WordPress: https://gist.github.com/hans2103/5657621
- Redis Cache Invalidation: https://gist.github.com/hans2103/5727924
Enjoy your fast website!
- update 17-10-2013: changed the Redis script. Ability to exclude pages from caching. Not useful to cache your WooCommerce Shopping Cart, Checkout nor My Account.
- update 16-09-2013: changed the Redis script. Now every page has it’s own key.
Meer lezen over Redis: