If you are working on Laravel project optimization, you might have considered using cache, with Redis being a popular choice. Laravel provides support for multiple cache drivers, including Redis.
To start using Redis, you'll first need to install the phpredis
package. You can do this by running the following command:
composer require predis/predis
Next, update your .env
file and change the CACHE_DRIVER
value to redis:
CACHE_DRIVER=redis
Now, let's set up Redis using a Docker container. If you don't have Docker Desktop installed, follow these steps:
Install Docker Desktop
Download Docker Desktop:
Visit the Docker Desktop website and download the appropriate version for your operating system.Install Docker Desktop:
Follow the installation instructions for your operating system.
Set Up Redis with Docker
- Create a Folder:
mkdir docker-workspace
- Navigate to the Folder:
cd docker-workspace
- Create a Docker Compose File:
touch docker-compose.yml
-
Open
docker-compose.yml
and Add the Following Content:
version: "3.7"
services:
# Redis Server for Cache
redis-cache:
image: redis:latest
restart: always
ports:
- "6379:6379"
command: redis-server --save 20 1 --loglevel warning
volumes:
- cache:/data
volumes:
cache:
driver: local
Save and Close the File.
Start Docker Containers:
docker-compose up -d
With this setup, you now have a Redis server running in a Docker container, ready to be used for caching in your Laravel application.
For a more detailed guide on setting up Docker containers for other services like MySQL and Mailhog, you can refer to my previous articles (Mailhog, MySQL).
Happy coding!
Top comments (0)