Package Data | |
---|---|
Maintainer Username: | yateric |
Maintainer Contact: | yateric@gmail.com (Eric Chow) |
Package Create Date: | 2017-01-01 |
Package Last Update: | 2017-01-08 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-23 03:24:47 |
Package Statistics | |
---|---|
Total Downloads: | 10 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 1 |
Total Watchers: | 1 |
Total Forks: | 0 |
Total Open Issues: | 0 |
This standalone package makes any object method return cacheable by prepend a chainable method.
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()
{
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();
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();
// Cache result for 120 minutes.
$results = $worker->cache(120)->timeConsumingTask();
$results = Worker::cacheStatic(120)->timeConsumingTaskInStatic();
There are three level of cache duration setting:
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();
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));
You can manually set the cache prefix by calling setCachePrefix()
.
use Yateric\Cacheable\CacheDecorator;
CacheDecorator::setCachePrefix('yourprefix_');
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).