I have a script which connects to a database, pulls a row, displays this data on a jpeg image and then displays itself. The script is called with an ?id=1 which selects what row to get from the database. Somehow I want to cache it so that it hasnt got to query the database every single its run. Perhaps it had a life time of some sort? For example if it hasnt been queried in the past 5 minutes then perform the query - if not then just display the cahced image. Any ideas how I can do this? Thanks
Store the generated image in a cache directory somewhere on your server. To identify which image is for which ID, you can either name the image with the ID or store this ID, file name and creation time in a database. In your PHP code, when a request for an image is received, you'll then first check whether the cached image exists and up to date. Another option is to use the Apache modules mod_rewrite to give the images a name like one for static file (e.g.: imgid-13.jpg), but is handled by a script that you specify in your rewrite rule. You may also want to use mod_header to control the cache expiry time. This is of course assuming you have access to make changes to Apache, or if it's already installed with the required modules. An advantage of doing it this way is, the caching is done on the proxy/cache server and/or users' browsers, so you can also save your server's bandwidth.