PHPackages                             athwari/laravel-method-cache - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. athwari/laravel-method-cache

ActiveLibrary[Caching](/categories/caching)

athwari/laravel-method-cache
============================

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

v1.0.0(4w ago)00MITPHPPHP ^8.2CI passing

Since May 13Pushed 4w agoCompare

[ Source](https://github.com/athwari/laravel-method-cache)[ Packagist](https://packagist.org/packages/athwari/laravel-method-cache)[ Docs](https://github.com/athwari/laravel-method-cache)[ GitHub Sponsors](https://github.com/athwari)[ RSS](/packages/athwari-laravel-method-cache/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (1)Dependencies (8)Versions (2)Used By (0)

Laravel Method Cache
====================

[](#laravel-method-cache)

[![Latest Version on Packagist](https://camo.githubusercontent.com/3c7446ca75aa907766b8015feca29c1bde317f853792e09cd365d2205dc6cb43/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f617468776172692f6c61726176656c2d6d6574686f642d63616368652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/athwari/laravel-method-cache)[![GitHub Tests Action Status](https://camo.githubusercontent.com/b111ebd96ca2091867fd45a8125a41342f0b2555011c59be8110521e38112bc4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f617468776172692f6c61726176656c2d6d6574686f642d63616368652f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/athwari/laravel-method-cache/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/9de0e0db1c85c2c99b85b087639c2f0f6026ef41b9bb183adb20f413a16a70a0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f617468776172692f6c61726176656c2d6d6574686f642d63616368652f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/athwari/laravel-method-cache/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/c0bca0d6e13e03f0670ef80d29db3cf45015fa4d06afc54f7f24603a75e5c58f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f617468776172692f6c61726176656c2d6d6574686f642d63616368652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/athwari/laravel-method-cache)

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
------------

[](#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
-------------

[](#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

[](#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
-----

[](#usage)

Annotate methods with the `#[Cacheable]` attribute to enable caching.

### Basic 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

[](#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

[](#conditional-caching)

```
#[Cacheable(condition: 'fn($args) => $args[0] > 0')]
public function expensiveCalculation(int $value): int
{
    // Only cache if value > 0
}
```

### Cache invalidation

[](#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
----------

[](#attributes)

- `#[Cacheable]`: Main caching attribute
- `#[NoCache]`: Skip caching for specific methods
- `#[CacheableUntil]`: Cache until a specific time

Exceptions
----------

[](#exceptions)

The package may throw exceptions related to caching or method overriding.

Testing
-------

[](#testing)

Run the test suite with Pest:

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Credits
-------

[](#credits)

- [athwari](https://github.com/athwari)
- [All Contributors](../../contributors)

License
-------

[](#license)

The package is open-source software licensed under the MIT License. Please see [License File](LICENSE.md) for more information.

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance94

Actively maintained with recent releases

Popularity0

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

28d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/244ab61401b3fa5d1b2f60ca760064e43e86831b848ecc253a226ebaa5e771f2?d=identicon)[athwari](/maintainers/athwari)

---

Tags

laravelcacheinterceptionathwarilaravel-method-cache

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/athwari-laravel-method-cache/health.svg)

```
[![Health](https://phpackages.com/badges/athwari-laravel-method-cache/health.svg)](https://phpackages.com/packages/athwari-laravel-method-cache)
```

###  Alternatives

[spatie/laravel-responsecache

Speed up a Laravel application by caching the entire response

2.8k8.7M64](/packages/spatie-laravel-responsecache)[propaganistas/laravel-disposable-email

Disposable email validator

6012.9M7](/packages/propaganistas-laravel-disposable-email)[awssat/laravel-visits

Laravel Redis visits counter for Eloquent models

975169.8k2](/packages/awssat-laravel-visits)[iazaran/smart-cache

Smart Cache is a caching optimization package designed to enhance the way your Laravel application handles data caching. It intelligently manages large data sets by compressing, chunking, or applying other optimization strategies to keep your application performant and efficient.

2119.7k](/packages/iazaran-smart-cache)[harris21/laravel-fuse

Circuit breaker for Laravel queue jobs. Protect your workers from cascading failures.

24740.3k](/packages/harris21-laravel-fuse)[omaralalwi/lexi-translate

Laravel translation package with morph relationships and caching.

748.5k3](/packages/omaralalwi-lexi-translate)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
