cache/prefixed-cache
PSR-6 cache pool decorator that prefixes all cache item keys with a predefined string. Wrap any PSR-6 cache (e.g., Redis) to safely namespace entries per app, tenant, or module. Part of the PHP-Cache ecosystem.
Psr\Cache\CacheItemPoolInterface. This ensures compatibility with Laravel’s Cache facade and underlying storage backends (Redis, Memcached, etc.).tenant_1:user:123). This addresses Laravel’s need for clean separation in shared environments without modifying core logic.CacheManager and service container.config/cache.php to define prefixed stores (e.g., prefixed_redis) alongside default drivers.CacheManager or use a wrapper (e.g., spatie/laravel-cache-prefix) for Laravel-specific features like service providers or facades.php-cache/redis-cache, php-cache/memcached-cache). Laravel’s default drivers (Redis, Memcached) are compatible out-of-the-box.Illuminate\Cache\DecoratedCache).cachetools/core if needed.spatie/laravel-cache-prefix.Cache::prefix() suffice?staging:)?Cache::forget()) handle prefixed keys?spatie/laravel-cache-prefix (Laravel-specific) or cachetools/core (more active maintenance).Cache::store() method (for per-request prefixes) is sufficient.redis, memcached, file, database) via PSR-6 adapters.Psr\Cache\CacheItemPoolInterface for dependency injection.acme:api:v1 vs. acme:api:v2).composer require cache/prefixed-cache php-cache/redis-cache
$redisPool = new RedisCachePool(new Redis());
$prefixedPool = new PrefixedCachePool($redisPool, 'tenant_1_');
CacheManager in AppServiceProvider:
Cache::extend('prefixed_redis', function () {
$pool = Cache::store('redis')->getStore();
return new PrefixedCachePool($pool, config('cache.prefixes.default'));
});
spatie/laravel-cache-prefix for out-of-the-box Laravel support.Cache::store('prefixed_redis')).cachetools/core) can replace it without breaking changes.tenant_{id}_, feature_{name}_) and document key formats.tenant_1_key vs. tenant_2_key).Cache::prefix() calls, reducing boilerplate.php-cache org for updates or maintaining a fork.config/cache.php.Cache::debug() to log prefixed keys:
Cache::debug(function ($key) {
logger()->debug("Prefixed cache key: {$key}");
});
redis-memory-usage.tenant_{id}_*).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Prefix exceeds key length limits | Cache misses or silent failures | Validate prefix length in config/cache.php. |
| Package abandonment | No security/bug fixes | Fork the package or migrate to cachetools/core. |
| Key collision in non-prefixed store | Data leakage between tenants | Audit all cache stores; enforce prefixing where needed. |
| Cache invalidation issues | Stale data in prefixed stores | Test Cache::forget() with prefixed keys; ensure wildcard support. |
| Debugging difficulties | Harder to inspect prefixed keys | Log prefixed keys in debug mode; use tools like Cache::getStore()->getItem($key). |
How can I help you explore Laravel packages today?