Moved caching methods out of functions.inc.php into ContentCache class.
This commit is contained in:
76
lib/Serendipity/ContentCache.php
Normal file
76
lib/Serendipity/ContentCache.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
// Serendipity
|
||||
// See LICENSE file for license information.
|
||||
|
||||
namespace Serendipity;
|
||||
|
||||
use voku\cache\Cache;
|
||||
use voku\cache\CacheAdapterAutoManager;
|
||||
use voku\cache\AdapterArray;
|
||||
use voku\cache\AdapterOpCache;
|
||||
|
||||
class ContentCache
|
||||
{
|
||||
protected static $instance = null;
|
||||
protected $cache_manager;
|
||||
protected $cache;
|
||||
|
||||
public static function getInstance()
|
||||
{
|
||||
if (self::$instance === null) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
// Configure voku/simple-cache to use templates_c as directory for the opcache files, the fallback
|
||||
// when Memcached and Redis are not used.
|
||||
// FIXME: Bad hack - remove when no longer needed!
|
||||
global $serendipity;
|
||||
$cacheDir = $serendipity['serendipityPath'] . '/templates_c/simple_cache';
|
||||
|
||||
$this->cache_manager = new CacheAdapterAutoManager();
|
||||
$this->cache_manager->addAdapter(
|
||||
AdapterOpCache::class,
|
||||
static function () use ($cacheDir) {
|
||||
return $cacheDir;
|
||||
}
|
||||
);
|
||||
|
||||
$this->cache_manager->addAdapter(
|
||||
AdapterArray::class
|
||||
);
|
||||
|
||||
$this->cache = new Cache(
|
||||
null,
|
||||
null,
|
||||
false,
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
'',
|
||||
$this->cache_manager,
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
public function addItem($key, $item, $ttl = 3600)
|
||||
{
|
||||
return $this->cache->setItem($key, $item, $ttl);
|
||||
}
|
||||
|
||||
public function getItem($key)
|
||||
{
|
||||
return $this->cache->getItem($key);
|
||||
}
|
||||
|
||||
public function clearCache()
|
||||
{
|
||||
return $this->cache->removeAll();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user