athwari / laravel-method-cache
Attribute-based method caching for Laravel using method overriding proxy system
Fund package maintenance!
Requires
- php: ^8.2
- athwari/laravel-method-overrider: ^1.0.0
- illuminate/cache: ^11.0|^12.0
- illuminate/support: ^11.0|^12.0
Requires (Dev)
- laravel/pint: ^1.13
- orchestra/testbench: ^9.0.0||^10.0.0||^11.0.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.4
This package is not auto-updated.
Last update: 2026-05-13 10:19:58 UTC
README
Attribute-based method caching for Laravel applications using method overriding proxy system.
This package allows you to cache method results by annotating them with attributes, automatically intercepting calls to store and retrieve cached results.
Installation
Install the package with Composer:
composer require athwari/laravel-method-cache
Laravel package auto-discovery is supported, so the service provider and facade are registered automatically.
Configuration
Publish the configuration file:
php artisan vendor:publish --tag=method-cache-config
The published config file is located at config/method-cache.php.
Default configuration
return [ 'default_ttl' => 3600, // Default TTL in seconds 'cache_store' => null, // Cache store to use, null for default 'key_prefix' => 'method_cache', 'enable_logging' => false, 'log_channel' => 'default', 'auto_flush_events' => [], // Eloquent events to auto-flush, e.g., ['saved', 'deleted'] ];
Usage
Annotate methods with the #[Cacheable] attribute to enable caching.
Basic caching
use Athwari\MethodCache\Attributes\Cacheable; class UserService { #[Cacheable(ttl: 300)] // Cache for 5 minutes public function getUser(int $id): User { // Expensive operation return User::find($id); } }
Custom key and tags
#[Cacheable(key: 'user_profile_{id}', tags: ['users'])] public function getUserProfile(int $id): array { return [ 'user' => User::find($id), 'posts' => Post::where('user_id', $id)->get(), ]; }
Conditional caching
#[Cacheable(condition: 'fn($args) => $args[0] > 0')] public function expensiveCalculation(int $value): int { // Only cache if value > 0 }
Cache invalidation
Use the facade to flush caches:
use Athwari\MethodCache\Facades\MethodCache; // Flush all MethodCache::flushAll(); // Flush by tags MethodCache::flushTags(['users']); // Flush by class MethodCache::flushClass(UserService::class);
Or use the console command:
php artisan method-cache:flush --tags=users php artisan method-cache:flush --class=App\\Services\\UserService
Attributes
#[Cacheable]: Main caching attribute#[NoCache]: Skip caching for specific methods#[CacheableUntil]: Cache until a specific time
Exceptions
The package may throw exceptions related to caching or method overriding.
Testing
Run the test suite with Pest:
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The package is open-source software licensed under the MIT License. Please see License File for more information.