diff --git a/Gemfile b/Gemfile index 49f494c..326f4e0 100644 --- a/Gemfile +++ b/Gemfile @@ -45,6 +45,7 @@ gem 'spring', :group => :development gem 'passenger' gem 'therubyracer' gem 'newrelic_rpm' + gem 'dalli' end #Other gem 'devise' diff --git a/Gemfile.lock b/Gemfile.lock index 1496c23..d17ca7a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -48,6 +48,7 @@ GEM execjs coffee-script-source (1.9.1.1) concurrent-ruby (1.0.0) + dalli (2.7.5) devise (3.4.1) bcrypt (~> 3.0) orm_adapter (~> 0.1) @@ -160,6 +161,7 @@ PLATFORMS DEPENDENCIES cancancan coffee-rails (~> 4.0.0) + dalli devise jbuilder (~> 2.0) jquery-rails diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index 0e15f7f..4d5bb68 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -66,8 +66,8 @@ class TournamentsController < ApplicationController def up_matches - @matches = @tournament.matches.where(mat_id: nil).order('bout_number ASC').limit(10).includes(:wrestlers) - @mats = @tournament.mats.includes(:matches) + @matches = @tournament.cached_matches.select{|m| m.mat_id == nil}.sort_by{|m| m.bout_number} + @mats = @tournament.mats end def index @@ -144,7 +144,7 @@ class TournamentsController < ApplicationController def check_for_matches if @tournament - if @tournament.matches.empty? + if @tournament.cached_matches.empty? redirect_to "/tournaments/#{@tournament.id}/no_matches" end end diff --git a/app/models/mat.rb b/app/models/mat.rb index 4d36353..e601d4c 100644 --- a/app/models/mat.rb +++ b/app/models/mat.rb @@ -18,6 +18,15 @@ class Mat < ActiveRecord::Base end end + def cached_matches + cache_timestamp = self.updated_at + cache_key = "matMatches|#{id}|#{cache_timestamp}" + + Rails.cache.fetch(cache_key, expires_in: 10.minutes) do + self.matches.includes(:wrestlers) + end + end + def assignNextMatch t_matches = tournament.matches.select{|m| m.mat_id == nil} if t_matches.size > 0 @@ -28,7 +37,7 @@ class Mat < ActiveRecord::Base end def unfinishedMatches - matches.select{|m| m.finished == nil}.sort_by{|m| m.bout_number} + cached_matches.select{|m| m.finished == nil}.sort_by{|m| m.bout_number} end end diff --git a/app/models/match.rb b/app/models/match.rb index 64ce59b..70ce5f8 100644 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -1,7 +1,7 @@ class Match < ActiveRecord::Base - belongs_to :tournament - belongs_to :weight - belongs_to :mat + belongs_to :tournament, touch: true + belongs_to :weight, touch: true + belongs_to :mat, touch: true has_many :wrestlers, :through => :weight after_save do diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 082c9f8..63daa85 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -9,7 +9,14 @@ class Tournament < ActiveRecord::Base has_many :wrestlers, through: :weights has_many :matches, dependent: :destroy + def cached_matches + cache_timestamp = self.updated_at + cache_key = "tournamentMatches|#{id}|#{cache_timestamp}" + Rails.cache.fetch(cache_key, expires_in: 10.minutes) do + self.matches.includes(:wrestlers) + end + end def tournament_types ["Pool to bracket"] diff --git a/config/environments/production.rb b/config/environments/production.rb index 543b8ff..196e5ec 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -54,6 +54,14 @@ Wrestling::Application.configure do # Use a different cache store in production. # config.cache_store = :mem_cache_store + config.cache_store = :dalli_store, + (ENV["MEMCACHIER_SERVERS"] || "").split(","), + {:username => ENV["MEMCACHIER_USERNAME"], + :password => ENV["MEMCACHIER_PASSWORD"], + :failover => true, + :socket_timeout => 1.5, + :socket_failure_delay => 0.2 + } # Enable serving of images, stylesheets, and JavaScripts from an asset server. # config.action_controller.asset_host = "http://assets.example.com"