Thursday, November 21, 2024
HomeEveryday WordPressLearn Redis CLI Basics With WordPress and Docker

Learn Redis CLI Basics With WordPress and Docker


Redis is a schemaless, in-memory key-value data store for real-time applications. Developers can use it as a NoSQL database, a secondary data layer, or a cache layer for computing-intensive processes.

Redis offers persistent storage that can cache website session states and other data. Applications that use Redis as a cache layer typically make one complex database query or API call and then cache the results. It can store and retrieve objects quickly and efficiently. Therefore, Redis is a superb companion for WordPress, which uses a more traditional relational database server as its primary content store.

The Redis command-line interface (CLI) can be used to communicate with your WordPress site’s Redis server. Using the Redis CLI (invoked on the command line as redis-cli), you can request and receive responses from the Redis server programmatically, monitor the commands executed in a Redis instance, check the latency of an instance, and observe the state of the server in real-time.

This tutorial shows you how to integrate Redis and your WordPress site and use the Redis CLI to perform create, read, update, and delete (CRUD) operations from your terminal.

Connect Redis to WordPress Using DevKinsta

To connect a WordPress site to a Redis database, we are using DevKinsta, a free, one-click toolkit for quickly spinning up, developing, and deploying WordPress sites from your local environment.

DevKinsta spins up a Dockerized WordPress site locally with the latest versions of its dependencies, including an Nginx web server, PHP, and a MySQL-compatible MariaDB database. It also features CLI support for WordPress.

DevKinsta is available for Windows, Linux, and macOS. You can find more information regarding the installation when you download DevKinsta.

After the installation has finished, create and install a new WordPress site locally on your device using the default hosting stack.

DevKinsta offers three options for locally creating WordPress sites: a brand-new site with default settings, a new site with custom settings, and cloning an existing site hosted on Kinsta to your local development environment.

This tutorial uses the first option with the default settings and the latest releases of Nginx, PHP, MariaDB, and WordPress.

Once you’ve followed those steps, you can view your new site dashboard in DevKinsta:

The DevKinsta dashboard after creating a new WordPress site.

Take note of the Site Path. This is the directory containing the files and folders for the local site. The WP Admin dashboard link appears in the upper right-hand corner. Finally, note the Site Host, which is the URL pointing to your new development site.

You can open Docker Desktop to list all the containers running as dependencies in your DevKinsta project. To do this, click Containers at the top of the left-hand sidebar:

Screenshot: Docker Desktop list of containers running in DevKinsta.
Docker Desktop’s list of DevKinsta containers.

Now that your WordPress site and container dependencies are up and running, you’re ready to connect a Redis database to your site:

  1. Click the WP Admin button in DevKinsta to navigate to your WordPress admin dashboard.
  2. Click on the Plugins tab on the left side.
  3. Click Add New, and search for “Redis Object Cache.”
Screenshot: Searching for Redis Object Cache while adding plugins within WordPress.
Searching for the Redis Object Cache plugin within WordPress.

Click Install Now next to the plugin. Then, confirm it is active by clicking the Installed Plugins tab.

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

When first activated, the Redis Object Cache plugin will report that it is unable to connect to a Redis database. We solve that by creating a container with a Redis instance within Docker.

Let’s open a terminal and create a network container called redisnet in which to run your Redis image:

docker network create -d bridge redisnet

Now, build and run a Redis image as a standalone container inside that network container:

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

After starting the container image, verify in Docker Desktop that the Redis image is up and running:

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

Remember that your WordPress site launched automatically inside a Docker container. You can confirm its container network name and ID:

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

Above, the truncated version of the network ID for devkinsta_network is d1fa155f7a4d. We’ll be using that later.

One way to connect your Redis cluster and server to your local WordPress site is by directly connecting the Redis and DevKinsta networks. First, verify the container ID for Redis. Here, with the Docker command to list containers, we are using the -l (latest) switch to show just the container created most recently:

docker ps -l
Screenshot: The output of the Docker command to show information about containers.
Container information generated by Docker’s ps command.

In this example, the truncated container ID appears as c3ffc630e44c.

Connect your Redis container to the DevKinsta network using your container ID in place of ours in the command below.

docker network connect devkinsta_network c3ffc630e44c

You should have successfully added the Redis container image to the list of running containers inside devkinsta_network. To verify the network IP address of the Redis container, run the following command, replacing our devkinsta_network ID with the one you found using the network ls command in your project:

docker inspect d1fa155f7a4d
Screenshot: Output of the docker inspect command.
Terminal displaying some the output of the docker inspect command.

In the image above, the IP address of the demo_redis container is 172.172.0.6. Make a note of the IP address in your project before proceeding with the connection.

Navigate to the root of the WordPress site on your local machine, which is on the Site Path displayed in DevKinsta. Open the wp-config.php file in a text editor and add the following code in the section for custom configuration variables:

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

The IP address you use for WP_REDIS_HOST is the one you discovered for demo_redis when you executed the docker inspect command.

This configuration code adds the Redis client, host, and port so that the plugin can access the Redis server inside the same Docker container and network as DevKinsta.

To confirm the plugin is connected and running:

  1. Navigate to your WordPress plugins page within the admin dashboard.
  2. Click the Settings link for the Redis Object Cache.
  3. On the Settings page, click the Enable Object Cache button.
Screenshot: Enabling the Redis Object Cache in WordPress.
Redis Object Cache is writeable and reachable, but needs to be enabled.

Once the Redis Object Cache is enabled, its Settings page will look something like this:

Screenshot: The Redis Object Cache Settings page with the plugin enabled.
The Redis Object Cache Settings page shows Redis is connected and running.



Source link

RELATED ARTICLES
Continue to the category

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Most Popular

Recent Comments