# 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:** 6\
**Page:** 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?

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [March 31, 2023, 9:52am UTC](https://discourse.laminas.dev/t/paginator-setcache-cache-paginator-with-filesystem-adapter-how-to-clear-it-manually/3268/2 "2023-03-31T09:52:35Z")

</div>

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

```php
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();

```

> **[Advanced Usage - laminas-paginator - Laminas Docs](https://docs.laminas.dev/laminas-paginator/advanced/#caching-features)**
>
> Paginate collections of data from arbitrary sources

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [March 31, 2023, 9:55am UTC](https://discourse.laminas.dev/t/paginator-setcache-cache-paginator-with-filesystem-adapter-how-to-clear-it-manually/3268/3 "2023-03-31T09:55:09Z")

</div>

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

---

<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:** [April 3, 2023, 2:54am UTC](https://discourse.laminas.dev/t/paginator-setcache-cache-paginator-with-filesystem-adapter-how-to-clear-it-manually/3268/4 "2023-04-03T02:54:44Z")

</div>

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.

---

<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:** [April 3, 2023, 3:14am UTC](https://discourse.laminas.dev/t/paginator-setcache-cache-paginator-with-filesystem-adapter-how-to-clear-it-manually/3268/5 "2023-04-03T03:14:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [April 3, 2023, 8:24am UTC](https://discourse.laminas.dev/t/paginator-setcache-cache-paginator-with-filesystem-adapter-how-to-clear-it-manually/3268/6 "2023-04-03T08:24:16Z")

</div>

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

> **[Usage in a laminas-mvc Application - laminas-cache - Laminas Docs](https://docs.laminas.dev/laminas-cache/v3/application-integration/usage-in-a-laminas-mvc-application/)**
>
> Caching implementation with a variety of storage options, as well as codified caching strategies for callbacks, classes, and output.
