Learn Redis CLI Basics Through WordPress as well as Docker (r) (r)

Oct 5, 2023
An illustration representing the Redis command line interface (CLI).

-sidebar-toc>

The Redis CLI command line interface (CLI) provides a means to interact with your WordPress website's Redis server. Utilizing Redis CLI Redis CLI (invoked through the command line with the redis-cli command-line option) It is possible to request and receive responses from your Redis server in a programmed manner, track the actions executed by the Redis instance, check the response speed for the instance and monitor the state of the server's condition live.

This article explains how to link Redis along with your WordPress site and use Redis CLI Redis CLI to perform the reading, update, and delete (CRUD) actions with your terminal.

Create a Connect Redis to WordPress Utilizing Dev

Dev develops the Dockerized WordPress website locally, using the most current versions of its dependencies that include an Nginx Web server PHP along with an MySQL suitable MariaDB database. In addition, it has support for CLI support for WordPress.

Dev provides three choices for creating locally hosted WordPress websites. These include creating a new site that has default settings, creating a brand new website with customized settings as well as cloning a site located within your own local development platform.

This tutorial uses the second alternative with default settings and the latest releases that include Nginx, PHP, MariaDB, and WordPress.

Once you've followed those steps, you'll have access to your dashboard via Dev:

Screenshot of the Dev dashboard and the details of a new WordPress site.
The Dev dashboard after creating the all-new WordPress website.

It is possible to open Docker Desktop to list all containers operating as dependencies in the Dev project. To do this, Click the icon for Containers in the top right-hand corner of the left-hand sidebar

Screenshot: Docker Desktop list of containers running in Dev.
A list of Docker desktop's lists of Dev containers.

Once your WordPress website and the container dependencies have been running without issue and ready to connect to an Redis database on your site:

  1. Hit the WP admin button on Dev to navigate to your WordPress administration dashboard.
  2. Click from the tab for the Plugins tab, which is located on the left side.
  3. Select to add a New Search to search for "Redis the object cache."
Screenshot: Searching for Redis Object Cache while adding plugins within WordPress.
Are you searching for Redis Object Cache plugin within WordPress.

Click "Install Right Now" next to the plugin. Once you have installed it, make sure that it's active by selecting the button on the Installed Plugins tab.

Screenshot: A list of installed WordPress plugins, with Redis Object Cache highlighted.
Installed WordPress plugins, for example Redis Object Cache.

When activated following the initial time that it is activated, Redis Object Cache plugin will show that it's not able to connect to the Redis database. It is possible to build containers with an Redis instance inside Docker.

Open a terminal, and create a container for the network called redisnet in which you will be able to execute your Redis image

docker network create -d bridge redisnet

Make and start an Redis image as a stand-alone container in the container which is connected to the network

docker run -d -p 6379:6379 --name demo_redis --network redisnet redis

After starting the container image Verify within Docker Desktop that the Redis image is in operation:

Screenshot: Indicator that demo_redis image is running in Docker Desktop.
Docker Desktop running the demo_redis image.

Remember that your WordPress website was launched automatically in the Docker container. It is possible to confirm the name of the container network and ID:

docker network Ls
Screenshot: Output of Docker's network list command.
Names and IDs for networks inside Docker Desktop.

The shortened version of the network ID used for that network is D1Fa155f7a4d. The ID will be used at a later date.

Another method to connect the Redis cluster and server to your local WordPress website is directly connecting the Redis and Dev networks. To begin, check whether the ID is the same of the container's name and connect it to Redis. In this case, by using the Docker command which displays containers, we've used the --l (latest) switch to only show the most recent container which was built:

docker PS"-l"
Screenshot: The output of the Docker command to show information about containers.
The information about containers is generated by Docker's ps command.

In this case, the container ID that is cut appears as C3FFFC630e44C..

Then connect the Redis container onto the Dev network by using the ID for the container instead of ours by using the following command.

docker network connect dev_network c3ffc630e44c

The Redis container should have been successfully added your Redis containers images to your container database operating within the network dev_network. To confirm the IP address for Redis, you need to verify the IP address for the Redis container. To do this, run the below command. Change your network dev ID with the one you have found with the network command ls command. In your application:

docker inspect d1fa155f7a4d
Screenshot: Output of the docker inspect command.
Terminal showing some outputs from the Inspector docker command.

As you can see in the picture above, the IP address of demo_redis demo_redis container is 172.172.0.6. Note the IP address of the application before you start the connection.

Locate where the root of the website is located. WordPress website from your home computer. It is situated at your Site Path displayed in Dev. The wp-config.php file in text editors and paste this code into the section that is devoted to customization of configuration variables.

define('WP_REDIS_CLIENT', 'predis'); define('WP_REDIS_HOST', '172.172.0.6'); define('WP_REDIS_PORT', '6379');

The IP address that you use for hosting WP_REDIS_HOST is the one that you found in demo_redis. demo_redis when you run the examine command.

This configuration code adds the Redis host as well as the port and client to allow the plugin to access the Redis server from that same Docker container and across the same network in similar fashion to Dev.

Check that your plugin's connection is active and running:

  1. Go to your WordPress plugins page within the administration dashboard.
  2. Click on the Settings link to access to access the Redis object cache.
  3. On the Settings page select the option to enable the Cache for the objects Cache button.
Screenshot: Enabling the Redis Object Cache in WordPress.
Redis Object Cache is writeable and accessible, but it requires to be enabled.

If the Redis object cache is enabled and enabled, the Settings page will appear identical to:

Screenshot: The Redis Object Cache Settings page with the plugin enabled.
The Redis page for Setting Object Cache Options shows that Redis is active and online.

The Redis CLI is available to be launched.

The Redis CLI tool ( redis-cli) is bundled with a Redis server. It is possible to install the Redis server on Windows, macOS, and Linux operating systems.

Above, when we used to execute the docker ps command and we could determine the ID of the container that runs the Redis image. It was identified as C3FFC630E44C. The altered ID was used to gain access to the demo_redis and launch a command-line shell within it:

docker exec -it c3ffc630e44c /bin/sh

Then you are able to begin the redis-cli software:

Screenshot: redis-cli invoked in Docker.
redis-cli invoked from Docker's shell. Docker.

It is possible to try to contact the server order to test the connection:

Ping
Screenshot: pinging the Redis server using redis-cli.
Server response to Ping in the redis server response to Ping in redis.

In order to connect with the Redis server with its port and IP address and confirm the connection run the following commands (using an IP address of your Redis container you're using for a demonstration container):

redis-cli 172.172.0.6 -p 6379 ping
Screenshot: Pinging the Redis server via an IP address.
Pings are transmitted to the Redis server through its IP address as well as port.

You can now connect with success to the Redis server, both from your own machine in addition to Docker. Docker container.

After that, verify that you are sure that your WordPress website is saving information within the Redis cache by using redis-cli as well as its keys* command. The command will show all the keys in your database that are saved in the database.

Screenshot: Listing the keys on the Redis server.
Searching for all keys stored in the Redis database by using an Asterisk wildcard query.

It is evident that some WordPress information has been transfered into Redis cache. Redis cache.

Redis CLI CRUD Operations

The CLI tool lets you execute CRUD functions within the Redis database using this CLI tool.

We'll find out what the worth of a key we duplicated from the previous Redis key requests. key Request.

get :u.:~8r]mC->Re/gG!&mGU. [{+;]t7o
Screenshot: Results of a Redis database query.
Response from Redis for "get" request made using Redis-cli.

You can modify the information in the Redis database using the redis-cli as well as using the set, get and del commands to create the key/value pair. Then, we can make a query on the new key, remove it

Screenshot: Creating, querying and deleting Redis key/value data.
Simple CRUD operations with an Redis database using the redis-cli.

Like I said in the previous paragraph, trying to discover the significance of a key which has been erased is a failure. zero.

There are numerous additional sophisticated capabilities that can be performed with the CLI Redis.

Summary

Redis CLI is an ideal way to test Redis commands in your site before bringing it into an actual environment. This is also an excellent option to keep track of the activity of a Redis instance.

In Docker the Redis instance includes everything you require to handle your database. This includes redis-cli which is all packed into a single container.

In addition, Dev's Dockerized technique makes it possible to build the WordPress site on a local computer and deploy the site to production in only a couple of clicks on a mouse.

Steve Bonisteel

Steve Bonisteel is a Technical Editor who started his writing career as an editor in print, and was chasing in search of ambulances and fire engines. The journalist has covered internet-related technologies since the mid 1990s.

This post was posted on here