views:

128

answers:

4

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.

A: 

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?

Chad Birch
You are correct about my intent for using the timestamp.
GameFreak
+1  A: 

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.

John Rasch
The filename IS the hash.
GameFreak
You did not indicate that in the question.
John Rasch
Ok I see the problem. I had typed <hash>.png in the question but <hash> was interpreted as a HTML tag and was stripped from the post.I will go and fix it now.
GameFreak
Interesting, you'd think it would convert that automatically...
John Rasch
+2  A: 

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
Ryan Graham
you can use the file's time stamp for the cache regeneration control.
sfossen
A: 

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 :) )

cwap