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?
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?
– Papa Charlie
@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!
– chambelix
@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... :)
– chambelix
That gain of
30%
is quite significant. I gave a simple answer, then I will test something else and update the answer. :)– Papa Charlie
@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!
– chambelix