Makes any object method return cacheable.

v1.0.3 2017-01-03 04:58 UTC

This package is not auto-updated.

Last update: 2024-07-20 18:36:48 UTC


Build Status Software License Total Downloads Latest Version

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


  • 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


Either PHP 5.5+ or HHVM 3.6+ are required.

To get the latest version of Cacheable, simply require the project using Composer:

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


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

use Yateric\Cacheable\Cacheable;

class Worker {
    use Cacheable;
    public static function timeConsumingTaskInStatic()
        return 'Some results';

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

Specific cache duration

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

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.

// Cache for 120 minutes by runtime setting.

// Return to default 60 minutes.

// Set default cache duration to 180 minutes.

// These calls will cache for 180 minutes.

// Set the global cache duration to 240 minutes.

// Worker A will remain cache for 180 minutes because 
// we have set the default cache duration in instance level.

// These calls will cache for 240 minutes.

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

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

use Yateric\Cacheable\CacheDecorator;



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


Cacheable is licensed under The MIT License (MIT).