2015-12-23 12 views
1

我知道如何創建一個緩存頁面純PHP,例如,苗條3:如何創建緩存頁面?

// @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(); 

但我怎麼能做到這一點的苗條3或其他微型框架?

use Slim\Http\Request; 
use Slim\Http\Response; 

require_once __DIR__ . '/../vendor/autoload.php'; 

$app = new \Slim\App(); 

// Routes: 
$app->get('/', function (Request $request, Response $response, array $args) { 
    $response->getBody()->write('Hello, World!'); 

    return $response; 
}); 

$app->run(); 

任何想法?

回答

-1

您可以創建一個緩存文件,類似於您在純php中創建的方式。不需要爲slim3做額外的工作。

+0

請舉個例子...? – laukok

1

我使用的是相同的,修身-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* 
    } 
+0

謝謝。我喜歡使用緩存作爲中間件的想法! – laukok