Skip to content

Abort execution early if client side cache is still valid #10

@micheh

Description

@micheh

I wonder if the current implementation of the HTTP Cache is really performant. To me, it looks like the cache middleware creates the whole response first and then compares the Last-Modified and ETag header of the response with the request to determine if a 304 status code should be returned. Therefore the complete response is created but will not be used, since Slim will not output the body if the status is 304.

In Slim 2 the abort method was used to immediately stop the execution as soon as the Last-Modified/ETag were set (assuming the client side cache was still valid). I think it would also be a good idea for Slim 3 to return the 304 response as soon as possible. This will make the usage a bit more verbose and not automagically, but will probably improve CPU/memory usage and response times.

For example, the current checks in the Cache middleware could be moved to a new method in the cache provider (e.g. isStillValid()):

$app->get('/foo', function ($req, $res, $args) {
    $resWithEtag = $this['cache']->withEtag($res, 'abc');

    if ($this['cache']->isStillValid($req, $res)) {
        return $resWithEtag->withStatus(304); // or even another convenience method
    }

    //  ... some intensive calls to create the response
    $resWithEtag->write($body);

    return $resWithEtag;
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions