Php Slim 3:如何创建缓存页?

Php Slim 3:如何创建缓存页?,php,caching,slim-3,Php,Caching,Slim 3,例如,我知道如何使用普通php创建缓存页面 // @ref: http://wesbos.com/simple-php-page-caching-technique/ // // define the path and name of cached file $cachefile = 'cache/'.date('M-d-Y').'.php'; // define how long we want to keep the file in seconds. I set mine to 1 hou

例如,我知道如何使用普通php创建缓存页面

// @ref: http://wesbos.com/simple-php-page-caching-technique/
//
// define the path and name of cached file
$cachefile = 'cache/'.date('M-d-Y').'.php';

// define how long we want to keep the file in seconds. I set mine to 1 hour.
$cachetime = 3600;

// Check if the cached file is still fresh. If it is, serve it up and exit.
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
    include($cachefile);
    echo '<!-- cached page - '.date('l jS \of F Y h:i:s A', filemtime($cachefile)) . ' -->';
    exit;
}

// if there is either no file OR the file to too old, render the page and capture the HTML.
ob_start();
?>
    <html>
        output all your html here.
    </html>
<?php

// We're done! Save the cached content to a file
$fp = fopen($cachefile, 'w');
fwrite($fp, ob_get_contents());
fclose($fp);

// finally send browser output
ob_end_flush();

有什么想法吗?

你可以像在普通php中创建的那样创建一个缓存文件。没有必要为slim3做额外的工作。

你可以像在普通php中创建的那样创建一个缓存文件。没有必要为slim3做额外的工作。

我使用的是同样的Slim-3框架,但我将缓存用作中间件:

<?php
    $app->add(
        new \App\Middleware\HttpCache\Cache($container)
    );

我使用的是相同的Slim-3框架,但我将缓存用作中间件:

<?php
    $app->add(
        new \App\Middleware\HttpCache\Cache($container)
    );

请举个例子…?请举个例子…?谢谢。我喜欢将缓存用作中间件的想法!谢谢我喜欢将缓存用作中间件的想法!
<?php

    namespace App\Middleware\HttpCache;
    use App\Middleware\Middleware;

    class Cache extends Middleware {
        *// your methods go here*
    }