Coldfusion Cached Image Rendering
I recently had the need to do some client side file caching for static files and images. Not usually a problem, just setup IIS to expire after X days for that directory. But for this site profile images were stored in the database, so coldfusion would have to control the cache headers on image render.
I attached the code for imageRender.cfm which adds two headers
< cfheader
name="Expires"
value="#GetHttpTimeString(dateadd('d', 30, Now()))#"
/>
< cfheader
name="Last-Modified"
value="#GetHttpTimeString(qry_imageRender.lastModified)#"
/>
The render page then does a check to see if the file has been modified before it outputs the file to the client and if it can sends a not modified header. This saves quite a bit of server traffic and as a result lowers the load time for a typical page view on the site.
This could work for any type of page but especially helpful when serving images or other large static files. Hope this helps someone, as most of my initial google searches showed how to not cache a script.


Hi Justin,
This looks like what I was after when I started my search, but I’m certainly no CF expert, so I’m not quite sure how to get it going.
*Coincidently, I see you are at Real Art – I’m at AIDA-America which is just off of 70 over here past Huber Heights.
Any help would be great!
Thanks.
-Matt
Sure Matt,
I posted the script that I use, it should give you a little more direction. imageRender
You should just be able to update the data source and fields in the query and get it working. Let me know if this gets your going.
Always glad to help a fellow Dayton guy.
JD
Justin,
I’m not actually calling from a database. I was thinking that maybe I could apply this to all images, so that when a page has the tag, that when it gets pushed to the browser, the appropriate cache header is included.
Make sense? Am I looking at this in the wrong way? Again, I’m def. no expert when it comes to ColdFusion. What I know I have basically just taught myself by trolling along on message boards, forums and blogs (like how I found you . . .)
THANKS!
Matt,
The code would work for a regular page, but it only applies to the single file, in that case your main cf page. For static files like jpeg and javascript files you will have to set that up in IIS.
http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/0fc16fe7-be45-4033-a5aa-d7fda3c993ff.mspx?mfr=true
I would recommend looking at the Yslow fire fox plug-in to see the individual files and their expire dates that load for a single page.
JD
Justin,
Unfortunately I’m on a shared sever so I don’t have the IIS access that would be required for that.
I am using YSlow and PageSpeed currently to see the expire dates and other info for my pages – they are great tools!