tourze/redis-dedicated-connection-bundle
Composer 安装命令:
composer require tourze/redis-dedicated-connection-bundle
包简介
为服务自动创建和注入专用的 Redis 连接,支持属性、标签、环境变量等多种配置方式
README 文档
README
This bundle provides automatic creation and injection of dedicated Redis connections for Symfony services, supporting attributes, tags, and environment variable configuration.
Table of Contents
- Features
- Dependencies
- Installation
- Usage
- Configuration
- Available Environment Variables
- Advanced Usage
- Testing
- Security
- Contributing
- License
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
- PHP 8.1 or higher
- Symfony 6.4 or higher
- PHP Redis extension
- tourze/symfony-runtime-context-bundle
Installation
composer require tourze/redis-dedicated-connection-bundle
Note: This bundle requires the PHP Redis extension to be installed:
pecl install redis
Usage
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
services: App\Service\SessionService: arguments: $redis: '@redis' tags: - { name: 'redis.dedicated_connection', channel: 'session' }
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
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
Environment Variables
The bundle supports two ways to configure Redis connections:
Method 1: Using Redis URL (Recommended)
Configure connections using Redis URLs following the official Redis URI scheme:
# 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 secondsread_write_timeout- Read/write timeout in secondspersistent- Use persistent connection (true/false)prefix- Key prefix for all operations
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
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
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
services: App\Service\CustomService: arguments: $cacheRedis: '@redis.cache_connection' $sessionRedis: '@redis.session_connection'
Checking Connection Existence
use Tourze\RedisDedicatedConnectionBundle\DependencyInjection\DedicatedConnectionHelper; if (DedicatedConnectionHelper::hasConnection($container, 'cache')) { // Connection exists }
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 Considerations
When using this bundle, please consider the following security aspects:
-
Environment Variables: Never commit Redis credentials to version control. Use environment variables or secure configuration management systems.
-
Network Security:
- Use SSL/TLS connections (rediss://) for production environments
- Restrict Redis server access to trusted networks only
- Configure Redis authentication and ACLs properly
-
Data Encryption: This bundle does not encrypt data at the application level. Consider encrypting sensitive data before storing it in Redis.
-
Connection Limits: Configure appropriate connection limits to prevent resource exhaustion attacks.
Reporting Security Vulnerabilities
If you discover a security vulnerability, please send an email to security@tourze.com instead of creating a public issue. All security vulnerabilities will be promptly addressed.
Contributing
We welcome contributions! Please see our contributing guidelines for details.
Running Tests
Before submitting a pull request, please ensure:
- All tests pass:
./vendor/bin/phpunit packages/redis-dedicated-connection-bundle/tests - Code follows PSR standards:
./vendor/bin/php-cs-fixer fix packages/redis-dedicated-connection-bundle - Static analysis passes:
./vendor/bin/phpstan analyse packages/redis-dedicated-connection-bundle
Reporting Issues
If you discover a bug or have a feature request, please create an issue on our GitHub repository.
License
This bundle is licensed under the MIT License. See the LICENSE file for details.
tourze/redis-dedicated-connection-bundle 适用场景与选型建议
tourze/redis-dedicated-connection-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.17k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 10 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「dedicated-connection」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tourze/redis-dedicated-connection-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tourze/redis-dedicated-connection-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tourze/redis-dedicated-connection-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The bundle for easy using json-rpc api on your project
Bundle Symfony DaplosBundle
Biblioteca PHP pura para pagamentos SISP/Vinti4 de Cabo Verde.
Open-source place library (OpenStreetMap point/polygon places) + Symfony UX Map integration: clustered, thumbnail-on-zoom item markers
Official BugBoard SDK for PHP — report errors as cards on your project board, with first-class Laravel and Symfony support.
A maintained, security-focused continuation of KCFinder for modern production environments
统计信息
- 总下载量: 6.17k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 9
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-31