# Caching the request using Akismet-based SLIM 4 installation

**URL:** <https://discourse.slimframework.com/t/caching-the-request-using-akismet-based-slim-4-installation/4450>\
**Category:** Questions\
**Created:** [October 16, 2020, 7:49am UTC](https://discourse.slimframework.com/t/caching-the-request-using-akismet-based-slim-4-installation/4450 "2020-10-16T07:49:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hcape](https://avatars.discourse-cdn.com/v4/letter/h/f1d935/32.png) [@hcape](https://discourse.slimframework.com/u/hcape)\
**Post date:** [October 16, 2020, 7:49am UTC](https://discourse.slimframework.com/t/caching-the-request-using-akismet-based-slim-4-installation/4450/1 "2020-10-16T07:49:54Z")

</div>

I am trying to use caching in SLIM 4 installation, which is based on Akismet-setup. So all of my request handlers are class type. There are a few requests that take considerably time (like 5-10 seconds) and I’d prefer to get them mostly from cache so that they normally would be fast. The data doesn’t change too often so caching seems to be a viable alternative.

I have added slim/httpcache to my installation, but as a new SLIM user I can’t figure out how to implement the actual caching to the app. I have tried to google the web for examples or a tutorial, but - alas - haven’t encountered so far anything my intelligence would convert as a working code…

Could anyone point me to the right direction, which allows me to use caching either on a select calls or even with all calls.

I haven’t added anything fancy in the implementation, so adding as an example the caching to akismet default handler (or as a general cache in the middleware) would probably give me enough hints to handle this from there on.

Otherwise Slim seems a very adequate framework (at least) for the backend API!

wbr

hank

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [October 17, 2020, 1:26pm UTC](https://discourse.slimframework.com/t/caching-the-request-using-akismet-based-slim-4-installation/4450/2 "2020-10-17T13:26:43Z")

</div>

Hi @hcape What have you tried so far? What was the concrete issue?

Have you tried this example?

> **[slimphp/Slim-HttpCache](https://github.com/slimphp/Slim-HttpCache#usage)**
>
> Slim Framework HTTP cache middleware and service provider - slimphp/Slim-HttpCache

---

<div class="post-metadata">

**Author:** ![hcape](https://avatars.discourse-cdn.com/v4/letter/h/f1d935/32.png) [@hcape](https://discourse.slimframework.com/u/hcape)\
**Post date:** [October 18, 2020, 3:52am UTC](https://discourse.slimframework.com/t/caching-the-request-using-akismet-based-slim-4-installation/4450/3 "2020-10-18T03:52:33Z")

</div>

Thanks @odan for your response.

I saw that example yes, but because of my missing knowledge (so far) of SLIM, I couldn’t “translate” it to Class-based handler.

So my routes is based like this:

`$app->get('/all[/{batch}]', AllActsGetHandler::class)->setName('allget');`

I would think that I somehow should put this into middleware but how to implement it there is beyond me (especially because I don’t have enough time to really dive into this (or basically anything 😉 ))

As I am in a **very tight** schedule, I had currently to implement that in the other end (in the system calling the backend API) but of course doing it on API-side would be much better alternative, only I don’t know how…

hank

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [October 18, 2020, 9:46am UTC](https://discourse.slimframework.com/t/caching-the-request-using-akismet-based-slim-4-installation/4450/4 "2020-10-18T09:46:16Z")

</div>

> [@hcape](#):
>
> I am trying to use caching in SLIM 4 installation, which is based on Akismet-setup.

I don’t know this special “Akismet-setup” setup. Can you add a link it to get more context?

Here is a class based example that used dependency injection. It requires a dependency injection container (e.g. PHP-DI).

Insert a `CacheProvider::class` container definition, e.g. in `config/container.php`:

```php
use Slim\HttpCache\CacheProvider;
// ...

CacheProvider::class => function () {
    return new \Slim\HttpCache\CacheProvider();
},

```

Create a new action class in: `src/Action/AllActsReaderAction.php`

```php
<?php

namespace App\Action;

use Slim\HttpCache\CacheProvider;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

final class AllActsReaderAction
{
    private $cacheProvider;

    public function __construct(CacheProvider $cacheProvider)
    {
        $this->cacheProvider= $cacheProvider;
    }

    public function __invoke(
        ServerRequestInterface $request, 
        ResponseInterface $response
    ): ResponseInterface {
        // Use the cache provider.
        $response = $this->cacheProvider->withEtag($response, 'abc');

        $response->getBody()->write('Hello world!');

        return $response;
    }
}

```

Then add the route:

```php
$app->get('/all[/{batch}]', \App\Action\AllActsReaderAction::class)->setName('allget');

```

---

<div class="post-metadata">

**Author:** ![hcape](https://avatars.discourse-cdn.com/v4/letter/h/f1d935/32.png) [@hcape](https://discourse.slimframework.com/u/hcape)\
**Post date:** [October 19, 2020, 7:45am UTC](https://discourse.slimframework.com/t/caching-the-request-using-akismet-based-slim-4-installation/4450/5 "2020-10-19T07:45:28Z")

</div>

Sorry, mixed two different things. What I tried to say was akrabat, not akismet 😊

I.e. I have installed slim 4 using this:

`composer create-project akrabat/slim4-starter api`

I haven’t yet had time to check your suggestion, will be doing it shortly

hank
