【问题标题】:How can I cache files into sub folders to reduce the number of inodes in the cache folder?如何将文件缓存到子文件夹中以减少缓存文件夹中的 inode 数量?
【发布时间】:2012-08-08 23:19:51
【问题描述】:

我的站点正在正确缓存指定文件夹中的所有文件,但是这些文件夹中存储的文件太多。我正在考虑将缓存文件夹拆分为包含文件名第一个字母的子目录,然后可能从那里进一步分解。

如下所示,压缩版本保存在“gz”文件夹中,常规缓存保存在“html2”文件夹中。

代码:

if ( $cache ) {
#header("Content-Type: text/html");
// get the buffer
$buffer = ob_get_contents();
#$length = ob_get_length();
#header('Content-Length: '.$length);
// end output buffering, the buffer content
// is sent to the client
ob_end_flush();

// now we create the cache file
if (!file_exists($pathName)) {
    mkdir($pathName, 0755, true);
}
if (!file_exists(str_replace('/html/html2/', '/html/gz/', $pathName))) {
    mkdir(str_replace('/html/html2/', '/html/gz/', $pathName), 0755, true);
}
$compressed = preg_replace('%<!--(.|\s)*?-->%', '', $buffer);
$compressed = sanitize_output($compressed);
$encoded = gzencode($compressed, 9);
file_put_contents($file, $compressed);
file_put_contents(str_replace('/html/html2/', '/html/gz/', str_replace('.html', '.gz', $file)), $encoded);

}

根据上面的代码,这里是当前缓存文件的路径:

/html2/New-York-Hotels.html

/gz/New-York-Hotels.gz

理想情况下,我希望缓存的文件位置如下所示:

/html2/N/New-York-Hotels.html

/gz/N/New-York-Hotels.gz

非常感谢您的帮助!提前致谢。

【问题讨论】:

  • 有点跑题了,但是为什么要同时存储压缩的未压缩的版本?存储一种类型(最常用的)并按需转换为另一种不是更好吗? Gzip 并不是一个特别耗费时间或资源的过程,如果数据是 HTML,它可能不会很大。
  • 戴夫,这个网站是由一个不再为我们工作的人建立的,我不确定这两种类型是否都被缓存了。我所知道的是,我不想做任何危害我们交通的事情。谢谢你的关心;-D

标签: php caching


【解决方案1】:

试试这个代码(FIXED):

if ($cache) {

  // Get the buffer into a string
  // I do it this way to save memory - no point in keeping 2 copies of the data
  $buffer = ob_get_clean();

  // Send buffer content to the client
  // header("Content-Type: text/html");
  // header('Content-Length: '.strlen($buffer));
  echo $buffer;
  flush();

  // Get the paths into sensibly named variables
  $fileBase = basename($file); // The name of the HTML file
  $htmlPath = rtrim($pathName, '/').'/'.strtoupper($fileBase[0]).'/'; // The directory the HTML file is stored in
  $gzPath = str_replace('/html/html2/', '/html/gz/', $htmlPath); // The directory the gzipped file is stored in
  $htmlFile = $htmlPath.$fileBase; // The full path of the HTML file
  $gzFile = $gzPath.str_replace('.html', '.gz', $fileBase); // The full path of the gzipped file

  // Make sure the paths exist
  if (!is_dir($htmlPath)) {
    mkdir($htmlPath, 0755, true);
  }
  if (!is_dir($gzPath)) {
    mkdir($gzPath, 0755, true);
  }

  // Strip comments out of the file and sanitize_output() (whatever than does)
  // $compressed is a silly name for a variable when we are also zipping the data
  $html = sanitize_output(preg_replace('%<!--(.|\s)*?-->%', '', $buffer));

  // Save the files
  file_put_contents($htmlFile, $html);
  file_put_contents($gzFile, gzencode($html, 9));

}

这里有几个未经检查的潜在错误需要处理,例如如果mkdir() 失败会发生什么,如果file_put_contents() 失败会发生什么?

【讨论】:

  • 非常好,就像一个魅力。我将不得不解决提到的潜在错误。感谢您的帮助!
  • 我真的希望 Stack Overflow 提供一个功能,您可以购买啤酒来帮助您解决问题!
  • @NotJay 不用担心,StackOverflow 是灵魂的啤酒...... :-D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-27
  • 1970-01-01
  • 1970-01-01
  • 2016-02-07
  • 1970-01-01
  • 1970-01-01
  • 2021-08-12
相关资源
最近更新 更多