The Scenario
I am building a custom CMS for a communications application. The users can upload images to the server and then later reference them in posts they write using markdown. Images are stored in a database and referenced with a url of the form images/{id}
. A custom image handler retrieves these and sets a far future expires header so they aren't fetched over and over again. Storing the images in the file system is not an option per the customer.
Posts are stored in the database as markdown and as html for performance.
Markdown
###Header
Lorem Ipsum dolor sit amet. ![funny cat](images/25)
HTML
<h3>Header</h3>
<p>
Lorem Ipsum dolor sit amet. <img alt="funny cat" src="images/25" />
</p>
The Problem
These images are also editable. From a caching perspective this presents a problem. When an image is edited I need to ensure that the browser gets the latest version. I have come up with the following solutions, all of which I have found to be lacking.
Possible Solutions
Version Field
Store a version field with the image. Increment it when the image is edited producing urls of the form images/{id}/version/{version}
.
This is nice if image urls are always generated from the database. However, I am storing urls in posts as text and would have to preprocess possibly large swaths of text for these requests. Also, linking an image would be troublesome since a url becomes stale after an image is edited. This is probably not a good idea.
New Url
When an image is edited, store it as a new entry in the database.
This means no version upkeep, but old links and old posts suffer from the same issues. They're never updated. The performance would be good, but the problem remains.
304 - Not Modified
Store a last edited field for each image. When a conditional request comes in return http status 304 - Not Modified, reducing bandwidth usage.
This seems like the best solution I've got so far. The image is cached unless edited, but I've never used this approach before. If there are 50 images on the page there are still 50 requests to the server. Bandwidth will be reduced, but the latency remains. Is this approach worth that cost?
The Question
I have very little experience in this area and I'm hoping all of you have more. Are any of the above solutions viable? If so what is your experience with them? If not what is a better approach?