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

ActiveLibrary[Caching](/categories/caching)

yateric/cacheable
=================

Makes any object method return cacheable.

v1.0.3(9y ago)210MITPHPPHP &gt;=5.4.0

Since Jan 1Pushed 9y ago1 watchersCompare

[ Source](https://github.com/yateric/cacheable)[ Packagist](https://packagist.org/packages/yateric/cacheable)[ RSS](/packages/yateric-cacheable/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (4)Dependencies (2)Versions (5)Used By (0)

Cacheable
=========

[](#cacheable)

[![Build Status](https://camo.githubusercontent.com/1238a90d8ad7d2bf3f10f1e181389ce66daf3066ac2c2fc157f7a8353f61de8a/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f796174657269632f636163686561626c652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/yateric/cacheable)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Total Downloads](https://camo.githubusercontent.com/953994c9152759821b0119ced65be89a0b09eefcd44022ff716d8a28b97d3714/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f796174657269632f636163686561626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/yateric/cacheable)[![Latest Version](https://camo.githubusercontent.com/8cc3fa0d2b29f82effe6d2058415c99f2dd01bc0ccb66ccdda8af72089ee2778/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f796174657269632f636163686561626c652e7376673f7374796c653d666c61742d737175617265)](https://github.com/yateric/cacheable/releases)

This standalone package makes any object method return cacheable by prepend a chainable method.

Features
--------

[](#features)

- Make any (static or non-static) object method call cacheable
- Specify cache duration
- Standalone package that you can use it without any frameworks
- Support Laravel 5+ out of the box

Installing
----------

[](#installing)

Either [PHP](https://php.net) 5.5+ or [HHVM](http://hhvm.com) 3.6+ are required.

To get the latest version of Cacheable, simply require the project using [Composer](https://getcomposer.org):

```
$ composer require yateric/cacheable
```

Instead, you may of course manually update your require block and run `composer update` if you so choose:

```
{
    "require": {
        "yateric/cacheable": "^1.0"
    }
}
```

Usage
-----

[](#usage)

First, pull in the `Yateric\Cacheable\Cacheable` trait to a class you want to cache the method result, it could be any kind of class: Eloquent model, repository or just an simple object.

```
use Yateric\Cacheable\Cacheable;

class Worker {
    use Cacheable;

    public function timeConsumingTask()
    {
        sleep(10);

        return 'Some results';
    }
}
```

You can now cache and return the `timeConsumingTask()` result by prepend a chainable method `cache()`

```
$worker = new Worker;

// By default, the results will cache for 60 minutes.
$results = $worker->cache()->timeConsumingTask();
```

#### Static method return caching

[](#static-method-return-caching)

```
use Yateric\Cacheable\Cacheable;

class Worker {
    use Cacheable;

    public static function timeConsumingTaskInStatic()
    {
        sleep(10);

        return 'Some results';
    }
}

// By default, the results will cache for 60 minutes.
$results = Worker::cacheStatic()->timeConsumingTaskInStatic();
```

#### Specific cache duration

[](#specific-cache-duration)

```
// Cache result for 120 minutes.
$results = $worker->cache(120)->timeConsumingTask();
$results = Worker::cacheStatic(120)->timeConsumingTaskInStatic();
```

#### Cache duration hierarchy

[](#cache-duration-hierarchy)

There are three level of cache duration setting:

- Runtime level
- Instance level
- Global level

Here are some example of the hierarchy:

```
use Yateric\Cacheable\CacheDecorator;

// Cache for 60 minutes by default.
$workerA->cache()->timeConsumingTask();

// Cache for 120 minutes by runtime setting.
$workerA->cache(120)->timeConsumingTask();

// Return to default 60 minutes.
$workerA->cache()->timeConsumingTask();

// Set default cache duration to 180 minutes.
$workerA->cache()->setDefaultCacheMinutes(180);

// These calls will cache for 180 minutes.
$workerA->cache()->timeConsumingTaskA();
$workerA->cache()->timeConsumingTaskB();
$workerA->cache()->timeConsumingTaskC();

// Set the global cache duration to 240 minutes.
CacheDecorator::setGlobalCacheMinutes(240);

// Worker A will remain cache for 180 minutes because
// we have set the default cache duration in instance level.
$workerA->cache()->timeConsumingTask();

// These calls will cache for 240 minutes.
$workerB->cache()->timeConsumingTask();
$workerC->cache()->timeConsumingTask();
```

#### Swap the underlying cache store

[](#swap-the-underlying-cache-store)

If you are using Laravel 5+, cacheable will use the default cache store `config('cache.default')` automatically. But you are free to specify any cache store which implement the `Illuminate\Contracts\Cache\Store` contract by calling `setCacheStore()`.

```
use Yateric\Cacheable\CacheDecorator;

CacheDecorator::setCacheStore(new RedisStore($redis));
```

#### Cache prefix

[](#cache-prefix)

You can manually set the cache prefix by calling `setCachePrefix()`.

```
use Yateric\Cacheable\CacheDecorator;

CacheDecorator::setCachePrefix('yourprefix_');
```

Security
--------

[](#security)

If you discover a security vulnerability within this package, please send an e-mail to Eric Chow at . All security vulnerabilities will be promptly addressed.

License
-------

[](#license)

Cacheable is licensed under [The MIT License (MIT)](LICENSE).

###  Health Score

27

—

LowBetter than 47% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity61

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

4

Last Release

3466d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1567297?v=4)[Eric Chow](/maintainers/yateric)[@yateric](https://github.com/yateric)

---

Top Contributors

[![yateric](https://avatars.githubusercontent.com/u/1567297?v=4)](https://github.com/yateric "yateric (9 commits)")

---

Tags

laravelobjectcacheable

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[spatie/laravel-responsecache

Speed up a Laravel application by caching the entire response

2.8k8.7M64](/packages/spatie-laravel-responsecache)[harris21/laravel-fuse

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

43140.3k](/packages/harris21-laravel-fuse)[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)[omaralalwi/lexi-translate

Laravel translation package with morph relationships and caching.

738.5k3](/packages/omaralalwi-lexi-translate)[karriere/state

Laravel package for storing current application state in cache/session

1719.0k](/packages/karriere-state)

PHPackages © 2026

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