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

Commit cba4751

Browse files
committed
Cache pages for ⚡ lightning speed
1 parent 772796f commit cba4751

7 files changed

Lines changed: 28 additions & 5 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
/log/*
1111
/public/system
1212
/public/assets
13+
/public/page_cache
1314
/tags
1415
/tmp/*
1516
/config/secrets.yml

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ source "https://rubygems.org"
33
ruby "2.2.3"
44

55
gem "adamantium"
6+
gem "actionpack-page_caching", "~> 1.0"
67
gem "airbrake"
78
gem "autoprefixer-rails"
89
gem "bourbon", "~> 4.2.0"

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ GEM
1414
rack-test (~> 0.6.2)
1515
rails-dom-testing (~> 1.0, >= 1.0.5)
1616
rails-html-sanitizer (~> 1.0, >= 1.0.2)
17+
actionpack-page_caching (1.0.2)
18+
actionpack (>= 4.0.0, < 5)
1719
actionview (4.2.3)
1820
activesupport (= 4.2.3)
1921
builder (~> 3.1)
@@ -312,6 +314,7 @@ PLATFORMS
312314
ruby
313315

314316
DEPENDENCIES
317+
actionpack-page_caching (~> 1.0)
315318
adamantium
316319
airbrake
317320
autoprefixer-rails

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ After bundler installs gems, these initial setup steps will get the app running:
5252

5353
rails server
5454

55+
* (Optional) For fast page loads, [configure your web server][1] to use the page
56+
caches that the app generates in `public/page_cache/`.
57+
[1]: https://github.com/rails/actionpack-page_caching/issues/27#issuecomment-110550147
58+
5559
Customizing
5660
===========
5761

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class PortfoliosController < ApplicationController
2+
caches_page :index, :show
3+
24
def index
35
end
46

@@ -7,16 +9,26 @@ def show
79
@portfolio = PortfolioStore.new.find(@github_username)
810
respond_to do |format|
911
format.html { render :loading if @portfolio.nil? }
10-
format.json do
11-
@portfolio = FetchDataWorker.new(@github_username).perform
12-
message = 'reload; portfolio serialization to json not yet supported'
13-
render json: {result: message}
14-
end
12+
format.json { reload_portfolio(@github_username) }
1513
end
1614
end
1715

1816
def reload
1917
@github_username = params[:id]
2018
render :loading
2119
end
20+
21+
private
22+
23+
def reload_portfolio(github_username)
24+
@portfolio = FetchDataWorker.new(github_username).perform
25+
expire_page_cache(github_username)
26+
message = 'reload; portfolio serialization to json not yet supported'
27+
render json: {result: message}
28+
end
29+
30+
def expire_page_cache(github_username)
31+
expire_page "/#{github_username}"
32+
expire_page '/' if github_username == ENV['PORTFOLIO']
33+
end
2234
end

config/environments/production.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
# Enable deflate / gzip compression of controller-generated responses
2929
config.middleware.use Rack::Deflater
3030

31+
config.action_controller.page_cache_directory = Rails.root.join "public/page_cache"
32+
3133
# Ensure requests are only served from one, canonical host name
3234
config.middleware.use Rack::CanonicalHost, ENV.fetch("HOST")
3335

public/page_cache/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)