feat: Add Redis support to all frameworks and comprehensive setup documentation
This commit is contained in:
@@ -284,6 +284,155 @@ Changes take effect after container restart:
|
||||
docker-compose restart php
|
||||
```
|
||||
|
||||
## Redis Caching
|
||||
|
||||
### Setup
|
||||
|
||||
Redis is included and pre-configured in `docker-compose.yml`. It runs in a separate container with:
|
||||
|
||||
- **Image**: `redis:alpine` (lightweight, fast)
|
||||
- **Persistent Storage**: Redis data is stored in `redis_data` volume
|
||||
- **Health Check**: Built-in health check to verify Redis is running
|
||||
- **Networking**: Connected to `app-network` for internal PHP communication
|
||||
|
||||
### Redis Service Configuration
|
||||
|
||||
Redis is automatically started with `docker-compose up`. To verify it's running:
|
||||
|
||||
```bash
|
||||
# Check Redis container status
|
||||
docker-compose ps redis
|
||||
|
||||
# Connect to Redis CLI
|
||||
docker-compose exec redis redis-cli
|
||||
|
||||
# Test Redis connection from PHP container
|
||||
docker-compose exec php php -r "
|
||||
\$redis = new Redis();
|
||||
\$redis->connect('redis', 6379);
|
||||
echo 'Redis connected: ' . (\$redis->ping() ? 'OK' : 'FAILED');
|
||||
\$redis->close();
|
||||
"
|
||||
```
|
||||
|
||||
### Using Redis in Your Application
|
||||
|
||||
#### Laravel Example
|
||||
|
||||
Update your `.env`:
|
||||
|
||||
```env
|
||||
CACHE_DRIVER=redis
|
||||
SESSION_DRIVER=redis
|
||||
REDIS_HOST=redis
|
||||
REDIS_PORT=6379
|
||||
REDIS_PASSWORD=null
|
||||
```
|
||||
|
||||
Install/configure in your Laravel app:
|
||||
|
||||
```bash
|
||||
# Inside container
|
||||
docker-compose exec php composer require predis/predis
|
||||
|
||||
# Or use PHP's native Redis extension (recommended)
|
||||
```
|
||||
|
||||
#### PHP Native Example
|
||||
|
||||
```php
|
||||
<?php
|
||||
$redis = new Redis();
|
||||
$redis->connect('redis', 6379);
|
||||
|
||||
// Set a cache value
|
||||
$redis->set('user:1:name', 'John Doe', 3600); // 1 hour TTL
|
||||
|
||||
// Get a value
|
||||
$name = $redis->get('user:1:name');
|
||||
|
||||
// Check if key exists
|
||||
if ($redis->exists('user:1:name')) {
|
||||
echo "Key exists";
|
||||
}
|
||||
|
||||
// Delete a key
|
||||
$redis->del('user:1:name');
|
||||
|
||||
// Increment counter
|
||||
$redis->incr('page_views');
|
||||
|
||||
$redis->close();
|
||||
?>
|
||||
```
|
||||
|
||||
#### Symfony Example
|
||||
|
||||
Configure in `config/packages/cache.yaml`:
|
||||
|
||||
```yaml
|
||||
framework:
|
||||
cache:
|
||||
default_redis_provider: 'redis://redis:6379'
|
||||
pools:
|
||||
cache.app:
|
||||
adapter: cache.adapter.redis
|
||||
provider: 'redis://redis:6379'
|
||||
```
|
||||
|
||||
#### WordPress Example
|
||||
|
||||
Use a Redis plugin like "Redis Object Cache" and configure:
|
||||
|
||||
```php
|
||||
define('WP_REDIS_HOST', 'redis');
|
||||
define('WP_REDIS_PORT', 6379);
|
||||
```
|
||||
|
||||
### Common Redis Operations
|
||||
|
||||
```bash
|
||||
# Connect to Redis CLI
|
||||
docker-compose exec redis redis-cli
|
||||
|
||||
# Inside redis-cli commands:
|
||||
PING # Check connection
|
||||
SET mykey "Hello" # Set a key
|
||||
GET mykey # Get a value
|
||||
DEL mykey # Delete a key
|
||||
KEYS * # List all keys
|
||||
FLUSHDB # Clear all keys
|
||||
INFO # Show Redis info
|
||||
MONITOR # Monitor commands in real-time
|
||||
```
|
||||
|
||||
### Redis Persistence
|
||||
|
||||
- Data persists in the `redis_data` volume
|
||||
- If you need to reset Redis data: `docker volume rm gold_redis_data`
|
||||
- Configure persistence in `docker-compose.yml` if needed:
|
||||
|
||||
```yaml
|
||||
redis:
|
||||
command: redis-server --appendonly yes # Enable AOF persistence
|
||||
```
|
||||
|
||||
### Troubleshooting Redis
|
||||
|
||||
```bash
|
||||
# Check if Redis is running
|
||||
docker-compose logs redis
|
||||
|
||||
# Restart Redis
|
||||
docker-compose restart redis
|
||||
|
||||
# Clear Redis cache
|
||||
docker-compose exec redis redis-cli FLUSHDB
|
||||
|
||||
# Monitor Redis activity
|
||||
docker-compose exec redis redis-cli MONITOR
|
||||
```
|
||||
|
||||
## Docker Architecture
|
||||
|
||||
### Multi-Stage Build
|
||||
@@ -315,6 +464,14 @@ The Dockerfile uses multi-stage builds to optimize for different frameworks:
|
||||
- **Volume**: `db_data` (persistent)
|
||||
- **Port**: `3306` (internal)
|
||||
|
||||
#### Redis Service (Cache)
|
||||
|
||||
- **Image**: `redis:alpine` (lightweight)
|
||||
- **Volume**: `redis_data` (persistent)
|
||||
- **Port**: `6379` (internal)
|
||||
- **Health Check**: Built-in Redis ping monitoring
|
||||
- **Available in all frameworks**: Laravel, Symfony, WordPress, OpenCart, Magento2
|
||||
|
||||
### Network & Volumes
|
||||
|
||||
```yaml
|
||||
@@ -340,13 +497,19 @@ volumes:
|
||||
|
||||
### Framework-Specific Extensions
|
||||
|
||||
**All Frameworks** (Laravel, Symfony, WordPress, OpenCart, Magento2):
|
||||
- `redis`: Redis cache and session support
|
||||
|
||||
**Laravel/Magento2**:
|
||||
- `redis`: Redis cache support
|
||||
- Additional caching optimizations
|
||||
|
||||
**Symfony**:
|
||||
- `apcu`: APCu caching
|
||||
- `sodium`: Modern cryptography
|
||||
|
||||
**WordPress**:
|
||||
- Additional compatibility features
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Port Already in Use
|
||||
|
||||
Reference in New Issue
Block a user