简体   繁体   English

什么是缓存(自动版本)通过组合多个css文件创建的php文件的有效方法?

[英]What is an efficient way to cache (auto version) a php file that is created by combining multiple css files?

I have a php file called main.css.php that collects multiple css files and outputs them as one large css file. 我有一个名为main.css.php的php文件,该文件收集多个CSS文件并将它们输出为一个大的CSS文件。 The number of css files that it collects can range from 1-10. 它收集的css文件数量可以在1到10之间。 The code is something like this: 代码是这样的:

header('Content-type: text/css');
header('Cache-Control: max-age=31536000');
ob_start("compress");
function compress($buffer) {
    /* remove comments */
    $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
    /* remove tabs, spaces, newlines, etc. */
    $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
    return $buffer;
}

// include css files
foreach ($css_files as $path) {
    if (file_exists($path)) {
        include_once($path);
    }
}
ob_end_flush();

So I have that part sorted but now I'm not sure how to cache it with a way that lets me update the cache if any of the original css files change. 因此,我对该部分进行了排序,但是现在我不确定如何使用某种方式来缓存它,如果任何原始css文件发生更改,该方式都可以让我更新缓存。 As suggested in this answer I was going to do something like: 正如这个答案所建议的,我将要做的事情是:

$cache = "";
foreach ($css_files as $path) {
    $cache.= filemtime($path);
}
...
echo "<link rel='stylesheet' href='/css/base.{$cache}.css'>";

However I can't use this apache rewrite rule: 但是我不能使用此apache重写规则:

RewriteEngine on
RewriteRule ^(.*)\.[\d]{10}\.(css|js)$ $1.$2 [L]

as I'm not sure how long the $cache will be as it will contain multiple unix timestamps. 因为我不确定$ cache会持续多久,因为它将包含多个unix时间戳。

Summary 摘要

I need to combine multiple css files into one php file that is sent to the browser with a css header. 我需要将多个css文件合并为一个php文件,该文件通过css标头发送到浏览器。 I then needed to correctly cache this file but with a way that allows me to update the cache if any of the original css files change. 然后,我需要正确地缓存此文件,但是需要一种方法,如果任何原始CSS文件发生更改,该方法都可以让我更新缓存。

Any advice, suggestions, or better ways to go about what I am doing would be much appreciated. 任何有关我正在做的事情的建议,建议或更好的方法,将不胜感激。

You should make a hash of the $cache variable and use that in the filename. 您应该对$cache变量进行哈希处理,然后在文件名中使用它。 It would be something like this: 就像这样:

<?php

$cache = "";
foreach ($css_files as $path) {
    $cache .= filemtime($path);
}

$cache = md5($cache);

And your rewrite rule would use the length of the hash function; 您的重写规则将使用哈希函数的长度; in this case, we use md5 , which is 32 characters long: 在这种情况下,我们使用md5 ,它是32个字符长:

RewriteEngine on
RewriteRule ^(.*)\.[\d]{32}\.(css|js)$ $1.$2 [L]

Hope that helps 希望能有所帮助

Why not store cache info on disk? 为什么不将缓存信息存储在磁盘上? This way you don't need to worry about .htaccess. 这样,您无需担心.htaccess。

$prevTimes = filegetcontents('/cache/css.txt')
if(array_sum($filetimes) != (int) $prevTimes){
  //recompile css
  //update csscacheinfo.txt with $newTimes
  //echo css file link for stylesheet using $newTimes.css as css file
} else {
  //echo css file link for stylesheet using $prevTimes.css as css file
}

FYI: It's faster to store / add integers than to concatenate and compare strings. 仅供参考:存储/添加整数比连接和比较字符串要快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM