Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Caching Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

  • Install via Composer: composer require nette/caching
  • First use case: Cache a simple expensive operation (e.g., database query result) to avoid redundant work:
    use 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'];
    });
    
  • Key files to inspect: Cache.php (core), Storages/ directory (backends: FileStorage, MemoryStorage, RedisStorage, etc.)
  • Start with FileStorage for local development; switch to RedisStorage for production scaling.

Implementation Patterns

  • Tag-based invalidation: Use tags to bust related cache entries at once:
    $cache->save('user_123_profile', $profileData, [
        Cache::TAGS => ['user-profiles', 'user_123']
    ]);
    // Later invalidate all user-related caches:
    $cache->clean([Cache::TAGS => ['user_123']]);
    
  • Callback dependency tracking: Automatically expire cache when dependencies change (e.g., filemtime, database rows):
    $cache->load('config', function (&$dependencies) {
        $dependencies[Cache::EXPIRATION] = filemtime('config.yaml');
        return parse_yaml('config.yaml');
    });
    
  • Cache as service in Laravel: Bind 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')));
    });
    
  • Multi-layer caching: Combine MemoryStorage (request-scoped) + FileStorage (cross-request) for hot paths.

Gotchas and Tips

  • Cache key restrictions: Keys must be strings; avoid special characters and length > 200 chars for compatibility (truncated silently in some backends).
  • Expiration quirks: Cache::EXPIRATION accepts timestamps or relative strings (e.g., '+ 5 minutes'), but PHP’s strtotime() parsing applies — be cautious with daylight saving.
  • Serialization: Cache values are serialized automatically (using Nette’s smart serialization); avoid caching closures or resources. Explicitly serialize with serialize() if custom control needed.
  • Debugging: Enable Cache::getStorage()->getContext() logging; check generated cache files in storage/cache/ for keys/tags.
  • Extend backends: Implement Nette\Caching\IStorage interface for custom backends (e.g., database, Memcached with stats).
  • Laravel interoperability: Use 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.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport