PHPackages                             chrgriffin/laravel-cacheable - 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. chrgriffin/laravel-cacheable

ActiveLibrary[Caching](/categories/caching)

chrgriffin/laravel-cacheable
============================

A GOAOP implementation for easily caching method calls.

v1.0.2(6y ago)152[1 PRs](https://github.com/ChrGriffin/laravel-cacheable/pulls)MITPHPCI failing

Since Mar 16Pushed 5y ago1 watchersCompare

[ Source](https://github.com/ChrGriffin/laravel-cacheable)[ Packagist](https://packagist.org/packages/chrgriffin/laravel-cacheable)[ RSS](/packages/chrgriffin-laravel-cacheable/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (3)Versions (6)Used By (0)

[![Build Status](https://camo.githubusercontent.com/a08796c98b1a99c7829bb7cf60b3c21ed63bc908c7bff45bd973bcc868ab4e32/68747470733a2f2f6170702e636f6465736869702e636f6d2f70726f6a656374732f64363933323738302d343933632d303133382d633932342d3561306239653464343131302f7374617475733f6272616e63683d6d6173746572)](https://camo.githubusercontent.com/a08796c98b1a99c7829bb7cf60b3c21ed63bc908c7bff45bd973bcc868ab4e32/68747470733a2f2f6170702e636f6465736869702e636f6d2f70726f6a656374732f64363933323738302d343933632d303133382d633932342d3561306239653464343131302f7374617475733f6272616e63683d6d6173746572)[![Coverage Status](https://camo.githubusercontent.com/cef79255d42f1d9afb479c371df1482e9d9bdcf4f2f454057c516d68cee4626d/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f4368724772696666696e2f6c61726176656c2d636163686561626c652f62616467652e7376673f6272616e63683d6d6173746572)](https://camo.githubusercontent.com/cef79255d42f1d9afb479c371df1482e9d9bdcf4f2f454057c516d68cee4626d/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f4368724772696666696e2f6c61726176656c2d636163686561626c652f62616467652e7376673f6272616e63683d6d6173746572)

Laravel Cacheable
=================

[](#laravel-cacheable)

`laravel-cacheable` makes it easy to automagically cache the results of any arbitrary class method, using your configured Laravel cache driver, by simply adding a docblock annotation to that method.

```
use LaravelCacheable\Annotations\Cache;

class MyClass
{
    /** @Cache(seconds=3600) */
    public function cacheMe(): string
    {
        return 'Hello!';
    }
}
```

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

[](#installation)

Install in your Laravel project via composer:

```
composer install chrgriffin/laravel-cacheable
```

If your version of Laravel supports auto-discovery (5.5 and up), you're done!

For older versions of Laravel, you'll need to edit your `config/app.php` file to include the service provider in your providers array:

```
return [
    // ...
    'providers' => [
        // ...
        LaravelCacheable\ServiceProvider::class
    ]
];
```

Usage
-----

[](#usage)

### Configuration (optional)

[](#configuration-optional)

`laravel-cacheable` uses [GOAOP](https://github.com/goaop/framework) to build cached versions of your classes which then have a method interceptor applied to it. The package will, by default, use your Laravel framework cache directory as its cache. It will also, by default, search for Cache annotations in your entire `app` directory. Both of these configs can be overridden. First, copy the package config:

```
php artisan vendor:publish --provider="LaravelCacheable\ServiceProvider"
```

Now, in `config/laravel-cacheable.php`, you can override the default package configs:

```
return [
    'cache' => [
        // this is where laravel-cacheable will store cached versions of your classes
        storage_path('framework/cache/laravel-cacheable')
    ],
    // this is an array of all the paths laravel-cacheable will 'scan' for Cache annotations
    'paths' => [
        app_path()
    ]
];
```

### Caching Method Returns

[](#caching-method-returns)

Now that the package is installed, caching the results of a method call is as easy as [making sure your Laravel cache is set up](https://laravel.com/docs/master/cache) and then adding an annotation to your method:

```
use LaravelCacheable\Annotations\Cache;

class MyClass
{
    /** @Cache */
    public function cacheMe(): string
    {
        return 'Hello!';
    }
}
```

#### Specifying a Custom Cache Time

[](#specifying-a-custom-cache-time)

By default, `laravel-cacheable` will cache a method return for 30 minutes. If you want to cache a method for a different length of time, you can add a `seconds` property to the annotation:

```
use LaravelCacheable\Annotations\Cache;

class MyClass
{
    /** @Cache(seconds=3600) */
    public function cacheMe(): string
    {
        return 'Hello!';
    }
}
```

### Advanced Behaviours

[](#advanced-behaviours)

The docblock alone is enough to automatically cache the results of a method. For more advanced behaviours, you will also need to use the `Cacheable` trait:

```
use LaravelCacheable\Annotations\Cache;
use LaravelCacheable\Traits\Cacheable;

class MyClass
{
    use Cacheable;

    /** @Cache(seconds=3600) */
    public function cacheMe(): string
    {
        return 'Hello!';
    }
}
```

#### Bypassing the Cache

[](#bypassing-the-cache)

Once your class is using the `Cacheable` trait, you can bypass the trait by chaining `->withoutCache()` before your method call:

```
// bypasses cache
(new MyClass())->withoutCache()->cacheMe();
```

Note that this will bypass both getting *and* setting the cache -- the return of this method will not be cached.

### Notes

[](#notes)

`laravel-cacheable` checks both the method name and the passed arguments to determine if the return should be pulled from cache or not. If different arguments are passed, a new cache index will be created.

```
(new MyClass())->cacheMe('argument 1');

// this method call will not use the cached return from the first one
(new MyClass())->cacheMe('argument 2');
```

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity60

Established project with proven stability

 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.

###  Release Activity

Cadence

Every ~0 days

Total

3

Last Release

2246d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/90b61f51551a66c90c131f873fb94ee87a064cb72dc178d8512e90e13dc769e6?d=identicon)[ChrGriffin](/maintainers/ChrGriffin)

---

Top Contributors

[![ChrGriffin](https://avatars.githubusercontent.com/u/10466686?v=4)](https://github.com/ChrGriffin "ChrGriffin (25 commits)")

### Embed Badge

![Health badge](/badges/chrgriffin-laravel-cacheable/health.svg)

```
[![Health](https://phpackages.com/badges/chrgriffin-laravel-cacheable/health.svg)](https://phpackages.com/packages/chrgriffin-laravel-cacheable)
```

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