2017 © Pedro Peláez
 

library cacheable

Makes any object method return cacheable.

image

yateric/cacheable

Makes any object method return cacheable.

  • Sunday, January 8, 2017
  • by yateric
  • Repository
  • 1 Watchers
  • 1 Stars
  • 4 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Cacheable

Build Status Software License Total Downloads Latest Version, (*1)

This standalone package makes any object method return cacheable by prepend a chainable method., (*2)

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

Either PHP 5.5+ or HHVM 3.6+ are required., (*3)

To get the latest version of Cacheable, simply require the project using Composer:, (*4)

$ composer require yateric/cacheable

Instead, you may of course manually update your require block and run composer update if you so choose:, (*5)

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

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., (*6)

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

$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()
    {
        sleep(10);

        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, (*8)

Here are some example of the hierarchy:, (*9)

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

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()., (*10)

use Yateric\Cacheable\CacheDecorator;

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

Cache prefix

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

use Yateric\Cacheable\CacheDecorator;

CacheDecorator::setCachePrefix('yourprefix_');

Security

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., (*12)

License

Cacheable is licensed under The MIT License (MIT)., (*13)

The Versions

08/01 2017

dev-master

9999999-dev

Makes any object method return cacheable.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eric Chow

laravel object cacheable

03/01 2017

v1.0.3

1.0.3.0

Makes any object method return cacheable.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eric Chow

laravel object cacheable

03/01 2017

v1.0.2

1.0.2.0

Makes any object method return cacheable.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eric Chow

laravel object cacheable

02/01 2017

v1.0.1

1.0.1.0

Makes any object method return cacheable.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eric Chow

laravel object cacheable

01/01 2017

v1.0.0

1.0.0.0

Makes any object method return cacheable.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eric Chow

laravel object cacheable