Bundle/Cache CSS files in PHP

Asked

Viewed 84 times

1

I’m developing a mechanism on PHP performing a file cache CSS in order to reduce the number of requests one-page and loading time.

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

For the file name I use a hash that results from the evaluation of the contents of each file. So if any change a character that is a new file will be generated, which will only happen during development and mess with CSS.

This mechanism is much more advantageous than having a request for each CSS. You can greatly decrease the load time of a page and in particular the number of requests for it.

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

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

Is there a better performance function for this work? Does anyone know another mechanism or algorithm?

It is certain that the CSS is only modified in development and never in production it will not be better to evaluate a hash by the set of CSS filenames to load, eliminating content evaluation?

  • 1

    I don’t know if it brings light to your problem, but have you considered using filemtime to create the hash based on when the file content has been modified?

  • @Papacharlie there is a good question! But it seems to me a good option...I don’t know how I didn’t remember it. I will test and I will see the difference in performance. I will tell you something later... If you remember anything else thank you very much for your help!

  • @Papacharlie effectively get a real gain about 30% in the project where I tested that will certainly result in a very good server gain with many requests! Don’t want to put your comment in response so I can value your answer? And add some more ideas... :)

  • That gain of 30% is quite significant. I gave a simple answer, then I will test something else and update the answer. :)

  • 1

    @Papacharlie tested in another project where I have to load some 8 CSS and some of the JQUERY-UI type among others is very good... :) Went to a request only, with load time on average of 19ms. I’m going to implement it in an MVC framework that I’m developing. Your tip was important!

1 answer

1


Usually when I work with cache, I consider the file change date determinant, so an alternative would be to create a hash using filemtime based on the timestamp of 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 file modification, or FALSE in case of an error. Time is returned as a Unix timestamp, which is suitable 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' ) )
  • 1

    clearly a tip that improved my running time! I’m sorry I can only vote once. this. I’ll keep the question open to see if I can get a few more tips... but yours went right to the problem.

  • @chambelix, I tried to make a Bench on Ideone but did not scroll reading the files.

  • you really have to note the difference because the hash_file will evaluate the content and returns the hash of it while filemtime obtains the timestamp of the "allocation table" that the system has in memory. muuuuito mais rápido. your tip is definitely better. I don’t know how I didn’t remember it. :)

  • @chambelix, yes, I removed the comment... was processing another thought and mixed one thing with another... rs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.