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

15

—

LowBetter than 3% of packages

Maintenance37

Infrequent updates — may be unmaintained

Popularity0

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity14

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://www.gravatar.com/avatar/d9ae6dcf5d71a4eda508bc3b84bc78470548ebd7ed1824f3c194e03fd6815bfb?d=identicon)[Behnamfe76](/maintainers/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

[predis/predis

A flexible and feature-complete Redis/Valkey client for PHP.

7.8k305.7M2.4k](/packages/predis-predis)[snc/redis-bundle

A Redis bundle for Symfony

1.0k39.4M67](/packages/snc-redis-bundle)[react/cache

Async, Promise-based cache interface for ReactPHP

444112.4M40](/packages/react-cache)[wp-media/wp-rocket

Performance optimization plugin for WordPress

7431.3M3](/packages/wp-media-wp-rocket)[illuminate/cache

The Illuminate Cache package.

12835.6M1.4k](/packages/illuminate-cache)[colinmollenhour/php-redis-session-abstract

A Redis-based session handler with optimistic locking

6325.6M14](/packages/colinmollenhour-php-redis-session-abstract)

PHPackages © 2026

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