-
-
Save Lazerproof/221d8cb401e637e662d8dfba0d0a648a to your computer and use it in GitHub Desktop.
OctoberCMS - CachableComponent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Cache; | |
use Cms\Classes\ComponentBase; | |
abstract class CachableComponentBase extends ComponentBase | |
{ | |
/** | |
* Build cache configuration which is an array: ['cachedKey' => 'The cache key value', 'cachedTimeInMinutes' => 'Cached time in minutes'] | |
* @return mixed | |
*/ | |
public abstract function cacheConfig(); | |
protected abstract function prepareData(); | |
public function onRender() | |
{ | |
$cachedConfig = $this->cacheConfig(); | |
$key = $cachedConfig['key']; | |
$minutes = $cachedConfig['cachedTimeInMinutes']; | |
$result = Cache::remember($key, $minutes, function() { | |
$this->prepareData(); | |
$output = $this->renderPartial('@default'); | |
return $output; | |
}); | |
return $result; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use \ExampleComponent; | |
class ExampleComponent extends CachableComponentBase | |
{ | |
public function cacheConfig() | |
{ | |
return ['key' => '_example_component_key', | |
'cachedTimeInMinutes' => 10]; | |
} | |
protected function prepareData() { | |
//prepare your data before rendering the component | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment