slimphp / Slim-HttpCache

Slim Framework HTTP cache middleware and service provider
MIT License
115 stars 25 forks source link

Twig cache integration #15

Closed borgogelli closed 5 months ago

borgogelli commented 8 years ago

Hello

Does "Slim-HttpCache" integrate with the twig cache or is an alternative to it ?

Thank you

Andrea

odan commented 5 months ago

This package and Twig's caching are complementary and serve different purposes but they can also be used together.

Slim-HttpCache is a middleware for HTTP caching. It handles caching at the HTTP level. This middleware uses cache-related HTTP headers such as Cache-Control, ETag, and Last-Modified to manage client-side and intermediary caches.

Twig's caching, on the other hand, is about caching the compiled template code. This reduces the overhead of compiling templates every time they are rendered.

Simple example (not tested)

$app->get('/', function (Request $request, Response $response, $args) use ($twig) {
    // Set Cache-Control and other HTTP cache headers
    $cacheProvider = new CacheProvider();
    $response = $cacheProvider->withExpires($response, time() + 3600);
    $response = $cacheProvider->withEtag($response, md5('home-page'));

    // Render template with Twig
    return $twig->render($response, 'home.twig');
});