# I am new to Laminas, I need to know how Cache works

**URL:** <https://discourse.laminas.dev/t/i-am-new-to-laminas-i-need-to-know-how-cache-works/2925>\
**Category:** Components & MVC\
**Tags:** laminas-mvc, laminas-cache\
**Created:** [June 16, 2022, 1:36pm UTC](https://discourse.laminas.dev/t/i-am-new-to-laminas-i-need-to-know-how-cache-works/2925 "2022-06-16T13:36:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![irfan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/irfan/32/1320_2.png) [@irfan](https://discourse.laminas.dev/u/irfan)\
**Post date:** [June 16, 2022, 1:36pm UTC](https://discourse.laminas.dev/t/i-am-new-to-laminas-i-need-to-know-how-cache-works/2925/1 "2022-06-16T13:36:38Z")

</div>

If anybody provides a complete working example, I’ll appreciate.

---

<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:** [June 20, 2022, 12:05pm UTC](https://discourse.laminas.dev/t/i-am-new-to-laminas-i-need-to-know-how-cache-works/2925/2 "2022-06-20T12:05:43Z")

</div>

Hello and welcome to our forums! 😃

> [@irfan](#):
>
> If anybody provides a complete working example…

Before starting, make sure a cache adapter is installed, like the APCu or the Filesystem adapter.

Here is an example which uses two different cache adapters as storage for the cache:

```php
require_once __DIR__. '/vendor/autoload.php';

$config = (new Laminas\ConfigAggregator\ConfigAggregator([
    Laminas\Cache\ConfigProvider::class,
    Laminas\Cache\Storage\Adapter\BlackHole\ConfigProvider::class,
    Laminas\Cache\Storage\Adapter\Memory\ConfigProvider::class,
]))->getMergedConfig();

$container = new Laminas\ServiceManager\ServiceManager($config['dependencies']);

/** @var Laminas\Cache\Service\StorageAdapterFactoryInterface $storageFactory */
$storageFactory = $container->get(
    Laminas\Cache\Service\StorageAdapterFactoryInterface::class
);

$storage1 = $storageFactory->create(Laminas\Cache\Storage\Adapter\BlackHole::class);
$storage2 = $storageFactory->create(Laminas\Cache\Storage\Adapter\Memory::class);

$storage1->setItem('key', 'value');
if ($storage1->hasItem('key')) {
    echo $storage1->getItem('key'); // "value"
}

$storage2->setItem('key', 'value');
if ($storage2->hasItem('key')) {
    echo $storage2->getItem('key'); // "value"
}

```

Check also the quick start and the example section:

- [Adapters - laminas-cache - Laminas Docs](https://docs.laminas.dev/laminas-cache/v3/storage/adapter/#quick-start)
- [Adapters - laminas-cache - Laminas Docs](https://docs.laminas.dev/laminas-cache/v3/storage/adapter/#examples)

---

<div class="post-metadata">

**Author:** ![irfan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/irfan/32/1320_2.png) [@irfan](https://discourse.laminas.dev/u/irfan)\
**Post date:** [July 23, 2022, 5:01pm UTC](https://discourse.laminas.dev/t/i-am-new-to-laminas-i-need-to-know-how-cache-works/2925/3 "2022-07-23T17:01:03Z")

</div>

Thank you froschdesign, Actually I am very beginner and could not get how to implement the code you provided.

1. I am using php 8 and APCu is for older php versions. I have to cache DB results for like 10 minutes. what is the best adapter you suggest?

2. I have installed Filesystem and here is require section of my composer.json.

“laminas/laminas-cache”: “\*”,  
“laminas/laminas-cache-storage-adapter-filesystem”: “^2.0”,  
“laminas/laminas-config-aggregator”: “^1.7”

1. here is the error I am getting

Class Application\Model\Laminas\ConfigAggregator\ConfigAggregator not found

Thank you,  
Irfan

---

<div class="post-metadata">

**Author:** ![ALTAMASH80](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/altamash80/32/895_2.png) [@ALTAMASH80](https://discourse.laminas.dev/u/ALTAMASH80)\
**Post date:** [August 2, 2022, 7:48am UTC](https://discourse.laminas.dev/t/i-am-new-to-laminas-i-need-to-know-how-cache-works/2925/4 "2022-08-02T07:48:15Z")

</div>

Dear @irfan,

Your problem is that you’ve not used your class correctly. So, some basic usage explanation here. When using a class of any third-party library you can use it in two ways. Here is an example.

```auto
<?php

namespace Application\Model;

use Laminas\ConfigAggregator\ConfigAggregator;

class MyModel {
    public function __constructor(){
       $configObject = new ConfigAggregator();
    }
}
/** Second way to use it is to use the complete namespace with a backslash and that is your problem. */

namespace Application\Model;
 
class MyModel {
    public function __constructor(){
       $configObject = new \Laminas\ConfigAggregator\ConfigAggregator;
    }
}

```

I hope it helps. Also, the working example given is for core PHP(without any framework) and you’re using the MVC application of Laminas. Therefore, things change in the MVC version compared to using your own constructed framework. The above example will most likely work in WordPress. Thanks!

---

<div class="post-metadata">

**Author:** ![irfan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/irfan/32/1320_2.png) [@irfan](https://discourse.laminas.dev/u/irfan)\
**Post date:** [August 2, 2022, 7:54am UTC](https://discourse.laminas.dev/t/i-am-new-to-laminas-i-need-to-know-how-cache-works/2925/5 "2022-08-02T07:54:57Z")

</div>

Thank you ALTAMASH, I will try this.

---

<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:** [August 2, 2022, 8:43am UTC](https://discourse.laminas.dev/t/i-am-new-to-laminas-i-need-to-know-how-cache-works/2925/6 "2022-08-02T08:43:03Z")

</div>

My code example is independent of an application and therefore the `ConfigAggregator` is used. In a laminas-mvc or Mezzio application this is not needed!  
Do use laminas-cache as stand-alone solution or do you have an application based on laminas-mvc or Mezzio?

Check also the documentation of PHP for [the basics of namespaces](https://www.php.net/manual/language.namespaces.basics.php).

---

<div class="post-metadata">

**Author:** ![irfan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/irfan/32/1320_2.png) [@irfan](https://discourse.laminas.dev/u/irfan)\
**Post date:** [August 2, 2022, 9:01am UTC](https://discourse.laminas.dev/t/i-am-new-to-laminas-i-need-to-know-how-cache-works/2925/7 "2022-08-02T09:01:30Z")

</div>

Yes my Application is based on laminas-mvc and I want o implement laminas cache in it. thanks

---

<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:** [August 2, 2022, 10:43am UTC](https://discourse.laminas.dev/t/i-am-new-to-laminas-i-need-to-know-how-cache-works/2925/8 "2022-08-02T10:43:38Z")

</div>

> [@irfan](#):
>
> Yes my Application is based on laminas-mvc…

Follow these steps for a laminas-mvc based application:

1. Install laminas-cache and a storage adapter, like `laminas-cache-storage-adapter-filesystem`  
(if you use the laminas-mvc-skeleton application then both components are automatically registered in the application in `config/modules.config.php`)

2. Extend the configuration of your application. You can use the global application configuration, e.g. the `config/autoload/global.php` file and choose a name for the cache:

3. Now you can fetch the cache by the name `default-cache` from the application service container.

This all works because both components, laminas-cache and laminas-cache-storage-adapter-filesystem, provide and register factories that use the configuration and look for the configuration keys `caches` and `cache`.

- use `caches` if you need multiple storage adapters, recommended – also for one adapter
- use `cache` if you only need one explicit storage adapter
