nette/caching
High-performance caching library from Nette. Provides flexible cache storage backends, automatic expiration, dependency-based invalidation, and easy integration for PHP apps. Ideal for speeding up responses and reducing database or API load.
composer require nette/cachinguse Nette\Caching\Cache;
use Nette\Caching\Storages\FileStorage;
$storage = new FileStorage(__DIR__ . '/cache');
$cache = new Cache($storage);
$data = $cache->load('expensive_query', function (&$dependencies) {
// Simulate expensive operation
return ['result' => 'computed_data'];
});
Cache.php (core), Storages/ directory (backends: FileStorage, MemoryStorage, RedisStorage, etc.)FileStorage for local development; switch to RedisStorage for production scaling.$cache->save('user_123_profile', $profileData, [
Cache::TAGS => ['user-profiles', 'user_123']
]);
// Later invalidate all user-related caches:
$cache->clean([Cache::TAGS => ['user_123']]);
$cache->load('config', function (&$dependencies) {
$dependencies[Cache::EXPIRATION] = filemtime('config.yaml');
return parse_yaml('config.yaml');
});
Nette\Caching\Cache as a singleton and inject into services or commands:
// In AppServiceProvider
$this->app->singleton(\Nette\Caching\Cache::class, function ($app) {
return new Cache(new \Nette\Caching\Storages\PhpFilesStorage(storage_path('cache')));
});
MemoryStorage (request-scoped) + FileStorage (cross-request) for hot paths.Cache::EXPIRATION accepts timestamps or relative strings (e.g., '+ 5 minutes'), but PHP’s strtotime() parsing applies — be cautious with daylight saving.serialize() if custom control needed.Cache::getStorage()->getContext() logging; check generated cache files in storage/cache/ for keys/tags.Nette\Caching\IStorage interface for custom backends (e.g., database, Memcached with stats).nette/caching for non-Eloquent caching (e.g., API response caching, template precompilation) while keeping Laravel’s Cache facade for framework integration — avoid conflating the two systems.How can I help you explore Laravel packages today?