Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
/log/*
/public/system
/public/assets
/public/page_cache
/tags
/tmp/*
/config/secrets.yml
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -312,6 +314,7 @@ PLATFORMS
ruby

DEPENDENCIES
actionpack-page_caching (~> 1.0)
adamantium
airbrake
autoprefixer-rails
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
===========

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

def index
end

Expand All @@ -7,16 +9,26 @@ 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

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
2 changes: 2 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")

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

0 comments on commit cba4751

Please sign in to comment.