PHPackages                             fereydooni/cachable-methods - 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. fereydooni/cachable-methods

ActiveLibrary[Caching](/categories/caching)

fereydooni/cachable-methods
===========================

A Laravel package for method caching using PHP attributes

00PHPCI failing

Since Apr 24Pushed 1y ago1 watchersCompare

[ Source](https://github.com/Behnamfe76/cachable-methods)[ Packagist](https://packagist.org/packages/fereydooni/cachable-methods)[ RSS](/packages/fereydooni-cachable-methods/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependenciesVersions (1)Used By (0)

Cachable Methods
================

[](#cachable-methods)

A Laravel package for method caching using PHP attributes. This package allows you to cache method results by simply adding an attribute to your methods.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require fereydooni/cachable-methods
```

The package will automatically register the service provider.

If you want to customize the configuration, you can publish it:

```
php artisan vendor:publish --tag="cachable-methods-config"
```

This will publish the configuration file to `config/cachable-methods.php`.

Usage
-----

[](#usage)

### Basic Usage

[](#basic-usage)

Simply add the `Cacheable` attribute to any method you want to cache:

```
use Fereydooni\CachableMethods\Attributes\Cacheable;

class UserService
{
    #[Cacheable(ttl: 3600)]
    public function getUser(int $id): User
    {
        return User::findOrFail($id);
    }
}
```

Then, to call the method with caching enabled, use the `CachableMethods` facade:

```
use Fereydooni\CachableMethods\Facades\CachableMethods;

$userService = new UserService();
$user = CachableMethods::call($userService, 'getUser', [1]);
```

### Customizing Cache Keys

[](#customizing-cache-keys)

You can customize the cache key by providing a `key` parameter:

```
#[Cacheable(ttl: 3600, key: 'user_{0}')]
public function getUser(int $id): User
{
    return User::findOrFail($id);
}
```

The placeholder `{0}` will be replaced with the first parameter passed to the method.

### Using Cache Tags

[](#using-cache-tags)

You can tag your cached results for easier invalidation:

```
#[Cacheable(ttl: 3600, tags: ['users', 'profiles'])]
public function getUserProfile(int $id): array
{
    return User::with('profile')->findOrFail($id)->toArray();
}
```

### Skipping Cache

[](#skipping-cache)

You can skip the cache for a specific method call:

```
$freshUser = CachableMethods::call($userService, 'getUser', [1], ['skip_cache' => true]);
```

### Invalidating Cache

[](#invalidating-cache)

To invalidate cache by tags:

```
use Fereydooni\CachableMethods\Facades\CachableMethods;

// Flush all caches with the 'users' tag
CachableMethods::flushByTags(['users']);
```

Configuration
-------------

[](#configuration)

The package can be configured in `config/cachable-methods.php`:

```
return [
    // Enable or disable caching globally
    'enabled' => env('CACHABLE_METHODS_ENABLED', true),

    // Default TTL in seconds
    'default_ttl' => env('CACHABLE_METHODS_TTL', 3600),

    // Cache store to use (null for default)
    'store' => env('CACHABLE_METHODS_STORE', null),

    // Parameter name to skip cache
    'skip_cache_flag' => 'skip_cache',

    // Cache key prefix
    'key_prefix' => 'cachable_',
];
```

Package Structure
-----------------

[](#package-structure)

```
cachable-methods/
├── config/
│   └── cachable-methods.php       # Configuration file
├── src/
│   ├── Attributes/
│   │   └── Cacheable.php         # The main attribute class
│   ├── Contracts/
│   │   └── CacheHandlerInterface.php  # Interface for the cache handler
│   ├── Facades/
│   │   └── CachableMethods.php    # Facade for easy access
│   ├── Services/
│   │   ├── CacheHandler.php      # Main caching logic
│   │   └── MethodProxy.php       # Method call interceptor
│   └── CachableMethodsServiceProvider.php  # Service provider
├── tests/
│   ├── Feature/                  # Feature tests
│   ├── Unit/                     # Unit tests
│   ├── Pest.php                  # Pest configuration
│   └── TestCase.php              # Base test case
├── .github/
│   └── workflows/
│       └── tests.yml             # CI workflow
├── composer.json                 # Composer configuration
├── LICENSE                      # MIT License
├── phpstan.neon                  # PHPStan configuration
└── README.md                     # This file

```

Error Handling
--------------

[](#error-handling)

The package gracefully handles cache store failures. If the cache is unavailable, the method will execute normally (uncached) and errors will be logged.

Testing
-------

[](#testing)

```
composer test
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE) for more information.

###  Health Score

14

—

LowBetter than 1% of packages

Maintenance32

Infrequent updates — may be unmaintained

Popularity0

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity16

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/101217538?v=4)[Behnam Fereydooni](/maintainers/Behnamfe76)[@Behnamfe76](https://github.com/Behnamfe76)

---

Top Contributors

[![Behnamfe76](https://avatars.githubusercontent.com/u/101217538?v=4)](https://github.com/Behnamfe76 "Behnamfe76 (17 commits)")

### Embed Badge

![Health badge](/badges/fereydooni-cachable-methods/health.svg)

```
[![Health](https://phpackages.com/badges/fereydooni-cachable-methods/health.svg)](https://phpackages.com/packages/fereydooni-cachable-methods)
```

###  Alternatives

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

21114.2k](/packages/iazaran-smart-cache)[phpfastcache/phpssdb

PHP SSDB Driver for phpFastCache

14736.9k1](/packages/phpfastcache-phpssdb)[rapidwebltd/rw-file-cache

RW File Cache is a PHP File-based Caching Library. Its syntax is designed to closely resemble the PHP memcache extension.

15196.8k7](/packages/rapidwebltd-rw-file-cache)[yuzuru-s/redis-recommend

Wrapping Redis's sorted set APIs for specializing recommending operations.

392.9k](/packages/yuzuru-s-redis-recommend)[ichhabrecht/intcache

Turn uncachable page objects into cacheable links

193.9k](/packages/ichhabrecht-intcache)

PHPackages © 2026

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