Back to .
This article describes how to use Redis Cloud services powered by Redis Labs® in the IBM® Cloud. To introduce how to use Redis Cloud services, we describe a scenario to enable WordPress blog object caching by using Redis Cloud services. Redis Cloud services: Redis Cloud is a fully managed cloud service for hosting and running your Redis data set in a highly available and scalable manner, with predictable and stable top performance. WordPress object caching: Cache is one of the best ways for WordPress to improve performance. WordPress object caching is a mechanism for caching data that might be computationally expensive to regenerate, such as the results of complex database queries. WordPress object caching Redis Cloud services: WordPress, by default, performs a form of object caching, but the cached object’s lifetime is only a single page load. Therefore, the object cache is non-persistent, and cached data will not be stored across page loads. WordPress allows you to make the objectcache persist across page loads by using files, a relational database, or an in-memory database. In our scenario, we illustrate how to use the Redis Cloud service that is provided by Redis Labs as the persistence layer of the WordPress object cache.
The following services of the are used in this article:
The following diagram summarizes the WordPress components and how WordPress interacts with Redis Cloud services. We use a WordPress plug-in to enable the object cache that is persisted by Redis Cloud.
The following video shows how to use Redis Cloud services to enable the WordPress object cache by using a WordPress plug-in.
Follow the steps in to deploy a WordPress blog in the IBM Cloud.
In the IBM Cloud marketplace, search for Redis Cloud. Follow to order the Redis Cloud service.
After the order is processed, you can obtain your Redis Cloud access information by logging on to .
Navigate to MY RESOURCES -> Manage. On the Manage page, you need to record the value of the Resource Name, Endpoint, and Redis Password. (Click Show to see the value of the Redis password.)
# wget # wget # sudo rpm -Uvh remi-release-6*.rpm epel-release-6*.rpmEnable the PHP Extension Community Library by using the following commands: # sudo yum -y install gcc # sudo yum -y install php-pear Install the Redis PECL module by using the following command: # sudo pecl install redis To load the extension on the PHP startup, use the following command: # sudo echo "extension=redis.so" >> /etc/php.ini To auto install the Redis Object Cache plug-in from the WordPress admin panel, follow these steps: Navigate to Plugins -> Add New. Search for Redis Object Cache. Find the Redis Object Cache plug-in. Click Install Now (under the plug-in name) to install the plug-in. Install Redis Object Cache plug-in Or, to upload the Redis Object Cache plug-in manually, follow these steps: Download the . Upload the Redis Object Cache plug-in (redis-object-cache.1.0.zip) to the $YOURWP/wp-contents/plugins/ folder. Extract the plug-in package: # sudo unzip redis-object-cache.1.0.zip Configure the Redis Object Cache plug-in in WordPress By default, the plug-in uses 127.0.0.1 and 6379 as the default host and port when you create a new client instance. The default database of “0” is also used. So, we need to point the plug-in to our newly subscribed Redis Cloud resource. To redirect the plug-in, follow these steps: Move the object-cache.php file to the wp-content/ directory. The plug-in’s major functionis in the object-cache.php file. To make this plug-in take effect, we need to move it from wp-content/plugins/redis-object-cache to wp-content/ by using the following command: # mv $YOURWP/wp-content/plugins/redis-object-cache/object-cache.php $YOURWP/wp-content/ Edit the object-cache.php file. In the object-cache.php file, three constants, WP_REDIS_BACKEND_HOST, WP_REDIS_BACKEND_PORT, and WP_REDIS_BACKEND_DB, are supported to specify the non-default Redis connection. We need to edit object-cache.php to add the following four lines at the beginning of the file (around line 23). The file needs to look like this example: /** * Adds a value to cache. * * If the specified key already exists, the value is not stored and the function * returns false. * * @param string $key The key under which to store the value. * @param mixed $value The value to store. * @param string $group The group value appended to the $key. * @param int $expiration Theexpiration time, defaults to 0. * * @global WP_Object_Cache $wp_object_cache * * @return bool Returns TRUE on success or FALSE on failure. */ define("WP_REDIS_BACKEND_HOST", "pub-redis-11223.dal-05.1.sl.garantiadata.com"); define("WP_REDIS_BACKEND_PORT", "11223"); define("WP_REDIS_BACKEND_DB", "wordpress"); define("WP_REDIS_PASSWORD", "passw0rd"); Use the Redis Cloud access information that you recorded in the “Get a Redis Cloud service account” section to set the value of the above constants. Use the “Resource Name” value for WP_REDIS_BACKEND_DB. Split the “Endpoint” value into the host and port for WP_REDIS_BACKEND_HOST and WP_REDIS_BACKEND_PORT. And, use the “Redis Password” value for WP_REDIS_PASSWORD. Because the original Redis Object Cache plug-in does not support connecting to Redis with a password, we need to slightly modify the plug-in’s code to adopt the Redis Cloud, which uses a password to protect the data set. That is why we added an extraconstant, WP_REDIS_PASSWORD, above. Additionally, we also need to modify the connecting code to consume the WP_REDIS_PASSWORD: Add the following code around line 325: if ( defined( 'WP_REDIS_PASSWORD' ) && WP_REDIS_PASSWORD ) { $redis['password'] = WP_REDIS_PASSWORD; } Add the following code around line 330: $this->redis->auth( $redis['password'] ); The file needs to look like this example: public function __construct() { global $blog_id, $table_prefix; // General Redis settings $redis = array( 'host' => '127.0.0.1', 'port' => 6379, ); if ( defined( 'WP_REDIS_BACKEND_HOST' ) && WP_REDIS_BACKEND_HOST ) { $redis['host'] = WP_REDIS_BACKEND_HOST; } if ( defined( 'WP_REDIS_BACKEND_PORT' ) && WP_REDIS_BACKEND_PORT ) { $redis['port'] = WP_REDIS_BACKEND_PORT; } if ( defined( 'WP_REDIS_BACKEND_DB' ) && WP_REDIS_BACKEND_DB ) { $redis['database'] = WP_REDIS_BACKEND_DB; } if ( defined( 'WP_REDIS_PASSWORD' ) && WP_REDIS_PASSWORD) { $redis['password'] = WP_REDIS_PASSWORD; } // Use Redis PECL library if available, otherwise default to bundled Predis library if ( class_exists( 'Redis' ) ) { try { $this->redis = new Redis(); $this->redis->connect( $redis['host'], $redis['port'] ); $this->redis->auth( $redis['password'] ); if ( isset( $redis['database'] ) ) { $this->redis->select( $redis['database'] ); } Verification Open a web browser and log on to . Navigate to MY RESOURCES -> Dashboard. On the Dashboard page, you can see the dynamic throughput and latency of data read and write from your Redis Cloud. You can also see the used memory, total keys, and connections in your Redis Cloud. All of these parameters are zero if you have not accessed your WordPress blog since you installed and configured the plug-in. Now, use your browser to experiment with your WordPress. As shown in the following UI, you see the used memory, total keys, andconnections all increase in the dashboard. The throughput shows curves to indicate the read and write operations in the Redis Cloud, as well as the latency charts. Also, notice that if you access the pages for a second time, the page loads much faster than the first hit. Redis Cloud dashboard shows the traffic from wordpress Share this: