Caching the request using Akismet-based SLIM 4 installation

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

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

Have you tried this example?

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 :wink: ))

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

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:

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

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

Create a new action class in: src/Action/AllActsReaderAction.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:

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

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

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