Bundle / Cache of CSS files in PHP

1

I'm developing a mechanism in PHP that performs a cache of CSS files in order to decrease the number of requests of a page and the loading time.

The engine evaluates in a first entry whether there is any cache file on the server for the CSS load file set and if it does not exist it will create a file that is unique and includes the contents of all CSS .

For the file name I use a hash that results from evaluating the contents of each file. So if someone changes a character that is a new file it will be generated, which will only happen during development and move with CSS .

This mechanism is much more advantageous than having a request for every CSS . You can greatly reduce the load time of a page and especially the number of requests of the page.

However the evaluation of a hash for each file CSS is time consuming but for me it seems to me the only way to ensure that if any CSS changes, the cache will always have the expected content. I use for performance issues the hash_file function within a cycle:

$fhash .= hash_file('crc32b', $filepath);

Is there any better performance function for this job? Does anyone know of another mechanism or algorithm?

If CSS is only modified in development and never in production, should not it be better to evaluate a hash for the set of CSS file names to load, eliminating content evaluation?

    
asked by anonymous 10.08.2015 / 13:44

1 answer

1

Generally when working with cache, I consider the date of change of the determining file, so an alternative would be to create a hash using filemtime " based on the timestamp when the contents of the file were last modified.

  

This function returns the time when the information block of a file was initially written, that is, the time when the contents of the file were modified.

     

Returns the time of the last modification of the file, or FALSE in case of an error. Time is returned as a Unix timestamp, which is appropriate for date () function

echo filemtime( 'style.css' )
// 1427338769 - output inicial
// 1439229908 - output após a alteração do arquivo

// aplicando o HASH, output: e2510ea8
echo hash( 'crc32b' , filemtime( 'style.css' ) )
    
10.08.2015 / 20:17