douzuita7325 2015-12-23 04:58
浏览 116

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

I know how to create a cache page with plain php, for instance,

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

But how can I do it in Slim 3 or other micro frameworks?

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

Any ideas?

  • 写回答

2条回答 默认 最新

  • douchen4547 2015-12-23 05:38
    关注

    You can create a cache file similiarly the way you created in plain php.There is no need to do extra effort for slim3.

    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法