title: Caching situation for images stored in a database
body: <p>The Django recommendation for dealing with user uploads is to store them on the filesystem and store the filesystem path in a database column.  This works, but presents some problems I do not want to deal with:</p>
  
  <ol>
  <li><p>No transactions</p></li>
  <li><p>No simple way to keep the filesystem and database in sync</p></li>
  <li><p>Complicates backups since data is stored in 2 places</p></li>
  </ol>
  
  <p>My solution is to store the image as a base64 encoded string in a text column (<a href="https://djangosnippets.org/snippets/1669/" rel="nofollow">https://djangosnippets.org/snippets/1669/</a>).  This requires more space, but makes replication dead simple.</p>
  
  <p>The concern with this approach is performance.  Hitting the database for every image request is not desirable.  I need some kind of server-side caching system together with reasonable caching headers.  For example, if someone requests "/media/documents/earth.jpg", the cache should be consulted first and if the file is not found there the database should be hit.</p>
  
  <p>Questions:</p>
  
  <ol>
  <li><p>What is a good cache tool for my purpose?</p></li>
  <li><p>Given these requirements is it required that every image request goes through my Django application?  Or is there a caching tool that I can use to prevent this.  I have certain files that can be accessed only by certain people.  For these I assume the request must go through the application since there would be no other way to check for authorizaton.</p></li>
  <li><p>If this tool caches the files to the filesystem, then are hashed directories enough to mitigate the problem of having too many files in one directory?  For example, a hashed directory path for elephant.gif could be /e/el/elephant.gif.</p></li>
  </ol>