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

**URL:** <https://discourse.laminas.dev/t/paginator-setcache-cache-paginator-with-filesystem-adapter-how-to-clear-it-manually/3268>\
**Category:** Components & MVC\
**Tags:** laminas-paginator, laminas-cache\
**Created:** [March 31, 2023, 8:43am UTC](https://discourse.laminas.dev/t/paginator-setcache-cache-paginator-with-filesystem-adapter-how-to-clear-it-manually/3268 "2023-03-31T08:43:08Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![jobsfan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/jobsfan/32/399_2.png) [@jobsfan](https://discourse.laminas.dev/u/jobsfan)\
**Post date:** [March 31, 2023, 8:43am UTC](https://discourse.laminas.dev/t/paginator-setcache-cache-paginator-with-filesystem-adapter-how-to-clear-it-manually/3268/1 "2023-03-31T08:43:08Z")

</div>

For faster visiting, I cache the paginator in cache with filesystem adapter.

============================================================  
my getPaginator function

```auto
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;
}

```

====================================================

```auto
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.

```auto
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');
}

```

=====================================================

```auto
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?

---

_[View the full topic](https://discourse.laminas.dev/t/paginator-setcache-cache-paginator-with-filesystem-adapter-how-to-clear-it-manually/3268)._
