douhui1333 2013-02-15 20:34
浏览 69
已采纳

我可以像register_shutdown_function一样使用fastcgi_finish_request()吗?

This simple method for caching dynamic content uses register_shutdown_function() to push the output buffer to a file on disk after exiting the script. However, I'm using PHP-FPM, with which this doesn't work; a 5-second sleep added to the function indeed causes a 5-second delay in executing the script from the browser. A commenter in the PHP docs notes that there's a special function for PHP-FPM users, namely fastcgi_finish_request(). There's not much documentation for this particular function, however.

The point of fastcgi_finish_request() seems to be to flush all data and proceed with other tasks, but what I want to achieve, as would normally work with register_shutdown_function(), is basically to put the contents of the output buffer into a file without the user having to wait for this to finish.

Is there any way to achieve this under PHP-FPM, with fastcgi_finish_request() or another function?

$timeout = 3600; // cache time-out
$file = '/home/example.com/public_html/cache/' . md5($_SERVER['REQUEST_URI']); // unique id for this page
if (file_exists($file) && (filemtime($file) + $timeout) > time()) {
  readfile($file);
  exit();
} else {
  ob_start();
  register_shutdown_function(function () use ($file) {
    // sleep(5);
    $content = ob_get_flush();
    file_put_contents($file, $content);
  });
}
  • 写回答

3条回答 默认 最新

  • douyue3800 2013-02-23 10:33
    关注

    Yes, it's possible to use fastcgi_finish_request for that. You can save this file and see that it works:

    <?php
    
    $timeout = 3600; // cache time-out
    $file = '/home/galymzhan/www/ps/' . md5($_SERVER['REQUEST_URI']); // unique id for this page
    if (file_exists($file) && (filemtime($file) + $timeout) > time()) {
      echo "Got this from cache<br>";
      readfile($file);
      exit();
    } else {
      ob_start();
      echo "Content to be cached<br>";
    
      $content = ob_get_flush();
      fastcgi_finish_request();
      // sleep(5);
    
      file_put_contents($file, $content);
    }
    

    Even if you uncomment the line with sleep(5), you'll see that page still opens instantly because fastcgi_finish_request sends data back to browser and then proceeds with whatever code is written after it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘