PHP code example of yateric / cacheable

1. Go to this page and download the library: Download yateric/cacheable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yateric / cacheable example snippets


use Yateric\Cacheable\Cacheable;

class Worker {
    use Cacheable;
    
    public function timeConsumingTask()
    {
        sleep(10);
        
        return 'Some results';
    }
}

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

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

use Yateric\Cacheable\CacheDecorator;

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

use Yateric\Cacheable\CacheDecorator;

CacheDecorator::setCachePrefix('yourprefix_');