Skip to content

Commit

Permalink
Cache pages for ⚡ lightning speed
Browse files Browse the repository at this point in the history
  • Loading branch information
nilbus committed Sep 14, 2015
1 parent 772796f commit cba4751
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -10,6 +10,7 @@
/log/* /log/*
/public/system /public/system
/public/assets /public/assets
/public/page_cache
/tags /tags
/tmp/* /tmp/*
/config/secrets.yml /config/secrets.yml
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -3,6 +3,7 @@ source "https://rubygems.org"
ruby "2.2.3" ruby "2.2.3"


gem "adamantium" gem "adamantium"
gem "actionpack-page_caching", "~> 1.0"
gem "airbrake" gem "airbrake"
gem "autoprefixer-rails" gem "autoprefixer-rails"
gem "bourbon", "~> 4.2.0" gem "bourbon", "~> 4.2.0"
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Expand Up @@ -14,6 +14,8 @@ GEM
rack-test (~> 0.6.2) rack-test (~> 0.6.2)
rails-dom-testing (~> 1.0, >= 1.0.5) rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.2) rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionpack-page_caching (1.0.2)
actionpack (>= 4.0.0, < 5)
actionview (4.2.3) actionview (4.2.3)
activesupport (= 4.2.3) activesupport (= 4.2.3)
builder (~> 3.1) builder (~> 3.1)
Expand Down Expand Up @@ -312,6 +314,7 @@ PLATFORMS
ruby ruby


DEPENDENCIES DEPENDENCIES
actionpack-page_caching (~> 1.0)
adamantium adamantium
airbrake airbrake
autoprefixer-rails autoprefixer-rails
Expand Down
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -52,6 +52,10 @@ After bundler installs gems, these initial setup steps will get the app running:


rails server 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 Customizing
=========== ===========


Expand Down
22 changes: 17 additions & 5 deletions app/controllers/portfolios_controller.rb
@@ -1,4 +1,6 @@
class PortfoliosController < ApplicationController class PortfoliosController < ApplicationController
caches_page :index, :show

def index def index
end end


Expand All @@ -7,16 +9,26 @@ def show
@portfolio = PortfolioStore.new.find(@github_username) @portfolio = PortfolioStore.new.find(@github_username)
respond_to do |format| respond_to do |format|
format.html { render :loading if @portfolio.nil? } format.html { render :loading if @portfolio.nil? }
format.json do format.json { reload_portfolio(@github_username) }
@portfolio = FetchDataWorker.new(@github_username).perform
message = 'reload; portfolio serialization to json not yet supported'
render json: {result: message}
end
end end
end end


def reload def reload
@github_username = params[:id] @github_username = params[:id]
render :loading render :loading
end 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 end
2 changes: 2 additions & 0 deletions config/environments/production.rb
Expand Up @@ -28,6 +28,8 @@
# Enable deflate / gzip compression of controller-generated responses # Enable deflate / gzip compression of controller-generated responses
config.middleware.use Rack::Deflater 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 # Ensure requests are only served from one, canonical host name
config.middleware.use Rack::CanonicalHost, ENV.fetch("HOST") config.middleware.use Rack::CanonicalHost, ENV.fetch("HOST")


Expand Down
Empty file added public/page_cache/.gitkeep
Empty file.

0 comments on commit cba4751

Please sign in to comment.