Skip to content

How to: Make a fast lookup able storage directory structure

clyfe edited this page Jun 4, 2011 · 6 revisions

If you have many files dumped in the same directory disk lookups might be slow.
That is why it's a good idea to partition your files into directories like in the following example:

class FileUploader < CarrierWave::Uploader::Base
  storage :file
  
  def store_dir
    "documents/#{model.file_name[0, 2]}/#{model.id}"
  end  
end

In this example we use the first two letters of the filename to create directoryes that will partition our files.
This way we can have nice dictionary-like lookups.

See also: http://stackoverflow.com/questions/446358/storing-a-large-number-of-images

Clone this wiki locally