For faster visiting, I cache the paginator in cache with filesystem adapter.
============================================================
my getPaginator function
public function getPaginator(BasicFunc $basicFunc, $page)
{
if (!$page) $page = 1;
$cache = $basicFunc->fsCachePaginator('data/cache/article/design-list','paginator', 600); //this method I pasted bellow.
Paginator::setCache($cache);
$select = new Select('article_designs');
$whereArr = ['valid' => 1];
$select->where($whereArr);
$select->order('id desc');
$selectAdapter = new DbSelect($select, $this->tableGateway->getAdapter());
$paginator = new Paginator($selectAdapter);
$paginator->setCurrentPageNumber($page);
$paginator->setPageRange(5);
$paginator->setItemCountPerPage(12);
return $paginator;
}
====================================================
public function fsCachePaginator($path, $namespace, $ttl)
{
$this->ensurePath($path);
$cache = $this->storageFactory->create(
'filesystem',
['ttl' => $ttl, 'namespace' => $namespace, 'cache_dir' => $path],
[
[
'name' => 'exception_handler',
'options' => [
'throw_exceptions' => false,
],
],
[
'name' => 'Serializer',
],
],
);
return $cache;
}
===============================================
I offered a api to clear the concrete cache by key stuff params.
public function cacheapiAction()
{
$paramsArr = $this->getRequest()->getQuery()->toArray();
$requestArr = array('time', 'key', 'path', 'namespace', 'ttl', 'signature'); //$key,$path,$namespace,$ttl
foreach ($requestArr as $value) {
if (!isset($paramsArr[$value])) exit('0' . $value);
}
if (count($paramsArr) != count($requestArr)) exit('0');
if ($paramsArr['time'] < time() - 3600 || $paramsArr['time'] > time() + 600) exit('0');
if (!$this->basicFunc->signatureProgress($paramsArr, 'privateSecretxxxxxxxx', 'valid')) exit('0');
$this->basicFunc->fsCacheApdater($paramsArr['key'], $paramsArr['path'], $paramsArr['namespace'], $paramsArr['ttl'], 'remove');
exit('1');
}
=====================================================
public function fsCacheApdater($key, $path, $namespace, $ttl, $action = 'get', $value = null, $metaMark = false): mixed
{
$this->ensurePath($path);
$cache = $this->storageFactory->create(
'filesystem',
['ttl' => $ttl, 'namespace' => $namespace, 'cache_dir' => $path],
[
[
'name' => 'exception_handler',
'options' => [
'throw_exceptions' => false,
],
],
[
'name' => 'Serializer',
],
],
);
//$cache->clearExpired(); /* 如果速度慢的话,可以注释掉这一行 */
if ($action == 'get') {
if (!$cache->hasItem($key)) {
return false;
} else {
if (!$metaMark) {
return $cache->getItem($key);
} else {
return [$cache->getItem($key), $cache->getMetadata($key)]; //先返回data,后返回meta
}
}
} elseif ($action == 'set') {
return $cache->setItem($key, $value);
} elseif ($action == 'remove') {
if ($cache->hasItem($key)) $cache->removeItem($key);
}
}
But how to clear the paginator’s cache manually when needed?