Mobile app version of vmapp.org
Login or Join
Kevin317

: Is it worth it to change my entire user images file structure to take advantage of simple browser caching? On one of my mobile sites, I simply store my user's profile images as '1.jpg' in

@Kevin317

Posted in: #Cache #Filenames #Images #Mysql #Php

On one of my mobile sites, I simply store my user's profile images as '1.jpg' in their user folder, and incrementally go from there for any extra pics they upload. This means that whenever they change their profile pic, for example, the file name stays the same.

I've been wanting to take advantage of image caching so that the same old pic doesn't get downloaded over and over again whenever a user's profile is viewed and re-viewed, but at the same time, I want my users' browsers to download the new one if it has changed.

From what I've been reading, it seems that the only way to truly do this is to actually use random file names and keep track of all those file names in the DB, so that you can set a non-expiring cache, while recently-changed pics get pulled again since they have a new file name. The beauty of the way I have them structured up until now, however, is that I can skip the database entirely and access the files directly since their location is predictable.

So my question is, is it worth it for me to change the entire file structure of my site, plus add the DB element, for the benefit of eternal caching and automatic re-downloading upon new upload?

This is a huge undertaking, but if it's deemed worthy, I have no problem moving forward with this drastic change. I just want to make sure this is how the "big boys" do it so that I never have to change the file structure ever again.

Thanks.

10.03% popularity Vote Up Vote Down


Login to follow query

More posts by @Kevin317

2 Comments

Sorted by latest first Latest Oldest Best

 

@Phylliss660

One commonly used solution is to make your image URLs look something like this:
www.example.com/path/to/images/1.jpg?v=123456

Here, /path/to/images/1.jpg is the actual URL path of the image, while ?v=123456 is just a dummy query staring tacked onto the end of the URL. The query string can be anything — a version number, a timestamp, a hash of the image content — as long as you change it whenever the image changes, and keep it the same when it doesn't.

The trick is that the web server, when asked to serve such a URL, will ignore the query string, since the URL in fact points to a static file. But to the user's browser (and to any proxies in between), URLs with different query strings will be completely different, and so any change to the query string forces the browser to reload the file.

Thus, you can configure your web server to send Expires and Cache-Control HTTP headers to allow indefinite caching, safe in the knowledge that you can force a reload by changing the query string. One way to do that, if you're using Apache with mod_expires, is to put an .htaccess file in your image directory with the lines:

ExpiresActive On
ExpiresDefault "access plus 1 year"


This technique is used by many popular websites. For example, if you look at the HTML source of this very page, you'll find that the style sheet for it is loaded from a URL like this:
cdn.sstatic.net/stackoverflow/all.css?v=7cd8ea9d6f1e

Here, the ?v=7cd8ea9d6f1e is a dummy query string just like I described above; you can confirm that by changing it and seeing that it indeed still returns the same file.

10% popularity Vote Up Vote Down


 

@Phylliss660

read about http status 304 Not Modified, you should be able to response to a download request with 304, and by that tell the server to use the cached data, insted of resending it to the browser. and read this question stackoverflow.com/questions/2978496/make-php-page-return-304-not-modified-if-it-hasnt-been-modified

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme