Commit memcache work. memcache is only enabled in production, and currently only...
authorJohn Dong <jdong@mit.edu>
Thu, 30 Aug 2007 23:12:35 +0000 (19:12 -0400)
committerJohn Dong <jdong@mit.edu>
Thu, 30 Aug 2007 23:12:35 +0000 (19:12 -0400)
app/models/election.rb
config/environment.rb
config/environments/development.rb
config/environments/production.rb
config/environments/test.rb

index 9c6337fdcf56ff78d7ee1d63a04ba1bfedf23bf0..ce7a0bceaff624a73231b6892b6743b0a36bd5f7 100644 (file)
@@ -88,8 +88,28 @@ class Election < ActiveRecord::Base
     longdesc.length > 0 ? longdesc : nil 
   end
   
-  #Calculate Election Results
+  #Calculate results if not in memcache
   def results
+    # Assignment is intentional
+    if Cache and c = Cache.get("election_results:#{id}:#{self.votes.length}")
+      @plurality_result = c['plurality']
+      @approval_result = c['approval']
+      @condorcet_result = c['condorcet']
+      @ssd_result = c['ssd']
+      @borda_result = c['borda']
+      return c
+    elsif Cache
+      # memcache is available, but missed.
+      results = self.results!
+      Cache.set("election_results:#{id}:#{self.votes.length}", results)
+      return results
+    else
+      return self.results!
+    end
+  end
+
+  #Always Calculate Election Results
+  def results!
     # initalize the tallies to empty arrays
     preference_tally = Array.new
     plurality_tally = Array.new
@@ -129,7 +149,6 @@ class Election < ActiveRecord::Base
     
     names
   end
-  
 end
 
 
index 80f19b46ee25983f4511ac36c4142d8daf895857..2a7700951185f8eb71204f1210948ed156ec81a3 100644 (file)
@@ -65,7 +65,6 @@ require 'uniq_token'
 require 'randarray'
 require 'gruff'
 require 'sparklines'
-
 require 'rubyvote'
 ELECTION_TYPES = %w(ssd plurality approval condorcet borda)
 
index 12a3e04e5fb70df7ae8d679999e2ee76cd50fe20..198c2bdafe91f92df9e624308df375b3dec695b2 100644 (file)
@@ -1,5 +1,6 @@
 # Settings specified here will take precedence over those in config/environment.rb
-
+#disable memcache
+Cache=nil
 # In the development environment your application's code is reloaded on
 # every request.  This slows down response time but is perfect for development
 # since you don't have to restart the webserver when you make code changes.
index cb295b83f17285331882dfdc0eebddaecf32f773..3192ef53fe394680cdc5785a3605c9ccfc1656ca 100644 (file)
@@ -1,5 +1,8 @@
 # Settings specified here will take precedence over those in config/environment.rb
-
+#
+#Enable memcache
+require('memcache')
+Cache=MemCache.new('localhost', :compression => true)
 # The production environment is meant for finished, "live" apps.
 # Code is not reloaded between requests
 config.cache_classes = true
index 7c2f617db2b1810fd86d3c15418b607403b991ca..a1a2608bbfd3ef55531c08d37403266a0d7d463f 100644 (file)
@@ -1,5 +1,6 @@
 # Settings specified here will take precedence over those in config/environment.rb
-
+#disable memcache
+Cache=nil
 # The test environment is used exclusively to run your application's
 # test suite.  You never need to work with it otherwise.  Remember that
 # your test database is "scratch space" for the test suite and is wiped

Benjamin Mako Hill || Want to submit a patch?