Jump to content

Caching parsed text files


NotionCommotion

Recommended Posts

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:

  1. Append the filename along with the serialized value of the array, and hash it using md5().
  2. Store the parsed file using name "file_".$hash
  3. Get the modification time of the newly created file using filemtime(), and store the value in a new file called "time_".$hash.
  4. bla bla bla
  5. When the next request comes in to parse a file, create the hash again.
  6. 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?

Link to comment
Share on other sites

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 by QuickOldCar
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 by QuickOldCar
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.