From 2658beb48f9c3310e84a2738b7c8293d4fc3dbd4 Mon Sep 17 00:00:00 2001 From: John Dong Date: Thu, 30 Aug 2007 19:12:35 -0400 Subject: [PATCH] Commit memcache work. memcache is only enabled in production, and currently only thing cached are election results. At least 5-fold render speed improvement... --- app/models/election.rb | 23 +++++++++++++++++++++-- config/environment.rb | 1 - config/environments/development.rb | 3 ++- config/environments/production.rb | 5 ++++- config/environments/test.rb | 3 ++- 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/app/models/election.rb b/app/models/election.rb index 9c6337f..ce7a0bc 100644 --- a/app/models/election.rb +++ b/app/models/election.rb @@ -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 diff --git a/config/environment.rb b/config/environment.rb index 80f19b4..2a77009 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -65,7 +65,6 @@ require 'uniq_token' require 'randarray' require 'gruff' require 'sparklines' - require 'rubyvote' ELECTION_TYPES = %w(ssd plurality approval condorcet borda) diff --git a/config/environments/development.rb b/config/environments/development.rb index 12a3e04..198c2bd 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -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. diff --git a/config/environments/production.rb b/config/environments/production.rb index cb295b8..3192ef5 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -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 diff --git a/config/environments/test.rb b/config/environments/test.rb index 7c2f617..a1a2608 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -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 -- 2.39.5