Mobile app version of vmapp.org
Login or Join
Shanna517

: Avoid caching by creating photo names that contain unique identifiers? If you are building a website that will display photos to users of the site, is it important to always give each photo

@Shanna517

Posted in: #Browsers #Cache #Cdn #Images

If you are building a website that will display photos to users of the site, is it important to always give each photo a unique name?
For example, instead of naming a photo file using an index number such as "00001" like this:

<username>_00001.jpg


would it be better to name it using a unique random character string like this?

<username>_<random>.jpg


My thinking is that if the photo's owner were to want to replace that photo with a different photo and you give the new photo the same name as the old photo, then when another user views the photo, their browser will see that same photo name in its cache and display the old photo rather than a newer one.

I should add that the photos will be stored on a CDN such as Akamai or Amazon Cloudfront.

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Shanna517

2 Comments

Sorted by latest first Latest Oldest Best

 

@Si4351233

The question is different, but I believe that the accepted answer to this question might also work for you. tl;dr, you can generate a string of characters for each file and tack it onto the src in a given element. Because the string will be unique, the browser will interpret the file as something new even if it has the same name. Anybody looking at the file directly or saving it will just see it as its name without some random string or super-long index number. You should be able to do this when your page is generated (no need for extra database entries), as long as the string creation is algorithmic based on some data about the image. As long as the image stays the same, it'll be the same string.

10% popularity Vote Up Vote Down


 

@LarsenBagley505

Why not use timestamps at the end of photo names for fresh photos?

For example:

userone-photo-472934729.jpg
userone-photo-472934730.jpg


And the difference between the two files is one second because of the values of the numbers. If you want instead, you can use dates, but timestamps are more accurate to the second if users constantly update photos.

Just make sure the HTML page is updated as well to reflect the new photo file name and the cache for the HTML should be smaller than the amount of patience the most impatient guest will likely have for your site. This means use no caching for the HTML and tons of caching for the image filenames.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme