PHP code example of thamtech / yii2-refresh-ahead-cache

1. Go to this page and download the library: Download thamtech/yii2-refresh-ahead-cache 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/ */

    

thamtech / yii2-refresh-ahead-cache example snippets



[
    'components' => [
        'cache' => [
            'class' => 'yii\redis\Cache',
            'as refreshAhead' => 'thamtech\caching\refreshahead\RefreshAheadCacheBehavior',
        ],
    ],
];


[
    'components' => [
        'redisCache' => [
            'class' => 'yii\redis\Cache',
        ],
        'appMutex' => [
            'class' => 'yii\redis\Mutex',
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
            'as refreshAhead' => [
                'class' => 'thamtech\caching\refreshahead\RefreshAheadCacheBehavior',
                'refreshTimeoutCache' => 'redisCache',
                'refreshAheadFactor' => 0.5,
                'refreshTimeoutKeySuffix' => 'refresh-ahead-timeout',
                'mutex' => 'appMutex',
            ],
        ],
    ],
];


$dataManager = Yii::createObject([
    'class' => DataManager::class,
    'as refreshAhead' => [
        'class' => 'thamtech\caching\refreshahead\RefreshAheadCacheBehavior',
        
        // use application 'cache' component for data storage
        'dataCache' => 'cache',
        
        // use application 'cache' component for refresh timeout key storage
        'refreshTimeoutCache' => 'cache',
    ],
]);


$dataManager = Yii::createObject([
    'class' => DataManager::class,
    'as refreshAhead' => [
        'class' => 'thamtech\caching\refreshahead\RefreshAheadCacheBehavior',
        
        // use application 'cache' component for both data storage and refresh
        // timeout key storage
        'cache' => 'cache',
    ],
]);


$data = $cache->getOrSet($key, function ($cache) {
    return $this->calculateSomething();
});

// drop-in replacement:
$data = $cache->getRefreshOrSet($key, function ($cache) {
    return $this->calculateSomething();
});


// with specified $duration and $dependency
$data = $cache->getOrSet($key, function ($cache) {
    return $this->calculateSomething();
}, $duration, $dependency);

// drop-in replacement
$data = $cache->getRefreshOrSet($key, function ($cache) {
    return $this->calculateSomething();
}, $duration, $dependency);


$data = $cache->getRefreshOrSet($key, [
    // called by Refresh Ahead strategy if the data is still cached, but it is
    // time to refresh it
    'refresh' => function ($cache) {
        // queue the refresh task to be run at a later time
        // return `true` when task is queued, `false` if the task was not queued
        // (in which case the `refresh` callable will be called again in a
        // subsequent request)
        return $this->taskQueue->append('calculateSomething');
    },
    
    // called by Refresh Ahead strategy if the data is not in cache (it may
    // have expired before it could be refreshed, or it could have been
    // flushed or evicted, etc.)
    'generate' => function ($cache) {
        return $this->calculateSomething();
    }
], $duration, $dependency);


$generator = [
    'refresh' => function ($cache) {
        return $this->taskQueue->append('calculateSomething');
    },
    'generate' => function ($cache) {
        return $this->calculateSomething();
    },
    // Attempt to acquire a mutex lock for 12 seconds before invoking
    // the 'generate' callback. If the lock is acquired, Refresh Ahead will
    // check to see if the value is in the data cache once more before invoking
    // 'generate', in case another process was generating and caching the value
    // already.
    'mutexLockTimeout' => 12,
];

$data = $cache->getRefreshOrSet($key, $generator, $duration, $dependency);


// using the same parameters defined in the previous example:
$data = $cache->generateAndSet($key, $generator, $duration, $dependency);