athwari/laravel-method-cache

Attribute-based method caching for Laravel using method overriding proxy system

Maintainers

Package info

github.com/athwari/laravel-method-cache

pkg:composer/athwari/laravel-method-cache

Fund package maintenance!

athwari

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-05-13 03:50 UTC

This package is not auto-updated.

Last update: 2026-05-13 10:19:58 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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.