Redis
Redis is an open-source, in-memory data structure store, used as a database, cache, and message broker. It supports various data structures such as strings, hashes, lists, sets, and more. Redis is known for its high performance, flexibility, and ease of use, making it a popular choice for caching, real-time analytics, and session management.
Using Redis locally
To use Redis locally, you need to set up the PHP Redis extension and run a Redis server instance. This guide will walk you through the steps to configure your environment and get Redis up and running.
php.ini Configuration
Download the
redis.dllfile from the official PECL repository. This file is the PHP extension for Redis. Match the file with your PHP version and architecture. Most likely the Thread Safe version is what you want. Windows 11 is 64-bit. The version of PHP on your machine can be checked with thephp -vcommand on your machine.Place the
redis.dllfile in your PHP extensions directory, typically located atC:\php\ext. This directory contains all the PHP extensions that can be enabled in yourphp.inifile.Open your
php.inifile, which is usually located in your PHP installation directory. Add the following line to enable the Redis extension:iniextension=php_redisThis line tells PHP to load the Redis extension, allowing your PHP applications to interact with Redis.
Installing Redis
To install Redis, you need to have Docker installed on your machine. Docker allows you to run Redis in a containerized environment, which simplifies the setup and management of Redis instances. You need a Docker account to run Docker. When you don't have one, make one and verify it. When you don't verify the account, Docker won't run any containers and the next steps will fail.
- Pull the latest Redis image from Docker Hub by running the following command in your terminal:shThis command downloads the latest Redis image from Docker Hub, which is a repository of pre-built Docker images.
docker pull redis:latest
Running Redis
To start a Redis server instance, use the following command in your Docker terminal or command line:
docker run --name redis-server -p 6379:6379 -d redisThis command will run a Redis container named redis-server, mapping the default Redis port 6379 to your local machine. The -d flag runs the container in detached mode, meaning it will run in the background. You can verify that Redis is running by checking the list of running Docker containers:
docker psConfiguring Your Application
Ensure that the CACHE_DRIVER setting in your .env file is set to redis:
CACHE_DRIVER=redisThe CACHE_DRIVER setting tells your application to use Redis as the caching backend. After making changes to the .env file, clear the configuration cache by running:
php artisan config:clearThis ensures that your application uses the updated configuration settings. Clearing the configuration cache is important because Laravel caches the configuration settings for better performance.
Troubleshooting Docker/Redis problems
If you're able to run Docker but other commands don't seem to work, or if you see an error saying that Docker does not appear to be running, then try opening the Docker Desktop app, it'll likely also complain In that case, you likely need to add yourself to the docker-users group using your user pc account name.
🛠️ Steps to Fix: Open Computer Management: Press Win + X and select Computer Management.
Navigate to Local Users and Groups: Expand Local Users and Groups > Click Groups.
Find and Double-click docker-users: In the list of groups, find docker-users and double-click it.
Add Your User: In the window that opens, click Add.... Enter your Windows username and click Check Names to make sure it's correct. Click OK, then Apply, then OK again.
Log Out and Back In: Log out of your Windows account, or restart your PC to apply the changes.