views:

134

answers:

3

Hi,

On my Apache server I have a PHP service that gets requests and generates an image from them. What I want to do is to cache the generated image on my server (not client's browser) once it was generated, so if the image with specific parameters has been generated already and someone requests it my web server will return cached image instead of generating a new one.

I know I could store generated images on the server manually and on each request check if they exist, and then run cron tasks to remove expired images, but I am looking for more efficient way.

Thanks.

+1  A: 

You already mentioned the most efficient way. Store it on disk. You could possibly use something like memcache, store it in memcache and then have memcache auto-expire the record after a certain amount of time.

X-Istence
@nigative, I'd recommend carefully reading the documentation on memcached and doing some testing, since by default the max object size is 1MB, which might be smaller than your images. http://dev.mysql.com/doc/refman/5.0/en/ha-memcached-faq.html
John Paulett
So in that case I still need to handle everything in my php code?Generate a key from request string, check if there is a record for this key in memcache, and if it is there then return it, otherwise generate a new image, key and store it in memcache?
negative
+1  A: 

Like you said, store it on the disk and read it back to the client. You could have ID.ext and ID.date where ID.date contains a expire date (in unix time). Then just compare what is in ID.date to what time() says, if time() is bigger than what ID.date is, then delete the two files and re-generate the image.

Henri Watson
ID.date is not required, just stat() the file to get the date it was created.
X-Istence
+1  A: 

Depending on how you're generating the images, you might be able to use mod_cache to do your caching for you.

Amber
It looks like mod_expires caching pages in browser (HTTP Header), not on server.
negative
Oh, whoops. Thought one thing, typed/linked another. Actually meant to suggest mod_cache, will edit.
Amber