Loading...
Loading

How To Configure Redis In WordPress

2022-11-08by Juliette Anderson

Redis is a data structure server that implements an advanced key-value database. Its high performance and scalability make it ideal for applications with heavy read, write, or user load requirements. It can be used as a small-scale cache for your WordPress site, but also as a full-blown caching backend solution for storing static content such as images and stylesheets. In this article, we'll show you how to configure Redis in WordPress using the Object Cache plugin.

What is Redis?

Redis is an open-source tool that can be used to store and retrieve data in the same database, avoiding any need for a separate database. It's also considered to be one of the most popular caching solutions on the market. Redis uses a programming language called Lua, which runs on any operating system and allows you to extend or customize its functionality if needed.

 

You can also use Redis with custom PHP Sites.

What are different Redis Data Types?

Redis comes with a wide range of data types. Some are better suited for certain tasks than others, and some have been built specifically for use in WordPress. The following table shows the most relevant ones:

Strings:

Strings are the most fundamental type of Redis value. Redis Strings are binary safe, which implies that they can hold any type of data, such as a JPEG image or a serialized Ruby object.

 

The maximum length of a String value is 512 Megabytes.

Lists:

Redis Lists are simply strings sorted in order of insertion. It is possible to add new entries to a Redis List by pushing them to the list's head (on the left) or tail (on the right).

Sets:

Redis Sets are a collection of Strings that are not ordered.

 

Redis Sets offer the advantage of not permitting duplicate members. When the same element is added several times, the set will only have one copy of it.

Hashes:

Because Redis Hashes are mappings between string fields and string values, they're ideal for representing objects.

 

Hashes are mostly used to represent objects, but because they can store a large number of items, they can be used for a variety of other purposes.

Sorted sets:

Redis Sorted Sets are non-repeating collections of Strings, similar to Redis Sets. The distinction is that every member of a Sorted Set has a score that is used to keep the Sorted Set in order, from lowest to highest score. Scores may be duplicated even though members are unique.

Install and Configure the Redis Object Cache plugin

To configure the Redis Object Cache plugin, you will need to do the following:

  • Install the plugin.
  • Configure the plugin.
  • Set up your site to use Redis Object Cache in a production environment

Testing and Verification

If you're not sure whether your WordPress site is using Redis, try the following:

  • Check that there's a redis.yml file in your wp-content/plugins directory. If it isn't there, create it with the following contents:

Redis : host : localhost port : 6379 dB : 1 keyspace : default timeout : 5s

  • Use the Redis Object Cache plugin to check if the WordPress site is using Redis by enabling its settings panel under Settings > Performance > Object Cache (or whatever relevant option). The plugin will then ask whether or not to enable object caching on this specific page; click on Yes if so. This will give us information about how much work has been done by each request type - namely 'key' and 'value', which are things like searches or blog posts being stored temporarily in memory until they need to be written out onto disk again later down the line when they're accessed again during regular visits from readers who may have logged out without saving anything since last time around when visiting this page last time around."

How to install Redis on CyberPanel?

Redis daemon can be easily installed if you are using CyberPanel, click here to see how you can easily install CyberPanel on your server.

Create a new website

Before using Redis on your PHP site you need to create it in CyberPanel, also make sure to note the PHP version you select while creating the site because we will use this version to later install the Redis PHP Cache extension.

Creating a website is so simple on CyberPanel that anyone can do it. Creating a website is only 4 steps procedure.

  • Login to your CyberPanel Dashboard

  • Click on Websites -> Create Website from the left-hand side menu


  • Enter all the relevant information, make sure to note down which php you are entering here



  • Click on "Create Website"


Install PHP Redis Cache on CyberPanel

Installing Redis on CyberPanel is really very easy and it only involves a couple of steps. So let us go through them.

  • Login to your CyberPanel dashboard

  • Click on "Manage Services" -> "Applications" from the left-hand side menu

  • Click on "Install" in front of "Redis"



Install Redis PHP Extension

Before you can do PHP programming with Redis, you need to install Redis PHP Extension which will communicate with the Redis daemon we just installed above

 

  • Login to SSH using Bitvise and open the command prompt



  • Enter the following command and make sure to enter your php after lsphp that you have selected while creating your website

apt-get install lsphp74-redis -y

  • Now you have to enter a command that will restart php so it could read the extension configuration

 

killall lsphp

  • Go into your site's file manager using CyberPanel

  • Create a file "redis.php" in public_html

  • Open the code mirror of your new file. Enter the following code and click save
  • <?php
  • //Connecting to Redis server on localhost
  • $redis = new Redis();
  • $redis->connect('127.0.0.1', 6379);
  • echo "Connection to server successfully";
  • //store data in the Redis list
  • $redis->lpush("tutorial-list", "Redis");
  • $redis->lpush("tutorial-list", "MongoDB");
  • $redis->lpush("tutorial-list", "Mysql"); 
  •  
  • // Get the stored data and print it
  • $arList = $redis->lrange("tutorial-list", 0 ,5);
  • echo "Stored string in redis:: ";
  • print_r($arList);

?>

  • Open your PHP file in the browser and you will see that your code is communicating with the Redis daemon.

  • Open your SSH and enter the command prompt and enter the command

 

Redis-cli monitor

and it will show your data and you can also check it again in the browser

Conclusion

Faster loading sites can be accomplished using the Redis cache. You can integrate Redis cache with PHP easily by following the PHP Redis Cache example above. Please note that data in Redis is not stored permanently as it is an in-memory cache, so make sure you also have a mechanism in your app to permanently store data for which you need to use MySQL.

news Buffer
Author

Juliette Anderson

Juliette Anderson

Juliette Anderson is an Outreach Community Specialist for an e-commerce fulfillment company that specializes in partnering with online sellers who have an average parcel weight of 5+ pounds or greater. She works hand-in-hand with e-commerce stores to achieve optimal sales for four years already. Her specialty lies in social media marketing and paid promotions.

View Juliette Anderson`s profile for more
line

Leave a Comment