I'm using PHP to render LaTeX to pngs as part of a CMS, I need some kind of simple caching mechanism because it takes the server about 2 seconds to render the png. I take the raw TeX and run it through the md5 function, then I insert the hash into the database along with the current timestamp, and use the <hash>.png as the filename. Every time the image is accessed the timestamo is updated.
Hmm, I think that it's a good approach, but you'll probably want to do some additional processing on the LaTeX before you hash it. Otherwise, changes in whitespace (and potentially comments and some other things, depending on your input method) would result in different hashes.
Also, what's the purpose of updating the timestamp? As part of a batch method that removes PNGs that haven't been accessed in a long time?
This sounds fine. If you could change the name of the .png to say, the database ID or even the hash itself so that the image can be cached on the client side as well.
For this type of scenario, I prefer to use a temporary directory on disk.
On request:
if the file doesn't exist
render it to the file
fpassthru() the file from disk
If the .png's could change after they've been rendered, I guess you also should account for that using some sort of observing whenever the .png is updated (checking thread, set methods to update a last-modified key, or something else :) )