cache/apcu-adapter
PSR-6 cache pool adapter backed by APCu from the PHP Cache organization. Drop-in cache implementation with no configuration required—instantiate ApcuCachePool and start caching. Supports shared PHP-Cache features like tagging and hierarchy via docs.
Installation:
composer require cache/apcu-adapter
No additional configuration is required—APCu must be enabled in your PHP environment (extension=apcu in php.ini).
First Usage:
use Cache\Adapter\Apcu\ApcuCachePool;
$cache = new ApcuCachePool();
$item = $cache->getItem('key');
$item->set('value')->expiresAfter(3600); // Cache for 1 hour
$cache->save($item);
Key Use Case:
Replace Laravel’s default cache driver (e.g., file, redis) in config/cache.php:
'apcu' => [
'driver' => 'cache',
'store' => 'apcu',
],
Then use it via Laravel’s cache facade:
Cache::put('key', 'value', 3600); // Uses ApcuCachePool under the hood
Tag-Based Cache Invalidation:
$cache = new ApcuCachePool();
$cache->getItem('user:1')->tag(['users', 'premium']);
$cache->save($item);
// Later, clear all items tagged 'users':
$cache->deleteItemsMatchingTag('users');
Hierarchical Caching: Leverage APCu’s prefixing to simulate namespaces:
$cache = new ApcuCachePool('prefix_');
$cache->getItem('config')->set('value');
Laravel Integration:
AppServiceProvider:
$this->app->bind('cache.store', function ($app) {
return new ApcuCachePool();
});
Cache::tags() for tag-based invalidation:
Cache::tags(['users'])->put('user:1', $data);
Cache::tags(['users'])->flush(); // Clears all tagged items
expiresAfter() for transient data (e.g., API responses):
$item->expiresAfter(60); // 1-minute TTL
$cache->deleteItems(['key1', 'key2']); // Delete multiple keys
$cache->getItems(['key1', 'key2']); // Fetch multiple keys
$cache = new ApcuCachePool();
$cache->save($cache->getItem('menu')->set(loadMenu()));
cache/array-adapter) for fallback:
$pool = new CachePool([
new ApcuCachePool(),
new ArrayCachePool(), // Fallback
]);
APCu Limitations:
apcu_cache_info().Tagging Quirks:
ApcuCachePool instances (e.g., in clustered environments).TTL Handling:
null to expiresAfter() now defaults to 0 (infinite TTL), but older versions may behave differently. Test thoroughly.var_dump(apcu_cache_info('user')); // Check cache stats
var_dump(apcu_fetch('key')); // Debug raw APCu storage
php.ini:
apc.logtime = 1
apc.enable_cli = 1
Custom Pool Configuration:
Override defaults (e.g., TTL behavior) by extending ApcuCachePool:
class CustomApcuPool extends ApcuCachePool {
protected function getDefaultTTL() {
return 7200; // Default to 2 hours
}
}
APCu Prefix Isolation:
Use unique prefixes per environment (e.g., dev_, prod_) to avoid collisions:
$cache = new ApcuCachePool(env('APP_ENV') . '_');
Integration with Laravel Events:
Listen to cache events (e.g., CacheStoreEvent) to log or transform cached data:
Cache::store('apcu')->extend(function ($store) {
$store->beforeSave(function ($key, $value) {
// Pre-process data
});
});
file, redis, or database for your use case. APCu excels at low-latency, in-memory operations.session.driver can use apcu for high-performance sessions (if APCu is enabled).ApcuCachePool once (e.g., as a singleton) to share tag metadata across requests.How can I help you explore Laravel packages today?