title: How do I use Redis in Ruby on Rails to take the dot product of two hashes efficiently
body: <p>I have a data structure like this in the database in the features table called <code>token_vector</code> (a hash):</p>
  
  <pre><code>Feature.find(1).token_vector = { "a" =&gt; 0.1, "b" =&gt; 0.2, "c" =&gt; 0.3 }
  </code></pre>
  
  <p>There are 25 of these features. First, I entered the data into Redis with this in <code>script/console</code>:</p>
  
  <pre><code>REDIS.set(  "feature1",
              "#{ TokenVector.to_json Feature.find(1).token_vector }"
  )
  # ...
  REDIS.set(  "feature25",
              "#{ TokenVector.to_json Feature.find(25).token_vector }"
  )
  </code></pre>
  
  <p><code>TokenVector.to_json</code> converts the hash into JSON format first. The 25 JSON hashes stored in Redis take up about 8 MB.</p>
  
  <p>I have a method, called <code>Analysis#locate</code>. This method takes the dot product between two token_vectors. The dot product for hashes works like this:</p>
  
  <pre><code>hash1 = { "a" =&gt; 1, "b" =&gt; 2, "c" =&gt; 3 }
  hash2 = { "a" =&gt; 4, "b" =&gt; 5, "c" =&gt; 6, "d" =&gt; 7 }
  </code></pre>
  
  <p>Each overlapping key in the hash (a, b, and c in this case, and not d) have their values multiplied pairwise together, then added up. </p>
  
  <p>The value for <code>a</code> in <code>hash1</code> is 1, the value for <code>a</code> in <code>hash2</code> is 4. Multiply these to get <code>1*4 = 4</code>.</p>
  
  <p>The value for <code>b</code> in <code>hash1</code> is 2, the value for <code>b</code> in <code>hash2</code> is 5. Multiply these to get <code>2*5 = 10</code>.</p>
  
  <p>The value for <code>c</code> in <code>hash1</code> is 3, the value for <code>c</code> in <code>hash2</code> is 6. Multiply these to get <code>3*6 = 18</code>.</p>
  
  <p>The value for <code>d</code> in <code>hash1</code> is nonexistent, the value for <code>d</code> in <code>hash2</code> is 7. In this case, set <code>d = 0</code> for the first hash. Multiply these to get <code>0*7 = 0</code>.</p>
  
  <p>Now add up the multiplied values. <code>4 + 10 + 18 + 0 = 32</code>. This is the dot product of hash1 and hash2.</p>
  
  <pre><code>Analysis.locate( hash1, hash2 ) # =&gt; 32
  </code></pre>
  
  <p>I have a method that is often used, <code>Analysis#topicize</code>. This method takes in a parameter, <code>token_vector</code>, which is just a hash, similar to above. <code>Analysis#topicize</code> takes the dot product of <code>token_vector</code> and each of the 25 features' <code>token_vectors</code>, and creates a new vector of those 25 dot products, called <code>feature_vector</code>. A <code>feature_vector</code> is just an array. Here is what the code looks like:</p>
  
  <pre><code>def self.topicize token_vector
  
    feature_vector = FeatureVector.new
  
    feature_vector.push(
      locate( token_vector, TokenVector.from_json( REDIS.get "feature1" ) )
    )
    # ...
    feature_vector.push(
      locate( token_vector, TokenVector.from_json( REDIS.get "feature25" ) )
    )
  
    feature_vector
  
  end
  </code></pre>
  
  <p>As you can see, it takes the dot product of <code>token_vector</code> and each feature's <code>token_vector</code> that I entered into Redis above, and pushes the value into an array.</p>
  
  <p>My problem is, this takes about 18 seconds each time I invoke the method. Am I misusing Redis? I think the problem could be that I shouldn't load Redis data into Ruby. Am I supposed to send Redis the data (<code>token_vector</code>) and write a Redis function to have it do the <code>dot_product</code> function, rather than writing it with Ruby code?</p>