Paginator::setCache cache paginator with filesystem adapter, how to clear it manually?

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?

The caching feature of laminas-paginator is described in the documentation and there you will also find some examples on the current topic:

use Laminas\Paginator\Paginator;

// $cache is a Laminas\Cache\Storage\StorageInterface instance
Paginator::setCache($cache);

// …

// clear the cache of the results for page 3
$paginator->clearPageItemCache(3);

// clear all the cache data
$paginator->clearPageItemCache();

Please note that you create two independent caches. One in the method fsCachePaginator and another one in fsCacheApdater.

Thank you so much for replying me!

Sorry that I didn’t read the documation clearly.

Yes, you are right! I create two independent caches, Because I don’t know how to merge them to one.

Now, they are two independent caches, It runs at least.

Later I will try to merge when I have time.

Now I need to clear the paginator’s cache separately.

Please see the laminas-cache documentation for details on how to integrate for a laminas-mvc based application: