Wordpress Redis



  1. Wordpress Redis Docker
  2. Redis Object
  3. Wordpress Redis Cache

When running WordPress sites you should always look for ways to enhance the sites’ performance by enabling caching and other performance boosting configurations…

This brief tutorial shows students and new users how to setup WordPress to use Redis for object caching to speed up database queries and PHP dynamic calls….

The first time a WordPress page is loaded a database query is made via PHP…. When using Redis, these queries can be stored into system memory so that when another user loads the same page there is no need to query the database again… Redis will retrieve the previously cached query results from memory…

Navigate around your WP admin area and keep an eye on the Redis monitor, to confirm if the WordPress/Redis integration is okay. Run the following command: redis-cli monitor. Log entries will appear if Redis is communicating with the WordPress; it’s a positive sign that Redis. So, WordPress and Redis story. Serverpilot has nice tutorial on how to configure the redis object cache. Before I installed and connected redis object cache on my WordPress site, everything worked perfectly. Once I connected my site to use redis, the issues described got repeated. Siteground‘s shared hosting uses Memcached for object cache. Redis and object caching will exponentially speed up your WordPress page load time with every subsequent visit. In this guide, we will cover how to configure Redis as a cache for WordPress to lessen all redundant and time-consuming database queries that are used for rendering a WordPress page.

Setting this will help improve WordPress performance…
To get started with enable Redis with WordPress support, please continue below:

The first thing is to have a working WordPress site… If you don’t have WordPress installed, then search our site for help installing WordPress..

Wordpress

Wordpress Redis Docker

We have covered WordPress installation with both Apache2 and Nginx support on this site….

Step 1: Install the Latest Redis Server

After installing WordPress, run the commands below to install the latest version of Redis…

The version of Redis server that comes with Ubuntu by default isn’t the latest… To install the latest version you will have to add the PPA below and install from there…

To do that, run the commands below:

sudo apt-get install software-properties-common

Once that package is installed, run the commands below to add the PPA that contains the latest version of Redis…

sudo add-apt-repository ppa:chris-lea/redis-server

After adding the repository, run the command below to install Redis…

Verify that you have version 4 of Redis installed with the following command:

redis-server --version

You should see Redis virsion installed on your system….

Step 2: Allocate Memory to Redis and Setup an Eviction Policy

Now that Redis is installed, run the commands below to open its configuration file…

sudo nano /etc/redis/redis.conf

Then uncomment the lines below to allow memory and an eviction policy… You’ll have to scroll through the lines to find these… Then set then up as shown below:

Save the file and exit…

When you’re done run the commands below restart Redis and PHP… If you’re running Apache2, then just restart Apache2 and Redis server…

Step 3: Configure WordPress wp-config.php file

After the above steps run the commands below to open WordPress wp-config.php file in your WordPress root directory…

sudo nano /var/www/html/wordpress/wp-config.php

Then add these lines just below WordPress unique Keys and Salts section…

After that, save the file and exit..

Step 4: Install a Redis Object Cache Plugin

The final step is to install WordPress Redis Object Cache Plugin… To do that navigate to the plugin page and install in WordPress…

After installing, activate and enable the plugin… When you’re done, go to the plugin settings page and if everything is configured correctly, you should see something similar to the image below:

That should do it!

Your WordPress setup should now use Redis object caching to help speed up your site…

Enjoy!

You may also like the post below:

Core class that implements an object cache.

Contents

Description Description

The WordPress Object Cache is used to save on trips to the database. The Object Cache stores all of the cache data to memory and makes the cache contents available by using a key, which is used to name and later retrieve the cache contents.

The Object Cache can be replaced by other caching mechanisms by placing files in the wp-content folder which is looked at in wp-settings. If that file exists, then this file will not be included.

More Information More Information

Role of the WordPress Object CacheRole of the WordPress Object Cache

WP_Object_Cache is WordPress’ class for caching data which may be computationally expensive to regenerate, such as the result of complex database queries. The object cache is defined in wp-includes/cache.php.

Do not use the class directly in your code when writing plugins, but use the wp_cache functions listed below.

By default, the object cache is non-persistent. This means that data stored in the cache resides in memory only and only for the duration of the request. Cached data will not be stored persistently across page loads unless you install a persistent caching plugin.

Use the Transients API instead of these functions if you need to guarantee that your data will be cached. If persistent caching is configured, then the transients functions will use the wp_cache functions described in this document. However if persistent caching has not been enabled, then the data will instead be cached to the options table.

wp_cache functionswp_cache functions

Most of these functions take a:

  • $key: the key to indicate the value.
  • $data: the value you want to store.
  • $group: (optional) this is a way of grouping data within the cache. Allows you to use the same key across different groups.
  • $expire: (optional) this defines how many seconds to keep the cache for. Only applicable to some functions. Defaults to 0 (as long as possible).

This function adds data to the cache if the cache key doesn’t already exist. If it does exist, the data is not added and the function returns false.

Adds data to the cache. If the cache key already exists, then it will be overwritten; if not then it will be created.

Returns the value of the cached object, or false if the cache key doesn’t exist.

To disambiguate a cached false from a non-existing key, you should do absolute testing of $found, which is passed by reference, against false: if $found false, the key does not exist.

Clears data from the cache for the given key.

Replaces the given cache if it exists, returns false otherwise. This is similar to wp_cache_set() except the cache object is not added if it doesn’t already exist.

Clears all cached data.

Hints to the object cache that the group or list of groups should not be cached in persistent storage. This is useful when adding items to the cache that should only be available for the duration of a script session, and not beyond. $groups can be an array of strings, or a single group name. NB: only some caching plugins implement this function!

Persistent CachingPersistent Caching

Prior to WordPress 2.5, data stored using the wp_cache functions was stored persistently if you added define('WP_CACHE', true) to your wp-config.php file.

This is no longer the case, and adding the define will have no effect unless you install a persistent cache plugin (see list below).

Persistent Cache PluginsPersistent Cache Plugins

  • Memcached Object Cache provides a persistent backend for the WordPress object cache. A memcached server and the PECL memcached extension are required.
  • APCu Object Cache provides a persistent backend for the WordPress object cache using APC / APCu, a PHP key/value store saved in memory.
  • Redis Object Cache supports the use of Predis (PHP client library), HHVM’s Redis extension and the PECL Redis extension to provide a persistent backend for the WordPress object cache. Redis is required.
  • W3 Total Cache provides object level caching using disk, opcode or memcache(d) memory stores. W3TC also provides: browser, page and database caching, in addition to Content Delivery Network Support, Mobile Support, Minification and more.
  • LiteSpeed Cache supports the use of Redis, Memcached, and LSMCD object caches. LSCWP also provides server-level full-page caching, support for browser cache and CDN, and other optimization features. LiteSpeed Web Server required.

RelatedRelated

The Transients_API provides persistent but temporary data caching by giving it a custom name and a timeframe after which it will be expired and regenerated.

Redis Object

Note: Transients only get deleted when a request is made. So, until someone visits your page and calls up the Transient, it will stay in the DB. In short: It’s not a real persistent cache and not equal to stuff running on cron jobs.

ResourcesResources

Wordpress Redis Cache

  • The Debug Bar plugin helps you analyze the object cache.
  • Scott Taylor’s article WordPress + Memcached explains various cache plugins and concepts.

Source Source

File: wp-includes/class-wp-object-cache.php

View on Trac

Methods Methods

  • __construct — Sets up object properties; PHP 5 style constructor.
  • __destruct — Saves the object cache before object is completely destroyed.
  • __get — Makes private properties readable for backward compatibility.
  • __isset — Makes private properties checkable for backward compatibility.
  • __set — Makes private properties settable for backward compatibility.
  • __unset — Makes private properties un-settable for backward compatibility.
  • _exists — Serves as a utility function to determine whether a key exists in the cache.
  • add — Adds data to the cache if it doesn't already exist.
  • add_global_groups — Sets the list of global cache groups.
  • decr — Decrements numeric cache item's value.
  • delete — Removes the contents of the cache key in the group.
  • flush — Clears the object cache of all data.
  • get — Retrieves the cache contents, if it exists.
  • get_multiple — Retrieves multiple values from the cache in one call.
  • incr — Increments numeric cache item's value.
  • replace — Replaces the contents in the cache, if contents already exist.
  • reset — Resets cache keys. — deprecated
  • set — Sets the data contents into the cache.
  • stats — Echoes the stats of the caching.
  • switch_to_blog — Switches the internal blog ID.

Changelog Changelog

Changelog
VersionDescription
2.0.0Introduced.

User Contributed Notes User Contributed Notes