PHPackages                             tourze/redis-dedicated-connection-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. tourze/redis-dedicated-connection-bundle

ActiveSymfony-bundle[Caching](/categories/caching)

tourze/redis-dedicated-connection-bundle
========================================

为服务自动创建和注入专用的 Redis 连接，支持属性、标签、环境变量等多种配置方式

1.0.1(5mo ago)05.7k4MITPHPCI passing

Since Oct 31Pushed 4mo agoCompare

[ Source](https://github.com/tourze/redis-dedicated-connection-bundle)[ Packagist](https://packagist.org/packages/tourze/redis-dedicated-connection-bundle)[ RSS](/packages/tourze-redis-dedicated-connection-bundle/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (17)Versions (3)Used By (4)

Redis Dedicated Connection Bundle
=================================

[](#redis-dedicated-connection-bundle)

[English](README.md) | [中文](README.zh-CN.md)

[![Latest Version](https://camo.githubusercontent.com/1304e2e32e9b12e8112c88370cb11d67cc802b1b8335d8ba28e8b30a2cc4b6c1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f746f75727a652f72656469732d6465646963617465642d636f6e6e656374696f6e2d62756e646c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/tourze/redis-dedicated-connection-bundle)[![PHP Version Require](https://camo.githubusercontent.com/190c4a0caeafdb73f92ff81887a979d6f81fbafbcd1c6c90f6f47e258d1f8050/68747470733a2f2f706f7365722e707567782e6f72672f746f75727a652f72656469732d6465646963617465642d636f6e6e656374696f6e2d62756e646c652f726571756972652f7068703f7374796c653d666c61742d737175617265)](https://packagist.org/packages/tourze/redis-dedicated-connection-bundle)[![Build Status](https://camo.githubusercontent.com/3c75cbf80739d3f42836bab035102123295320544fd5b4157e0fec7fc34b2780/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f746f75727a652f7068702d6d6f6e6f7265706f2f63692e796d6c3f6272616e63683d6d6173746572267374796c653d666c61742d737175617265)](https://github.com/tourze/php-monorepo/actions)[![Quality Score](https://camo.githubusercontent.com/43930c8561a14d3f5a68c62b692ee20749bac4c043e09e88fd3fdd611660198f/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f746f75727a652f7068702d6d6f6e6f7265706f2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/tourze/php-monorepo)[![Code Coverage](https://camo.githubusercontent.com/f8c8d287e21b1753139ef0e764b0199f0ab3d06609c63b799dac90d702850f3d/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f746f75727a652f7068702d6d6f6e6f7265706f3f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/tourze/php-monorepo)[![License](https://camo.githubusercontent.com/6de1ee907109d26b32e4c03d66efc065c4d55ab4a6b915b49cfa569f6498fd35/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f746f75727a652f7068702d6d6f6e6f7265706f3f7374796c653d666c61742d737175617265)](LICENSE)[![Total Downloads](https://camo.githubusercontent.com/5a02875f885894a9cd296f7bd5853063ef20e386a5e3da98a559ecff6457fa0c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f746f75727a652f72656469732d6465646963617465642d636f6e6e656374696f6e2d62756e646c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/tourze/redis-dedicated-connection-bundle)

This bundle provides automatic creation and injection of dedicated Redis connections for Symfony services, supporting attributes, tags, and environment variable configuration.

Table of Contents
-----------------

[](#table-of-contents)

- [Features](#features)
- [Dependencies](#dependencies)
- [Installation](#installation)
- [Usage](#usage)
    - [Method 1: Using PHP Attributes](#method-1-using-php-attributes)
    - [Method 2: Using Service Tags](#method-2-using-service-tags)
    - [Method 3: Using Connection Channel Tags](#method-3-using-connection-channel-tags)
    - [Method 4: Using the Helper Class](#method-4-using-the-helper-class)
- [Configuration](#configuration)
    - [Environment Variables](#environment-variables)
    - [Method 1: Using Redis URL (Recommended)](#method-1-using-redis-url-recommended)
    - [Method 2: Using Individual Parameters](#method-2-using-individual-parameters)
- [Available Environment Variables](#available-environment-variables)
- [Advanced Usage](#advanced-usage)
    - [Multiple Connections for One Service](#multiple-connections-for-one-service)
    - [Direct Connection Reference](#direct-connection-reference)
    - [Checking Connection Existence](#checking-connection-existence)
- [Testing](#testing)
- [Security](#security)
- [Contributing](#contributing)
    - [Running Tests](#running-tests)
    - [Reporting Issues](#reporting-issues)
- [License](#license)

Features
--------

[](#features)

- **Automatic Connection Creation**: Automatically creates Redis connections based on service requirements
- **Multiple Configuration Methods**: Support for PHP attributes, service tags, and environment variables
- **Connection Isolation**: Each service can have its own dedicated Redis connection
- **Coroutine Support**: Proper connection management in coroutine environments
- **Flexible Configuration**: Support for single instance, cluster, and replication modes

Dependencies
------------

[](#dependencies)

- PHP 8.1 or higher
- Symfony 6.4 or higher
- PHP Redis extension
- tourze/symfony-runtime-context-bundle

Installation
------------

[](#installation)

```
composer require tourze/redis-dedicated-connection-bundle
```

**Note**: This bundle requires the PHP Redis extension to be installed:

```
pecl install redis
```

Usage
-----

[](#usage)

### Method 1: Using PHP Attributes

[](#method-1-using-php-attributes)

```
use Tourze\RedisDedicatedConnectionBundle\Attribute\WithDedicatedConnection;

#[WithDedicatedConnection('cache')]
class CacheService
{
    public function __construct(
        private readonly \Redis $redis
    ) {}
}
```

### Method 2: Using Service Tags

[](#method-2-using-service-tags)

```
services:
  App\Service\SessionService:
    arguments:
      $redis: '@redis'
    tags:
      - { name: 'redis.dedicated_connection', channel: 'session' }
```

### Method 3: Using Connection Channel Tags

[](#method-3-using-connection-channel-tags)

```
services:
  App\Service\QueueService:
    calls:
      - [setRedis, ['@redis']]
    tags:
      - { name: 'redis.connection_channel', channel: 'queue' }
```

### Method 4: Using the Helper Class

[](#method-4-using-the-helper-class)

```
use Tourze\RedisDedicatedConnectionBundle\DependencyInjection\DedicatedConnectionHelper;

// In your bundle extension or compiler pass
$definition = $container->getDefinition('app.my_service');
DedicatedConnectionHelper::addDedicatedConnection($definition, 'metrics');

// Or create a new service with connection
$definition = DedicatedConnectionHelper::createServiceWithConnection(
    MyService::class,
    'analytics',
    ['@logger'] // additional arguments
);
$container->setDefinition('app.analytics_service', $definition);
```

Configuration
-------------

[](#configuration)

### Environment Variables

[](#environment-variables)

The bundle supports two ways to configure Redis connections:

### Method 1: Using Redis URL (Recommended)

[](#method-1-using-redis-url-recommended)

Configure connections using Redis URLs following the [official Redis URI scheme](https://github.com/redis/redis-specifications/blob/master/uri/redis.txt):

```
# Default connection
REDIS_URL=redis://127.0.0.1:6379/0

# With authentication
REDIS_URL=redis://password@localhost:6379/0

# With Redis 6+ ACL (username and password)
REDIS_URL=redis://username:password@localhost:6379/0

# With query parameters
REDIS_URL=redis://localhost:6379/0?timeout=10&prefix=app:

# SSL/TLS connection (rediss://)
REDIS_URL=rediss://secure.redis.host:6380/0

# Channel-specific URLs
CACHE_REDIS_URL=redis://cache.redis.local:6379/1?prefix=cache:
SESSION_REDIS_URL=redis://session.redis.local:6379/2?prefix=session:
QUEUE_REDIS_URL=redis://queue.redis.local:6379/3?persistent=true
```

Supported query parameters in URLs:

- `timeout` - Connection timeout in seconds
- `read_write_timeout` - Read/write timeout in seconds
- `persistent` - Use persistent connection (true/false)
- `prefix` - Key prefix for all operations

### Method 2: Using Individual Parameters

[](#method-2-using-individual-parameters)

Configure connections using individual environment variables with the pattern `{CHANNEL}_REDIS_{OPTION}`:

```
# Basic configuration
CACHE_REDIS_HOST=localhost
CACHE_REDIS_PORT=6379
CACHE_REDIS_DB=0
CACHE_REDIS_PASSWORD=secret
CACHE_REDIS_PREFIX=myapp:cache:

# Advanced options
CACHE_REDIS_TIMEOUT=5.0
CACHE_REDIS_READ_WRITE_TIMEOUT=0.0
CACHE_REDIS_PERSISTENT=true
```

**Note**: Individual parameters take precedence over URL parameters if both are specified.

Available Environment Variables
-------------------------------

[](#available-environment-variables)

For each channel, you can configure:

- `{CHANNEL}_REDIS_URL`: Redis connection URL (takes precedence if set)
- `{CHANNEL}_REDIS_HOST`: Redis host (default: 127.0.0.1)
- `{CHANNEL}_REDIS_PORT`: Redis port (default: 6379)
- `{CHANNEL}_REDIS_DB`: Database number (default: 0)
- `{CHANNEL}_REDIS_PASSWORD`: Connection password
- `{CHANNEL}_REDIS_TIMEOUT`: Connection timeout in seconds (default: 5.0)
- `{CHANNEL}_REDIS_READ_WRITE_TIMEOUT`: Read/write timeout (default: 0.0)
- `{CHANNEL}_REDIS_PERSISTENT`: Use persistent connections (default: false)
- `{CHANNEL}_REDIS_PREFIX`: Key prefix for all operations

For the default channel, you can also use the global `REDIS_URL` environment variable.

Advanced Usage
--------------

[](#advanced-usage)

### Multiple Connections for One Service

[](#multiple-connections-for-one-service)

```
use Tourze\RedisDedicatedConnectionBundle\DependencyInjection\DedicatedConnectionHelper;

// In your service configuration
$definition = $container->getDefinition('app.multi_redis_service');
DedicatedConnectionHelper::addMultipleDedicatedConnections(
    $definition,
    ['cache', 'session', 'queue']
);
```

### Direct Connection Reference

[](#direct-connection-reference)

```
services:
  App\Service\CustomService:
    arguments:
      $cacheRedis: '@redis.cache_connection'
      $sessionRedis: '@redis.session_connection'
```

### Checking Connection Existence

[](#checking-connection-existence)

```
use Tourze\RedisDedicatedConnectionBundle\DependencyInjection\DedicatedConnectionHelper;

if (DedicatedConnectionHelper::hasConnection($container, 'cache')) {
    // Connection exists
}
```

Testing
-------

[](#testing)

Run the test suite:

```
# From the monorepo root directory
./vendor/bin/phpunit packages/redis-dedicated-connection-bundle/tests
```

**Note**: Integration tests require a running Redis server. If Redis is not available, these tests will be skipped.

Security
--------

[](#security)

### Security Considerations

[](#security-considerations)

When using this bundle, please consider the following security aspects:

1. **Environment Variables**: Never commit Redis credentials to version control. Use environment variables or secure configuration management systems.
2. **Network Security**:

    - Use SSL/TLS connections (rediss://) for production environments
    - Restrict Redis server access to trusted networks only
    - Configure Redis authentication and ACLs properly
3. **Data Encryption**: This bundle does not encrypt data at the application level. Consider encrypting sensitive data before storing it in Redis.
4. **Connection Limits**: Configure appropriate connection limits to prevent resource exhaustion attacks.

### Reporting Security Vulnerabilities

[](#reporting-security-vulnerabilities)

If you discover a security vulnerability, please send an email to  instead of creating a public issue. All security vulnerabilities will be promptly addressed.

Contributing
------------

[](#contributing)

We welcome contributions! Please see our [contributing guidelines](../../CONTRIBUTING.md) for details.

### Running Tests

[](#running-tests)

Before submitting a pull request, please ensure:

1. All tests pass: `./vendor/bin/phpunit packages/redis-dedicated-connection-bundle/tests`
2. Code follows PSR standards: `./vendor/bin/php-cs-fixer fix packages/redis-dedicated-connection-bundle`
3. Static analysis passes: `./vendor/bin/phpstan analyse packages/redis-dedicated-connection-bundle`

### Reporting Issues

[](#reporting-issues)

If you discover a bug or have a feature request, please create an issue on our [GitHub repository](https://github.com/tourze/php-monorepo/issues).

License
-------

[](#license)

This bundle is licensed under the MIT License. See the [LICENSE](../../LICENSE) file for details.

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance72

Regular maintenance activity

Popularity18

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity36

Early-stage or recently created project

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~13 days

Total

2

Last Release

178d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e354fdb316da535dfa8ba2e9193a473c403b6bc6fb9170778d1dc50e304c6e9d?d=identicon)[tourze](/maintainers/tourze)

---

Tags

symfonydedicated-connection

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tourze-redis-dedicated-connection-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/tourze-redis-dedicated-connection-bundle/health.svg)](https://phpackages.com/packages/tourze-redis-dedicated-connection-bundle)
```

###  Alternatives

[shopware/platform

The Shopware e-commerce core

3.3k1.5M3](/packages/shopware-platform)[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.4k5.6M650](/packages/sylius-sylius)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M152](/packages/sulu-sulu)[contao/core-bundle

Contao Open Source CMS

1231.6M2.3k](/packages/contao-core-bundle)[prestashop/prestashop

PrestaShop is an Open Source e-commerce platform, committed to providing the best shopping cart experience for both merchants and customers.

9.0k15.4k](/packages/prestashop-prestashop)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

595.2M386](/packages/shopware-core)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
