From cba47513888c5cdea0105b334862a50ffa7a0dcb Mon Sep 17 00:00:00 2001 From: Edward Anderson Date: Mon, 14 Sep 2015 12:58:10 -0400 Subject: [PATCH] Cache pages for :zap: lightning speed --- .gitignore | 1 + Gemfile | 1 + Gemfile.lock | 3 +++ README.md | 4 ++++ app/controllers/portfolios_controller.rb | 22 +++++++++++++++++----- config/environments/production.rb | 2 ++ public/page_cache/.gitkeep | 0 7 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 public/page_cache/.gitkeep diff --git a/.gitignore b/.gitignore index c58eb54..78f85b0 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ /log/* /public/system /public/assets +/public/page_cache /tags /tmp/* /config/secrets.yml diff --git a/Gemfile b/Gemfile index 52a42e4..4830263 100644 --- a/Gemfile +++ b/Gemfile @@ -3,6 +3,7 @@ source "https://rubygems.org" ruby "2.2.3" gem "adamantium" +gem "actionpack-page_caching", "~> 1.0" gem "airbrake" gem "autoprefixer-rails" gem "bourbon", "~> 4.2.0" diff --git a/Gemfile.lock b/Gemfile.lock index df488e6..69d4005 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -14,6 +14,8 @@ GEM rack-test (~> 0.6.2) rails-dom-testing (~> 1.0, >= 1.0.5) rails-html-sanitizer (~> 1.0, >= 1.0.2) + actionpack-page_caching (1.0.2) + actionpack (>= 4.0.0, < 5) actionview (4.2.3) activesupport (= 4.2.3) builder (~> 3.1) @@ -312,6 +314,7 @@ PLATFORMS ruby DEPENDENCIES + actionpack-page_caching (~> 1.0) adamantium airbrake autoprefixer-rails diff --git a/README.md b/README.md index f41d070..d5a7229 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,10 @@ After bundler installs gems, these initial setup steps will get the app running: rails server +* (Optional) For fast page loads, [configure your web server][1] to use the page + caches that the app generates in `public/page_cache/`. +[1]: https://github.com/rails/actionpack-page_caching/issues/27#issuecomment-110550147 + Customizing =========== diff --git a/app/controllers/portfolios_controller.rb b/app/controllers/portfolios_controller.rb index 9f43302..aa57f27 100644 --- a/app/controllers/portfolios_controller.rb +++ b/app/controllers/portfolios_controller.rb @@ -1,4 +1,6 @@ class PortfoliosController < ApplicationController + caches_page :index, :show + def index end @@ -7,11 +9,7 @@ def show @portfolio = PortfolioStore.new.find(@github_username) respond_to do |format| format.html { render :loading if @portfolio.nil? } - format.json do - @portfolio = FetchDataWorker.new(@github_username).perform - message = 'reload; portfolio serialization to json not yet supported' - render json: {result: message} - end + format.json { reload_portfolio(@github_username) } end end @@ -19,4 +17,18 @@ def reload @github_username = params[:id] render :loading end + + private + + def reload_portfolio(github_username) + @portfolio = FetchDataWorker.new(github_username).perform + expire_page_cache(github_username) + message = 'reload; portfolio serialization to json not yet supported' + render json: {result: message} + end + + def expire_page_cache(github_username) + expire_page "/#{github_username}" + expire_page '/' if github_username == ENV['PORTFOLIO'] + end end diff --git a/config/environments/production.rb b/config/environments/production.rb index db5560c..b59aaf2 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -28,6 +28,8 @@ # Enable deflate / gzip compression of controller-generated responses config.middleware.use Rack::Deflater + config.action_controller.page_cache_directory = Rails.root.join "public/page_cache" + # Ensure requests are only served from one, canonical host name config.middleware.use Rack::CanonicalHost, ENV.fetch("HOST") diff --git a/public/page_cache/.gitkeep b/public/page_cache/.gitkeep new file mode 100644 index 0000000..e69de29