cache/hierarchical-cache
PSR-6 cache pool with hierarchical keys and selective flushing. Organize cache entries like |users|:uid|followers| and invalidate whole branches (e.g., all followers) without clearing everything. Part of the PHP Cache organization.
Install the package to add hierarchical cache invalidation to any PSR-6 pool. It doesn’t provide a cache driver itself—instead, it wraps existing PSR-6 pools (e.g., symfony/cache, php-cache/file-cache) to enable prefix-based flushing of nested keys.
First use case: Invalidate all user-specific cache entries (e.g., |users|123|profile, |users|123|settings, |users|123|recent-activity) by clearing just |users|123|.
Get started in 3 steps:
composer require cache/hierarchical-cacheuse Cache\HierarchicalCache\HierarchicalCachePool;
$pool = new HierarchicalCachePool($yourPsr6Pool);
|-delimited keys and flush with a prefix ending in |:
$pool->clear('|users|123|'); // clears all keys starting with this prefix
Start with the official hierarchy docs for syntax and examples.
| as delimiter (e.g., |products|{sku}|inventory|{warehouse} or |comments|{post_id}|{comment_id}). Leading | is required.$pool->clear('|users|123|');$pool->clear('|products|sku-789|variants|');|users|123 won’t match anything—always end prefixes with |.CacheManager to register a custom hierarchical-redis store.// In App\Extensions\HierarchicalRedisAdapter
$pool = new HierarchicalCachePool(new \Cache\Adapter\Redis\RedisPool($client));
TaggableCache after HierarchicalCachePool for dual invalidation (e.g., |users|123|* + tag:recent-activity).ArrayCachePool with HierarchicalCachePool for deterministic, in-memory tests of hierarchy logic.|—no configuration exists. Keys without leading | break hierarchy (e.g., users|123 won’t match |users|123|*).| is mandatory: clear('|users|123') hits no items; clear('|users|123|') flushes |users|123|profile, |users|123|orders|*, etc.clear() scans all items in the underlying pool (O(n)). Avoid broad prefixes like | or |app| on production caches—preflight with dump() in non-prod.getItems(), hasItem(), deleteItem()). Avoid pools with aggressive LRU eviction that skip items during iteration.HierarchicalCachePool and override getKeysMatching() to use native prefix scans (e.g., Redis KEYS "|users|*" or memcached scan), bypassing full pool iteration.TagAwareAdapter (PSR-6) or CacheManager with custom invalidation logic—especially if PSR-16 is acceptable.How can I help you explore Laravel packages today?