Hello Big Guns!
I use filesystem cache to store my patinator results. see the following codes.
public function getPaginator($class = 1,$page = 1,ApiModel $apiModel)
{
if (!$page) $page = 1;
$cache = $apiModel->fsCacheApdater('article-list-'.$class.'-'.$page,'data/cache/article','7200',7200);
if ($cache)
{
//when visited the second time, I exit() here, it works, means the cache is ok.
$paginator = unserialize($cache);
}
else
{
$contentExpression = new Expression('left(content,60)');
$select = new Select('article');
$select->columns(['id','class','title','cover','content' => $contentExpression,'addtime']);
$whereArr = ['valid' => 1];
if ($class) $whereArr['class'] = $class;
$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->setCacheEnabled(true);
$apiModel->fsCacheApdater('article-list-'.$class.'-'.$page,'data/cache/article','7200',7200,'set',serialize($paginator));
}
return $paginator;
}
The following code is the fsCacheAdapter in apiModel class:
public function fsCacheApdater($key,$path,$namespace,$ttl,$action = 'get',$value=null)
{
$this->ensurePath($path);
$cache = StorageFactory::factory([
'adapter' => [
'name' => 'filesystem',
'options' => [
'ttl' => $ttl,
'namespace' => $namespace,
'cache_dir' => $path,
],
],
'plugins' => [
'exception_handler' => ['throw_exceptions' => false],
],
]);
//$cache->clearExpired();
if ($action == 'get')
{
if (!$cache->hasItem($key))
{
return false;
}
else
{
return $cache->getItem($key);
}
}
elseif ($action == 'set')
{
return $cache->setItem($key, $value);
}
elseif ($action == 'remove')
{
if ($cache->hasItem($key)) $cache->removeItem($key);
}
}
I think my exit(‘0000’) test the pagination results are cached in a local .dat file.
What I don’t understand is that why it is so slow when it was cached and no need to connect the database to acquire the data?
I checked it out in my mysql slow-log