Skip to content

Conversation

@huangdijia
Copy link
Member

Summary

This PR refactors the cache configuration structure to follow Laravel's convention with a stores-based pattern, improving flexibility and consistency with other modern PHP frameworks.

Changes

Configuration Structure (src/cache/publish/cache.php)

  • Introduced default key for driver selection with environment variable support (CACHE_DRIVER)
  • Moved cache configurations under stores array
  • Maintained backward compatibility with existing driver configurations

CacheManager Updates (src/cache/src/CacheManager.php)

  • Modified getDriver(?string $name = null) to accept null parameter and read from default config
  • Updated config resolution path from cache.{$name} to cache.stores.{$name}
  • Improved call() method with proper type hints and simplified parameter signature
  • Better separation of concerns between driver selection and configuration retrieval

Test Updates (src/cache/tests/Cases/RedisDriverTest.php)

  • Updated test configurations to match new structure
  • Ensured all existing tests pass with the new configuration format

Benefits

  • Better Organization: Clearer separation between default driver selection and store configurations
  • Framework Consistency: Aligns with Laravel's caching conventions, making it easier for developers familiar with Laravel
  • Environment Configuration: Native support for CACHE_DRIVER environment variable
  • Flexibility: Easier to manage multiple cache stores and switch between them
  • Maintainability: More intuitive structure for future enhancements

Migration Guide

Users need to update their config/autoload/cache.php file from:

return [
    'default' => [
        'driver' => RedisDriver::class,
        'packer' => PhpSerializerPacker::class,
        'prefix' => 'c:',
    ],
];

To:

return [
    'default' => env('CACHE_DRIVER', 'default'),
    'stores' => [
        'default' => [
            'driver' => RedisDriver::class,
            'packer' => PhpSerializerPacker::class,
            'prefix' => 'c:',
        ],
    ],
];

Test Plan

  • Existing unit tests updated and passing
  • Verified CacheManager correctly resolves default driver
  • Tested environment variable configuration
  • Confirmed backward compatibility with driver selection
  • Manual testing with different cache stores recommended
  • Verify in production-like environment before merging

@huangdijia huangdijia marked this pull request as draft November 3, 2025 14:40
This refactors the cache configuration to follow Laravel's convention with a stores-based pattern, improving flexibility and consistency with other frameworks.

Changes:
- Restructured cache configuration with 'default' driver selection and 'stores' array
- Added support for CACHE_DRIVER environment variable
- Updated CacheManager::getDriver() to accept null and read from default config
- Changed config path from 'cache.{$name}' to 'cache.stores.{$name}'
- Simplified CacheManager::call() method signature with proper type hints
- Updated test configurations to match new structure

Benefits:
- More intuitive configuration structure
- Better alignment with Laravel's caching conventions
- Easier to manage multiple cache drivers
- Improved environment-based driver selection
Simplified the description for cache configuration changes in the changelog by removing reference to Laravel-style stores pattern.
@huangdijia huangdijia force-pushed the refactor/cache-stores-configuration branch from 41035d2 to 2e7c430 Compare November 3, 2025 14:44
@huangdijia huangdijia marked this pull request as ready for review November 3, 2025 16:34
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the cache configuration structure to be more consistent with other components like logging. The main change introduces a stores array to hold cache driver configurations, with a top-level default key that specifies which store to use by default. Backward compatibility is maintained through automatic migration logic in CacheManager.

  • Introduced a new configuration structure with default key pointing to a store name and stores array containing driver configurations
  • Added backward compatibility logic to automatically migrate old configuration format
  • Updated tests and documentation to reflect the new structure

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/cache/src/CacheManager.php Added backward compatibility migration logic and updated driver lookup to use cache.stores.* path
src/cache/tests/Cases/RedisDriverTest.php Updated test configuration to use new stores structure
src/cache/publish/cache.php Migrated publish configuration to new format with environment variable support
CHANGELOG-3.2.md Added documentation of the breaking change with before/after examples

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@huangdijia huangdijia changed the title refactor(cache): adopt Laravel-style stores configuration pattern refactor(cache): adopt stores configuration pattern Nov 4, 2025
@huangdijia huangdijia changed the title refactor(cache): adopt stores configuration pattern refactor(cache): refactor cache configuration pattern Nov 4, 2025
@limingxinleo limingxinleo merged commit 37b1cd0 into hyperf:3.2 Nov 4, 2025
67 checks passed
@huangdijia huangdijia deleted the refactor/cache-stores-configuration branch November 4, 2025 10:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants