# How the redis work in laminas-mvc not as cache storage?

**URL:** <https://discourse.laminas.dev/t/how-the-redis-work-in-laminas-mvc-not-as-cache-storage/2354>\
**Category:** Components & MVC\
**Tags:** laminas-cache\
**Created:** [June 15, 2021, 2:28am UTC](https://discourse.laminas.dev/t/how-the-redis-work-in-laminas-mvc-not-as-cache-storage/2354 "2021-06-15T02:28:44Z")\
**Posts on this page:** 12\
**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:** [June 15, 2021, 2:28am UTC](https://discourse.laminas.dev/t/how-the-redis-work-in-laminas-mvc-not-as-cache-storage/2354/1 "2021-06-15T02:28:45Z")

</div>

I worked out a filesystem cache adapter as

 ![11111](https://canada1.discourse-cdn.com/flex032/uploads/zendframework/original/2X/c/ca930b169bb9e3a106f8ddfe2d440080bd9d56e4.png)

then I changed it into a redis one like

 ![22222](https://canada1.discourse-cdn.com/flex032/uploads/zendframework/original/2X/5/5ca1facd3a3a2076c32313f420eacab4ad66e6f4.png)

These part is too complicate [Adapters - laminas-cache - Laminas Docs](https://docs.laminas.dev/laminas-cache/storage/adapter/#redis-adapter)

 ![3333](https://canada1.discourse-cdn.com/flex032/uploads/zendframework/original/2X/b/b6cabf20536f5639587731907a7bb6906003df0b.png)

Generally, I would like to store a redis connection instance inside the container before. This way will keep just one instance in the whole lifecycle. But was told that not to change container and keep it stateless.

What should I do with a normal redis operation, not for a cache purpose?

---

<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 15, 2021, 6:13am UTC](https://discourse.laminas.dev/t/how-the-redis-work-in-laminas-mvc-not-as-cache-storage/2354/2 "2021-06-15T06:13:18Z")

</div>

Your entire `BaseFuncModel` class is only a workaround. It looks like a class that creates different objects of your application, so it combines different factories in one class.

I think the following could work: create a separate factory for the cache and then you can create a factory for Redis itself that fetches Redis from the cache.

* * *

But I don’t know why you want to access Redis directly because you make the whole principle of the laminas-cache component and the interchangeable adapters useless!

---

<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:** [June 15, 2021, 6:39am UTC](https://discourse.laminas.dev/t/how-the-redis-work-in-laminas-mvc-not-as-cache-storage/2354/3 "2021-06-15T06:39:26Z")

</div>

> [@froschdesign](#):
>
> But I don’t know why you want to access Redis directly because you make the whole principle of the laminas-cache component and the interchangeable adapters useless!

Becuase I want to use Redis not as a cache purpose, in my opinion, lpush/rpop, such kind of list operation doesn’t sound like cache.

---

<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 15, 2021, 6:51am UTC](https://discourse.laminas.dev/t/how-the-redis-work-in-laminas-mvc-not-as-cache-storage/2354/4 "2021-06-15T06:51:04Z")

</div>

Then try my suggestion:

> [@froschdesign](#):
>
> I think the following could work: create a separate factory for the cache and then you can create a factory for Redis itself that fetches Redis from the cache.

---

<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:** [June 15, 2021, 6:54am UTC](https://discourse.laminas.dev/t/how-the-redis-work-in-laminas-mvc-not-as-cache-storage/2354/5 "2021-06-15T06:54:58Z")

</div>

Okay, I will try. Should I register a service-kind class? with a factory to do the DI job?  
And then register it via the moduel.config.php’s **service\_manager** key?

Or register it in Module.php’s **getServiceConfig()** method?

Is there any difference between **moduel.config.php’s service\_manager key** and **Module.php’s getServiceConfig() method**?

---

<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 15, 2021, 7:09am UTC](https://discourse.laminas.dev/t/how-the-redis-work-in-laminas-mvc-not-as-cache-storage/2354/6 "2021-06-15T07:09:32Z")

</div>

### Example

`module/Application/config/module.config.php`:

```php
return [
    'service_manager' => [
        'factories' => [
            Redis::class => Application\MyRedisFactory::class,
        ],
    ],
    // …
];

```

> [@jobsfan](#):
>
> Is there any difference between **moduel.config.php’s service\_manager key** and **Module.php’s getServiceConfig() method**?

Use only one option and I recommend the configuration file.

---

<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:** [June 15, 2021, 7:22am UTC](https://discourse.laminas.dev/t/how-the-redis-work-in-laminas-mvc-not-as-cache-storage/2354/7 "2021-06-15T07:22:48Z")

</div>

Ok, thank you! Your suggestion is obviously the best solution!

---

<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:** [June 15, 2021, 7:56am UTC](https://discourse.laminas.dev/t/how-the-redis-work-in-laminas-mvc-not-as-cache-storage/2354/8 "2021-06-15T07:56:41Z")

</div>

I tried to follow your recommendation,

 ![1111](https://canada1.discourse-cdn.com/flex032/uploads/zendframework/original/2X/c/c27a791a0ddec6c8261cd31ee51433e7b23f45e6.png)

I typed **Redis** in the phpstorm IDE, it suggested as follow:

 ![22222](https://canada1.discourse-cdn.com/flex032/uploads/zendframework/original/2X/1/194dfbc1b3ddd148d5ad6d04e83272c3388a84b7.png)  
If I choose the first one, then it import **use Laminas\Cache\Storage\Adapter\Redis;**  
If I choose the second one, then **\Redis::class**  
 ![666666](https://canada1.discourse-cdn.com/flex032/uploads/zendframework/original/2X/5/55d342d360f654838811ddc9551d1b9a43024488.png)

Which one should I choose? If I choose the first one, will it affect my function _**redisCacheApdater()**_

 ![4444](https://canada1.discourse-cdn.com/flex032/uploads/zendframework/original/2X/9/982becd84ed2954fb2b6edf2c9d291616a592793.png)

Besides, what should I do when I need to inject the **Redis::class** into my controller’s \_\_construct method?

 ![55555](https://canada1.discourse-cdn.com/flex032/uploads/zendframework/original/2X/4/4f479791490882360a146e33194d2055d51a4108.png)

I didn’t create a factory for each controller. I use abstract\_factories **LazyControllerAbstractFactory::class**

---

<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 15, 2021, 8:26am UTC](https://discourse.laminas.dev/t/how-the-redis-work-in-laminas-mvc-not-as-cache-storage/2354/9 "2021-06-15T08:26:07Z")

</div>

> [@jobsfan](#):
>
> Which one should I choose?

You don’t want the cache adapter, so do not use his name. The `Redis::class` comes with redis extension of PHP, use this. But create a custom factory for it.

> [@jobsfan](#):
>
> Besides, what should I do when I need to inject the **Redis::class** into my controller’s \_\_construct method?

If you register `Redis::class` in the service manager or something else like `Foobar::class` or `Something::class` then the `LazyControllerAbstractFactory::class` of laminas-mvc or the `ReflectionBasedAbstractFactory` of laminas-servicemanager will find it under these names.

There is no magic, only some simple matching! 😃

---

<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:** [June 15, 2021, 9:32am UTC](https://discourse.laminas.dev/t/how-the-redis-work-in-laminas-mvc-not-as-cache-storage/2354/10 "2021-06-15T09:32:41Z")

</div>

I got the error

```bash
Unable to create controller "Application\Controller\ForumController"; unable to resolve parameter "redis" using type hint "Application\Controller\Redis"

```

my module.config.php

 ![11111](https://canada1.discourse-cdn.com/flex032/uploads/zendframework/original/2X/c/c765cd757581e49d2a1960cdf3a746c7bbc0d65a.png)

I wrote a CommonRedisFactory like

 ![222222](https://canada1.discourse-cdn.com/flex032/uploads/zendframework/original/2X/6/6d96e25d4397ab4525f76974fb4ae422c87a0a58.png)

```bash
<?php
/**
 * single instance of redis connnection
 */

namespace Application\Service\Factory;

use Interop\Container\ContainerInterface;
use Interop\Container\Exception\ContainerException;
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
use Laminas\ServiceManager\Factory\FactoryInterface;

class CommonRedisFactory implements FactoryInterface
{

    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $redis = new \Redis();
        $redis->connect('localhost', 6379);
        return $redis;
    }
}

```

controller \_\_construct like:

 ![3333](https://canada1.discourse-cdn.com/flex032/uploads/zendframework/original/2X/5/5544e03e66ae5b6148803b60987b1afb5fb63ee3.png)

```bash
class ForumController extends AbstractActionController
{
    public $forumMainTable;
    public $userFavoriteTable;
    public $baseFuncModel;
    public $forumClassTable;
    public $redis;

    public function __construct(ForumMainTable $forumMainTable, UserFavoriteTable $userFavoriteTable, BaseFuncModel $baseFuncModel, ForumClassTable $forumClassTable, Redis $redis)
    {
        $this->forumMainTable = $forumMainTable;
        $this->userFavoriteTable = $userFavoriteTable;
        $this->baseFuncModel = $baseFuncModel;
        $this->forumClassTable = $forumClassTable;
        $this->redis = $redis;
    }

```

Not work in this way.

---

<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 15, 2021, 9:45am UTC](https://discourse.laminas.dev/t/how-the-redis-work-in-laminas-mvc-not-as-cache-storage/2354/11 "2021-06-15T09:45:15Z")

</div>

> [@jobsfan](#):
>
> I got the error
> 
> ```auto
> Unable to create controller "Application\Controller\ForumController"; unable to resolve parameter "redis" using type hint "Application\Controller\Redis"
> 
> ```

See the error message because there is no class `Application\Controller\Redis` and for that reason PHPStorm highlights `Redis` in your controller.

Typical namespace error: you must import the `Redis` in your controller class:

```php
// …
use Laminas\Mvc\Controller\AbstractActionController;
use Redis;

class ForumController extends AbstractActionController
{
    // …
}

```

---

<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:** [June 16, 2021, 1:25am UTC](https://discourse.laminas.dev/t/how-the-redis-work-in-laminas-mvc-not-as-cache-storage/2354/12 "2021-06-16T01:25:56Z")

</div>

@froschdesign

Thank you! I adopt your way. I belive it is the best way! Thank you so much for your time and efforts.
