Skip to content

Instantly share code, notes, and snippets.

@tsabat
Created July 5, 2012 03:05
Show Gist options
  • Save tsabat/3050903 to your computer and use it in GitHub Desktop.
Save tsabat/3050903 to your computer and use it in GitHub Desktop.
Generating a bank of unique hashes for short URLs

###The Problem:

How do you create URLs that are short, easy to remember, and unique? Also, how do you store 60M rows so they are easily accessable?

###Getting the data

Get all 5 letter permutations for the letters between a and z as well as A through F.

###The Source

First, you get all the permutations and write them to disk.

#! /bin/ruby

open('/tmp/random.out', 'w') do |f|
  (('a'..'z').to_a + ('A'..'L').to_a).permutation(5) do |p|
    f.puts [p.to_a.join, Time.now.to_s(:db), Time.now.to_s(:db)].join(',')
  end
end

Why disk? Having a flat file would allow you do do interesting things like pipelining to redis, or LOAD DATA calls in mysql.

###Storage

Ok, so we got the data, but how do we store it? First though was redis, so we did this:

###Questions

Why did we stop at F? Franky, we felt like approximately 60 million hashes would get us by for a while. We can always generate more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment