PHP:缓存整洁的解析字符串

发布于 2024-11-30 07:05:45 字数 956 浏览 3 评论 0原文

在我的页面顶部,我有这段代码来检查缓存并启动输出缓冲:

ob_start( );
    $cache_time = 3600; 
    $cache_folder = $_SERVER['DOCUMENT_ROOT'].'/cache'; 
    $cache_filename = $cache_folder.md5($_SERVER['REQUEST_URI']);
    $cache_created  = (file_exists($cache_filename)) ? filemtime($cache_filename->filename) : 0;  

    if ((time() - $cache_created) < $cache_time) {  
      readfile($cache_filename); 
      die();  
    }  

然后在底部我用它来整理输出缓冲区并缓存页面,但似乎没有任何内容被缓存......

$html = ob_get_clean();
$config = array('indent' => TRUE,
                'drop-empty-paras' => FALSE,
                'output-xhtml' => TRUE,
                'quote-ampersand' => TRUE,
                'indent-cdata' => TRUE,
                'tidy-mark' => FALSE,
                'wrap' => 200);
$tidy = tidy_parse_string($html, $config, 'UTF8');
file_put_contents($cache_filename, $tidy);
echo $tidy;

任何人都知道什么做什么?

At the top of my page I have this piece of code to check cache and initiate output buffering:

ob_start( );
    $cache_time = 3600; 
    $cache_folder = $_SERVER['DOCUMENT_ROOT'].'/cache'; 
    $cache_filename = $cache_folder.md5($_SERVER['REQUEST_URI']);
    $cache_created  = (file_exists($cache_filename)) ? filemtime($cache_filename->filename) : 0;  

    if ((time() - $cache_created) < $cache_time) {  
      readfile($cache_filename); 
      die();  
    }  

Then at the bottom I use this to tidy the output buffer and cache the page, but it nothing appears to be cached...

$html = ob_get_clean();
$config = array('indent' => TRUE,
                'drop-empty-paras' => FALSE,
                'output-xhtml' => TRUE,
                'quote-ampersand' => TRUE,
                'indent-cdata' => TRUE,
                'tidy-mark' => FALSE,
                'wrap' => 200);
$tidy = tidy_parse_string($html, $config, 'UTF8');
file_put_contents($cache_filename, $tidy);
echo $tidy;

Anyone know what to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ら栖息 2024-12-07 07:05:45

您的代码中存在一些错误:

  • $cache_filename 是一个字符串,而不是一个对象
  • $tiny()-对象不能简单地放入文件中 - 您需要采用

我像这样修复它们的值,以php 手册页

<?php
    $cache_time = 10; 
    $cache_folder = '/tmp/'; 
    $cache_filename = $cache_folder.md5($_SERVER['REQUEST_URI']);
    $cache_created  = (file_exists($cache_filename)) ? filemtime($cache_filename) : 0;  
    var_dump(time() - $cache_created);
    if ((time() - $cache_created) < $cache_time) {  
      readfile($cache_filename); 
      exit();  
    } 
?>

<?php
ob_start();
?>

<html>
  <head>
   <title>test</title>
  </head>
  <body>
   <p>error<br>another line</i>
  </body>
</html>

<?php

$buffer = ob_get_clean();
$config = array('indent' => TRUE,
                'output-xhtml' => TRUE,
                'wrap' => 200);

$tidy = tidy_parse_string($buffer, $config, 'UTF8');

$tidy->cleanRepair();
file_put_contents($cache_filename, $tidy->value);
echo $tidy;
?>

There were some errors in your code:

  • $cache_filename is a string, not an object
  • the $tiny()-object can't be simply put into a file - you need to take the value

I fixed them like so, taking the example of the php manual page:

<?php
    $cache_time = 10; 
    $cache_folder = '/tmp/'; 
    $cache_filename = $cache_folder.md5($_SERVER['REQUEST_URI']);
    $cache_created  = (file_exists($cache_filename)) ? filemtime($cache_filename) : 0;  
    var_dump(time() - $cache_created);
    if ((time() - $cache_created) < $cache_time) {  
      readfile($cache_filename); 
      exit();  
    } 
?>

<?php
ob_start();
?>

<html>
  <head>
   <title>test</title>
  </head>
  <body>
   <p>error<br>another line</i>
  </body>
</html>

<?php

$buffer = ob_get_clean();
$config = array('indent' => TRUE,
                'output-xhtml' => TRUE,
                'wrap' => 200);

$tidy = tidy_parse_string($buffer, $config, 'UTF8');

$tidy->cleanRepair();
file_put_contents($cache_filename, $tidy->value);
echo $tidy;
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文