Page 1 of 1

Cache an Image

Posted: Sun Jun 11, 2006 8:47 am
by derchris
Hi folks,

just something easy, I think.
I have a Script, which retreives some Infos from 3rd party websites, and then generates an Image
out of the data.
Now, it can take some sec. before the Image is created and displayed on the screen.
What I would like to do is cache the Images for like one hour.
Everytime I use the script, it should check if there is a cache version available, check if this is still valid, and display it.
If it isn't vailid anymore, then he should do like always, grab info from Webpage, save it as cache and display it.
How would it be the easiest way to get this working ?

Thanks in advance.

Posted: Sun Jun 11, 2006 8:59 am
by feyd
use a hash of something unique from the request known ahead of time (if the sites vary) or the same filename if they don't and some time reference like filemtime()

Posted: Sun Jun 11, 2006 10:40 am
by derchris
I was thinking about doing it with mysql.
Everytime I create the Image, add a date / time in a colum in the DB.
And then on every load check the date / time in the DB against the current date / time.
Depending on the difference, either show the cached Image or create it with new Infos.

Posted: Sun Jun 11, 2006 11:21 am
by bokehman
Depending what you are doing and the complexity of the image, caching might be more trouble than it is worth. Your biggest overhead in this case is accessing 3rd party websites to collect data. Instead of caching the image itself you might want to consider caching the collected data and making the image on the fly on each access. Whether this is a good idea will depend on your specific case.

Posted: Sun Jun 11, 2006 11:39 am
by derchris
Thats the thing.
There are not only Text infos I'm looking at, but Images aswell.
Some of them have to be resized, so I thought I can speed it up by caching the whole thing.