Skip to content

Conversation

@deemonic
Copy link
Collaborator

@deemonic deemonic commented Dec 3, 2025

Summary

  • Adds the ability to specify a custom cache driver for Blasp via config or environment variable
  • Useful for environments like Laravel Vapor where DynamoDB has size limits that can be exceeded when caching profanity expressions
  • Allows users to switch to a different cache driver (e.g., Redis) to avoid these limitations

Changes

  • Added cache_driver config option to config/config.php
  • Added BLASP_CACHE_DRIVER environment variable support
  • Refactored ConfigurationLoader to use a configurable cache store via getCache() method
  • Added 6 new tests for cache driver configuration
  • Updated README with cache driver documentation

Usage

// config/blasp.php
'cache_driver' => env('BLASP_CACHE_DRIVER'),
BLASP_CACHE_DRIVER=redis

Credits

This PR is based on the original work in #25 by @dimzeta, adapted for Blasp v3.0 architecture.

Closes #25

Test plan

  • All existing tests pass (154 tests, 871 assertions)
  • New cache driver tests pass (6 tests)
  • Test with BLASP_CACHE_DRIVER=array environment variable
  • Test with BLASP_CACHE_DRIVER=redis if available

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added configurable cache driver selection via environment variable and config setting.
  • Documentation

    • Updated README with instructions and rationale for cache driver configuration and alternatives.
  • Tests

    • Added comprehensive tests covering default/custom cache drivers, persistence, key tracking, and cache clearing across driver switches.

✏️ Tip: You can customize this high-level summary in your review settings.

Add the ability to specify a custom cache driver for Blasp via config
or environment variable. This is useful for environments like Laravel
Vapor where DynamoDB has size limits that can be exceeded when caching
profanity expressions.

- Add `cache_driver` config option with `BLASP_CACHE_DRIVER` env support
- Refactor ConfigurationLoader to use configurable cache store
- Add tests for cache driver configuration
- Update README with cache driver documentation

Based on PR #25 by @dimzeta - adapted for Blasp v3.0 architecture.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@coderabbitai
Copy link

coderabbitai bot commented Dec 3, 2025

Walkthrough

Adds a configurable cache driver option (env/config) and routes all cache operations in ConfigurationLoader through a new getCache() accessor; updates README and tests to validate custom driver selection, key tracking, and cache clearing behavior.

Changes

Cohort / File(s) Summary
Configuration & Documentation
README.md, config/config.php
Add cache_driver config entry (driven by BLASP_CACHE_DRIVER) and README section describing how to configure a custom cache driver and rationale for environments like Laravel Vapor. README additions appear duplicated in the diff.
Caching Implementation
src/Config/ConfigurationLoader.php
Add private static getCache() to select configured cache store or default; replace direct Cache::get/put/forget calls with self::getCache()->get/put/forget across caching methods.
Tests
tests/CacheDriverConfigurationTest.php, tests/TestCase.php
Add CacheDriverConfigurationTest with multiple tests for driver selection, caching, key tracking, and clearing; propagate blasp.cache_driver into test setup in TestCase.php.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Verify correctness of getCache() selection logic and fallback to default store.
  • Confirm all Cache facade usages were replaced and no calls remain that bypass the configured driver.
  • Review new tests for proper setup/teardown of cache state and that they don't depend on external cache services (use of array driver).
  • Inspect README duplication and ensure documentation appears once and in the intended location.

Poem

"I hopped through configs, soft and snappy,
Switched drivers quick to keep caches happy.
Keys tracked and cleared with a twitch of my paw,
Redis or array — no DynamoDB flaw.
— a rabbit, in a caching shawl 🐇"

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 35.71% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat: add custom cache driver configuration' accurately and concisely summarizes the main change: introducing custom cache driver configuration support.
Linked Issues check ✅ Passed The PR successfully implements all coding requirements from issue #25: custom cache driver config option, BLASP_CACHE_DRIVER env variable support, refactored ConfigurationLoader with getCache() method, comprehensive unit tests, and README documentation.
Out of Scope Changes check ✅ Passed All changes are directly aligned with issue #25 objectives: config additions, environment variable support, cache method refactoring, tests, and documentation updates are all in-scope.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feature/cache-driver-configuration

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/Config/ConfigurationLoader.php (1)

25-35: Consider more explicit null check.

The current implementation works, but using a truthy check means an empty string "" would call Cache::store("") instead of using the default. While unlikely to occur in practice, a more explicit check would be clearer.

Apply this diff for more explicit behavior:

 private static function getCache(): \Illuminate\Contracts\Cache\Repository
 {
     $driver = config('blasp.cache_driver');
 
-    return $driver ? Cache::store($driver) : Cache::store();
+    return $driver !== null ? Cache::store($driver) : Cache::store();
 }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8352bea and f412b8a.

📒 Files selected for processing (5)
  • README.md (1 hunks)
  • config/config.php (1 hunks)
  • src/Config/ConfigurationLoader.php (5 hunks)
  • tests/CacheDriverConfigurationTest.php (1 hunks)
  • tests/TestCase.php (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
tests/CacheDriverConfigurationTest.php (3)
src/Facades/Blasp.php (1)
  • Blasp (21-127)
src/Config/ConfigurationLoader.php (3)
  • ConfigurationLoader (21-369)
  • load (50-93)
  • clearCache (358-368)
tests/TestCase.php (2)
  • TestCase (9-31)
  • setUp (19-30)
src/Config/ConfigurationLoader.php (3)
src/Registries/DetectionStrategyRegistry.php (1)
  • get (39-48)
src/Config/MultiLanguageDetectionConfig.php (1)
  • getCurrentLanguage (47-50)
src/Contracts/MultiLanguageConfigInterface.php (1)
  • getCurrentLanguage (14-14)
🔇 Additional comments (14)
README.md (1)

269-288: LGTM! Clear and helpful documentation.

The cache driver configuration section is well-written and provides practical context for why this feature is needed, particularly for environments like Laravel Vapor with DynamoDB limitations.

config/config.php (1)

26-40: LGTM! Well-documented configuration option.

The cache driver configuration is properly implemented with comprehensive inline documentation. The null default (when env var is not set) correctly allows fallback to Laravel's default cache driver.

tests/TestCase.php (1)

29-29: LGTM! Consistent test setup.

The cache driver configuration is properly propagated to the test environment, following the same pattern as other configuration settings.

src/Config/ConfigurationLoader.php (4)

261-272: LGTM! Cache retrieval properly refactored.

The cache retrieval operation is correctly routed through the getCache() helper, ensuring it respects the configured cache driver.


309-334: LGTM! Cache storage properly refactored.

The cache storage operation is correctly routed through the getCache() helper. The formatting cleanup is also appropriate.


342-351: LGTM! Efficient cache instance reuse.

The implementation correctly caches the repository instance in a local variable, avoiding redundant getCache() calls within the method. This is an efficient pattern.


358-368: LGTM! Cache clearing properly refactored.

The cache clearing logic is correctly refactored to use the configured cache driver. All tracked keys and the tracking key itself are properly cleared from the correct cache store.

tests/CacheDriverConfigurationTest.php (7)

1-27: LGTM! Proper test setup with good isolation.

The test class is properly structured with appropriate mocking and cache flushing to ensure test isolation. The mock expression generator returning an empty array is sufficient for testing cache driver behavior.


28-36: LGTM! Validates default cache driver behavior.

The test correctly verifies that when cache_driver is not configured, the default Laravel cache driver is used and cache key tracking works as expected.


38-49: LGTM! Validates custom cache driver selection.

The test correctly verifies that when a custom cache driver is configured, all cache operations use that driver. The assertion correctly checks the 'array' store specifically.


51-68: LGTM! Validates cache clearing with custom driver.

The test correctly verifies that the clearCache() method respects the configured cache driver and clears from the correct cache store.


70-88: LGTM! Smart test for cache persistence across instances.

The test cleverly uses different mock behavior for the second loader to prove that cached data is reused. This validates that caching works correctly across different ConfigurationLoader instances.


90-109: LGTM! Thorough validation of cache key tracking.

The test comprehensively validates that cache key tracking works with custom drivers, including verifying that all tracked keys actually exist in the cache store.


111-127: LGTM! Validates cache clearing respects current driver.

The test verifies that clearCache() clears from the currently configured cache driver. While the test doesn't explicitly switch drivers mid-test, it effectively validates that the static clearCache() method uses the current configuration.

Address CodeRabbit nitpick - use `!== null` instead of truthy check
to make intent clearer and handle edge case of empty string config.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/Config/ConfigurationLoader.php (1)

25-35: getCache() abstraction is clean; consider whether to fail fast or fall back on invalid driver names

The helper cleanly centralizes cache store selection and honors blasp.cache_driver while defaulting to the main store when unset, which fits the PR goals.

One thing to think about: if blasp.cache_driver is set to a non-existent store, Cache::store($driver) will raise an InvalidArgumentException and break configuration loading at runtime. That may be exactly what you want (fail-fast on misconfiguration), but if you’d prefer to degrade more gracefully you could optionally catch that exception and fall back to the default store (possibly logging a warning) instead of hard-failing.

For example, if you decide you want a soft fallback:

-    private static function getCache(): \Illuminate\Contracts\Cache\Repository
-    {
-        $driver = config('blasp.cache_driver');
-
-        return $driver !== null ? Cache::store($driver) : Cache::store();
-    }
+    private static function getCache(): \Illuminate\Contracts\Cache\Repository
+    {
+        $driver = config('blasp.cache_driver');
+
+        if ($driver === null) {
+            return Cache::store();
+        }
+
+        try {
+            return Cache::store($driver);
+        } catch (\InvalidArgumentException $e) {
+            // Optionally log here, then fall back to default store
+            return Cache::store();
+        }
+    }

If strict failure on bad configuration is desired, the current implementation is fine as-is — just worth making that decision explicit.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f412b8a and 728aaa7.

📒 Files selected for processing (1)
  • src/Config/ConfigurationLoader.php (5 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/Config/ConfigurationLoader.php (3)
src/Registries/DetectionStrategyRegistry.php (1)
  • get (39-48)
src/Config/MultiLanguageDetectionConfig.php (1)
  • getCurrentLanguage (47-50)
src/Contracts/MultiLanguageConfigInterface.php (1)
  • getCurrentLanguage (14-14)
🔇 Additional comments (1)
src/Config/ConfigurationLoader.php (1)

264-265: Consistent routing of cache operations through getCache() aligns with configurable driver support

All cache reads/writes/clears now go through self::getCache() (loadFromCacheOrGenerate, cacheConfiguration, trackCacheKey, clearCache), so the configured blasp.cache_driver is honored uniformly for:

  • loading and storing both single-language and multi-language configs,
  • tracking cached keys in blasp_cache_keys, and
  • clearing all tracked entries via clearCache().

This keeps behavior coherent across stores and matches the PR objective of allowing a custom cache driver without leaving any operations on the default store.

Also applies to: 332-333, 344-350, 360-368

@deemonic deemonic merged commit c2b8d31 into main Dec 3, 2025
3 checks passed
@deemonic deemonic deleted the feature/cache-driver-configuration branch December 3, 2025 13:11
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