How to Configure Redis in WordPress

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 WordPress sites, or as a full-fledged caching backend solution for storing static content such as images and style sheets. In this article, we will 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 without any separate database. It is also considered 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 as needed.

You can also use Redis with custom PHP sites.

What are the different Redis data types?

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

String:

Strings are the most basic Redis value type. Redis strings are binary-safe, which means they can hold any kind of data, such as JPEG images or serialized Ruby objects.

The maximum length of a string value is 512 megabytes.

List:

Redis lists are just strings sorted in insertion order. New entries can be added to a Redis list by pushing them to the head (left) or tail (right) of the list.

set:

A Redis Set is an unordered collection of strings.

Redis Sets have the advantage of not allowing duplicate members. When adding the same element multiple times, the set will have only one copy.

hash:

Because Redis hashes are mappings between string fields and string values, they are great for representing objects.

Hashing is primarily used to represent objects, but since they can store large numbers of items, they can be used for a variety of other purposes.

Sorted set:

Redis Sorted Sets are distinct collections of strings, similar to Redis Sets. The difference is that each member of the Sorted Set has a score that keeps the Sorted Set in order, from lowest to highest score. Scores may repeat even if members are unique.

Install and configure the Redis Object Cache plugin

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

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

Test and Validation

If you’re not sure if your WordPress site uses Redis, try the following:

  • Check your wp-content/plugins directory for a redis.yml file. If it doesn’t exist, create it with:

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

  • Use the Redis Object Cache plugin to check if your WordPress site is using Redis by enabling its settings panel under Settings > Performance > Object Cache (or any related options). The plugin will then ask whether to enable object caching on this particular page; if so, click Yes. This will give us information about how much work has been done for each request type – i.e. “keys” and “values” which are things like searches or blog posts that are temporarily stored in memory until they are needed later Write to disk again When they are accessed again by readers during periodic access, they may have exited without saving anything since the last time this page was accessed. “

How to install Redis on CyberPanel?

If you use Cyber​​​​​​​

Create a new website

Before you can use Redis on your PHP site, you will need to create it in Cyber​​​​Panel, and also make sure to note the PHP version you selected when creating your site, as we will use this version to install the Redis PHP cache extension later.

Creating a website on CyberPanel is so easy that anyone can do it. Create a website in just 4 steps.

  • Login to your Cyber​​​Panel Dashboard

  • Click on Website -> Create Website from left menu
  • Enter all relevant information, be sure to note the php you enter here

  • Click on “Create Website”

Install PHP Redis Cache on Cyber​​​​​

Installing Redis on Cyber​​​Panel is very easy and only takes a few steps. So let’s take a look at them.

  • Login to your Cyber​​​Panel Dashboard

  • Click Manage Services -> Applications in the left menu

  • Click “Install” in front of “Redis”

Install the Redis PHP extension

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

  • Log into SSH with Bitvise and open a command prompt

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

apt-get install lsphp74-redis -y

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

kill all lsphp

  • Use CyberPanel to enter your website’s file manager

  • Create a file “redis.php” in public_html

  • Open the code mirror of the new file.Enter the following code and click save
  • //Connect to Redis server on localhost
  • $redis = new Redis();
  • $redis->connect(‘127.0.0.1’, 6379);
  • echo “connection to server successful”;
  • // store the data in a Redis list
  • $redis->lpush(“Tutorial List”, “Redis”);
  • $redis->lpush(“tutorial-list”, “MongoDB”);
  • $redis->lpush(“tutorial-list”, “Mysql”);
  • // get stored data and print
  • $arList = $redis->lrange(“Tutorial List”, 0 ,5);
  • echo “string stored in redis::”;
  • print_r($arList);

?>

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

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

Redis-cli monitor

It will show your data and you can also view it again in your browser

in conclusion

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

Leave a Reply

Your email address will not be published. Required fields are marked *