NotionCommotion Posted October 2, 2014 Share Posted October 2, 2014 I have some fairly small text files (2K) which are parsed where certain deliminated fields are replaced with values provided in an associated array. There will typically be less than 5 replaced fields, and I plan on using preg_replace_callback to find and replace them. I am contemplating caching the files after being parsed (note that they will only be accessed by PHP, and not directly by Apache). Do you think doing so will provide any significant performance improvement? If I do go this route, I would like thoughts on how to proceed. I am thinking something like the following: Append the filename along with the serialized value of the array, and hash it using md5(). Store the parsed file using name "file_".$hash Get the modification time of the newly created file using filemtime(), and store the value in a new file called "time_".$hash. bla bla bla When the next request comes in to parse a file, create the hash again. If the file exists for the given hash name, and the time file matches filemtime(), use that file, else parse the original file. Is this a good approach? Quote Link to comment https://forums.phpfreaks.com/topic/291395-caching-parsed-text-files/ Share on other sites More sharing options...
QuickOldCar Posted October 2, 2014 Share Posted October 2, 2014 (edited) More information what the purpose of all this is may help. Going by what you are doing I would just do a timestamp-array_value.txt md5() will help for storing safe filenames, but would have to save the hash somewhere to know the reverse. May even be easier to create folders of the array values and timestamp.txt within. At least that way you can target each folder if was to be many of them or multiple timestamped files to compare, or even types Like I said, more information would probably help. Edited October 2, 2014 by QuickOldCar Quote Link to comment https://forums.phpfreaks.com/topic/291395-caching-parsed-text-files/#findComment-1492548 Share on other sites More sharing options...
NotionCommotion Posted October 2, 2014 Author Share Posted October 2, 2014 More information what the purpose of all this is may help. Going by what you are doing I would just do a timestamp-array_value.txt md5() will help for storing safe filenames, but would have to save the hash somewhere to know the reverse. May even be easier to create folders of the array values and timestamp.txt within. At least that way you can target each folder if was to be many of them or multiple timestamped files to compare, or even types Like I said, more information would probably help. Thanks for your reply QuickOldCar, As for more information, the text will be parsed as described by http://forums.phpfreaks.com/topic/291400-constructive-criticism-on-parsing-text/. So, store a big array in a single file to find all the time stamps? What if there were a million elements? Would checking if a file exists be faster? I suppose I will have problems seeing if one of a million files exists as well??? Why would I need to save the hash somewhere other than as the filename? Wouldn't I just parse and save the file with name $md_filename, and then the next filename/array I get, hash it and check if the file exists? Thanks again $filename="bla bla.txt"; $array=array('a'=>123,'b'=>'Hello'); $string=$filename.serialize($array); $md_filename=md5($string); //Save file using this name Quote Link to comment https://forums.phpfreaks.com/topic/291395-caching-parsed-text-files/#findComment-1492555 Share on other sites More sharing options...
QuickOldCar Posted October 5, 2014 Share Posted October 5, 2014 (edited) I can see making a new folder like cache_files. You can look for cached files existing in that folder first. Keep file names the same. If the cached file exists then use it. If the cached version does not exist...run your script to transform them for initial display....make the new cache file for next time. Up to you about setting expire times on them or just always keep them for the future. I forgot to mention if this data changes in the file, you can just replace the cached file. Edited October 5, 2014 by QuickOldCar Quote Link to comment https://forums.phpfreaks.com/topic/291395-caching-parsed-text-files/#findComment-1492794 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.