I have a method for detecting a post's critical css when the page is requested and in-lining it to optimize page speed. I would like to cache this css to avoid having to compile it for every page request. I am using
file_get_contents on multiple css files to concatenate the css and I've been told this means a lot of read operations in the file system per request and it isn't optimal for server load.
I am trying to decide whether to cache this critical css in the file system or in the database, maybe even as it's own post type. I know I am going to use a hash of the css filenames used to generate the cache file as the name of the cache file. On page request I would use this hash to check if their is a cached file and either inline or en-queue that cached file.
I am in-lining this cached file when the browser would have an old version of it or not have it already cached, and this is where I need to decide whether to
file_get_contents the puppy or get it from the database. Like I said before, I was told to minimize the amount of read operations done on the server and I can reduce this by storing the css to inline in the database instead.
Would getting a post and its content from the database be better than calling
file_get_contents()? If the CSS is rather large, maybe it would be, and I know that MySQL has its own caching built in which is probably better than any caching system I could write. However reading one file on disk doesn't seem SO bad and it wouldn't happen on every request for every page, just a few times per user session probably.
This is kind of a long winded post. Has anyone else come up with a solution to critical CSS like this besides using a plugin?