Learn Redis CLI Basics Through WordPress along with Docker (r) (r)

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

-sidebar-toc>

The Redis CLI command line interface (CLI) offers a way to interact to your WordPress website's Redis server. Utilizing Redis CLI Redis CLI (invoked through the command line using the redis-cli command-line option) It's possible to request and receive answers from your Redis server, in a programmable way, monitor what actions are executed by your Redis instance, examine the speed at which the server responds for each instance, and track the status of the server's condition active.

This article will show you how to join Redis to your WordPress website, and how to make use of Redis CLI Redis CLI for updating, reading and deletion (CRUD) operations using your terminal.

Create a Connect Redis for WordPress Utilizing the Dev

Dev develops the Dockerized WordPress site locally, making use of the latest versions of its dependencies which include an Nginx Web server PHP and an MySQL compatible MariaDB database. Furthermore, it comes with an option for CLI support to WordPress.

Dev gives three possibilities for creating locally hosted WordPress websites. They include setting up a new website that uses default settings, creating a brand fresh website that has custom configurations, and cloning a site located within your own local development platform.

This tutorial employs the second option using default settings as well as new releases, which include Nginx PHP, MariaDB, and WordPress.

If you've done these 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 new WordPress website.

You can open Docker Desktop to list all containers operating as dependencies in the Dev project. To open it, click the icon for containers at the top-right corner on the left sidebar.

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

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

  1. Hit the administrator button in Dev to access your WordPress admin dashboard.
  2. Select the tab for click on the Plugins tab. This is located on the left.
  3. Choose to include a New Search to search for "Redis the cache for objects."
Screenshot: Searching for Redis Object Cache while adding plugins within WordPress.
Are you searching for Redis Object Cache plugin within WordPress.

Choose "Install Right Now" next to the plugin. After you've installed the plugin ensure that it's activated by clicking the button on the installed Plugins section.

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

In the event that it is activated within the initial time that it is activated, Redis Object Cache plugin will show that it's incapable of connecting with the Redis database. It is possible to build containers using a Redis instance in Docker.

Open a terminal, and create a container for the network called redisnet in which you can create your Redis image

docker network create -d bridge redisnet

Create and begin a Redis image as a stand-alone container within the container that is connected to the network

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

When you have started 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.

Be aware that your WordPress site was automatically launched inside Docker. Docker container. It is possible to confirm the name of the container network as well as the ID number:

docker network Ls
Screenshot: Output of Docker's network list command.
Network IDs and names within Docker Desktop.

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

Another method to connect to the Redis cluster as well as server to the local WordPress site is to directly connect both the Redis or 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 that displays containers, we've used the --l (latest) option in order 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 details about containers are created by the Docker's command ps. command.

In this particular case the ID for the container that has been cut is C3FFFC630e44C..

After that, connect the Redis container onto the Dev network using the ID of the container rather than ours applying the below command.

docker network connect dev_network c3ffc630e44c

The Redis container has successfully added your Redis images of containers into the database of your container that is running within the network called dev_network. In order to confirm the address of IP for Redis You must verify the IP address for Redis. Redis container. For this, you need to run the following command. Replace the network dev ID with the one you have found with this network command ls command. Within the application you are using:

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

In the picture above, the IP address of demo_redis the demo-redis container can be found at 172.172.0.6. Take note of the IP address of the program before you begin the connection.

Find out where the source of the site's root is. WordPress website from your desktop computer. It's located on your Site Path displayed in Dev. The wp-config.php file in text editors. Paste this code into the section where you can customize the appearance of the 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 are using to host WP_REDIS_HOST is the same one you saw in demo_redis. demo_redis is the result of running examine. analyze command.

This configuration code adds the Redis host as well as the port and client in order to enable the plugin to access the Redis server through the identical Docker container and across the same network in the same way as Dev.

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

  1. You can visit the WordPress plugins page within the admin dashboard.
  2. Click on the Settings link for access to access Redis object cache. Redis objects cache.
  3. On the Settings page select on the Settings page, then click option to enable the Cache for objects Cache option.
Screenshot: Enabling the Redis Object Cache in WordPress.
Redis Object Cache is writeable and accessible. However, it must be activated.

If the Redis object cache has been enabled and turned on, the Settings page will be similar to:

Screenshot: The Redis Object Cache Settings page with the plugin enabled.
The Redis page on Setting Object Cache Options shows that Redis is up and running.

It is now possible to use the Redis CLI is ready to be activated.

The Redis CLI application ( redis-cli) comes with a Redis server. It's possible to install this Redis server in Windows, macOS, and Linux operating systems.

In the above example, we were able to execute our docker ps command, we were able determine the ID of the container that is running the Redis image. The ID was determined to be C3FFC630E44C. This ID was altered to access the demo_redis and launch a command-line shell inside it:

docker exec -it c3ffc630e44c /bin/sh

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

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

It is possible to connect with the server to test the connection:

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

In order to connect with the Redis server with its IP address and port, and confirm that the connection has been established, you can execute the following commands (using an IP address for the 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 the IP address of its server as along with port.

It is now possible to connect with success to the Redis server from your computer and in addition to Docker. Docker container.

After that, verify that you're certain that you are sure that your WordPress website is saving data within the Redis cache by using redis-cli and the keys* command. This command will display the key names stored in your database and stored in your database.

Screenshot: Listing the keys on the Redis server.
Searching for all keys stored in the Redis database with an Asterisk query that is a wildcard.

It is evident that some WordPress information was transferred into Redis cache. Redis cache.

Redis CLI CRUD Operations

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

We'll find out what the worth of a key we duplicated in the past Redis key requests. keys Request.

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

The information can be modified within the Redis database with Redis-cli as well as using the sets, get and del commands to generate an appropriate key/value pairing. We can then make an inquiry on the newly created key, remove it

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

As I mentioned in the previous paragraph, trying to figure out the significance of a key that's been erased is a failure. zero.

Additional complex capabilities which are possible with the CLI Redis.

Summary

Redis CLI is a great way to test Redis commands within your website before it is incorporated to a live context. It is also a great option to keep track of the activities of the Redis instance.

With Docker the Redis instance contains everything you'll need to run your database. It includes redis-cli and redis-cli, all of which are put into one.

In addition, Dev's Dockerized process allows to develop the WordPress site on a local computer and deploy the site into production with just few clicks a mouse.

Steve Bonisteel

Steve Bonisteel is a Technical Editor. He began his professional writing career as an editor in the print media, but was also looking for ambulances and fire engines. He has been covering technology related to the internet since the middle of 1990s.

This post was posted on here

Article was posted on here