silverstripe-versionfeed/src/Filters/CachedContentFilter.php
Dylan Wagstaff 67e112fd12 FIX: Minor functional alterations and CI improvements
FIX: PSR-2 codebase. Formatting via phpcbf
FIX: rendering bug in allchanges
FIX: update .gitattributes to not export codecov's config file
FIX: Update SiteTree_versions to the ss4 equivalent SiteTree_Versions
2017-12-12 16:12:03 +13:00

41 lines
974 B
PHP

<?php
namespace SilverStripe\VersionFeed\Filters;
use SilverStripe\Core\Config\Config;
/**
* Caches results of a callback
*/
class CachedContentFilter extends ContentFilter
{
/**
* Enable caching
*
* @config
* @var boolean
*/
private static $cache_enabled = true;
public function getContent($key, $callback)
{
$cache = $this->getCache();
// Return cached value if available
$cacheEnabled = Config::inst()->get(get_class(), 'cache_enabled');
$result = (isset($_GET['flush']) || !$cacheEnabled)
? null
: $cache->get($key);
if ($result) {
return $result;
}
// Fallback to generate result
$result = parent::getContent($key, $callback);
$lifetime = Config::inst()->get(ContentFilter::class, 'cache_lifetime') ?: null;
$cache->set($key, $result, $lifetime);
return $result;
}
}