Skip to content

Instantly share code, notes, and snippets.

@JangoSteve
Created November 17, 2011 03:32
Show Gist options
  • Select an option

  • Save JangoSteve/1372299 to your computer and use it in GitHub Desktop.

Select an option

Save JangoSteve/1372299 to your computer and use it in GitHub Desktop.

Revisions

  1. JangoSteve revised this gist Nov 17, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cache.rake
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ namespace :pages do
    task :cache => :environment do

    # First, let's instantiate a new session so we can `get` our paths,
    #just like if we were writing an integration test
    # just like if we were writing an integration test
    app = ActionDispatch::Integration::Session.new(Rails.application)

    # Now, we'll loop through each path we want to cache
  2. JangoSteve revised this gist Nov 17, 2011. No changes.
  3. JangoSteve revised this gist Nov 17, 2011. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion cache.rake
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,8 @@ namespace :pages do
    desc "Cache ALL the pages!"
    task :cache => :environment do

    # First, let's instantiate a new session so we can `get` our paths, just like if we were writing an integration test
    # First, let's instantiate a new session so we can `get` our paths,
    #just like if we were writing an integration test
    app = ActionDispatch::Integration::Session.new(Rails.application)

    # Now, we'll loop through each path we want to cache
  4. JangoSteve revised this gist Nov 17, 2011. 1 changed file with 13 additions and 4 deletions.
    17 changes: 13 additions & 4 deletions cache.rake
    Original file line number Diff line number Diff line change
    @@ -14,12 +14,21 @@
    namespace :pages do
    desc "Cache ALL the pages!"
    task :cache => :environment do

    # First, let's instantiate a new session so we can `get` our paths, just like if we were writing an integration test
    app = ActionDispatch::Integration::Session.new(Rails.application)
    %w( / /about /contact /products /products/cool_thing /products/other_cool_thing ).each do |path|

    # Now, we'll loop through each path we want to cache
    %w( /index /about /contact /products /products/cool_thing /products/other_cool_thing ).each do |path|

    # Get the path
    app.get path
    # Optionally, save all html pages in the same flat directory (/public/cache), by uncommenting this:
    # path = "/cache/#{path.gsub('/', '_')[1,path.length]}"
    ActionController::Base.cache_page(app.response.body, path)

    # Let's figure out the file path/name to which we're saving the file
    file_path = "/cache/#{path.gsub('/', '_')[1,path.length]}"

    # And now let's cache the response body HTML, using the ActionController's
    ApplicationController.cache_page(app.response.body, file_path)
    end
    end
    end
  5. JangoSteve revised this gist Nov 17, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cache.rake
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ namespace :pages do
    app.get path
    # Optionally, save all html pages in the same flat directory (/public/cache), by uncommenting this:
    # path = "/cache/#{path.gsub('/', '_')[1,path.length]}"
    ApplicationController.cache_page(app.response.body, path)
    ActionController::Base.cache_page(app.response.body, path)
    end
    end
    end
  6. JangoSteve created this gist Nov 17, 2011.
    25 changes: 25 additions & 0 deletions cache.rake
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    # See rails source:
    # https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/caching/pages.rb
    #
    # Turn on caching in development, by changing this line to true in config/environments/development.rb:
    #
    # config.action_controller.perform_caching = true
    #
    # Then run:
    #
    # bundle exec rake pages:cache
    #
    # All cached pages will be in /public/path_name/xxxx.html

    namespace :pages do
    desc "Cache ALL the pages!"
    task :cache => :environment do
    app = ActionDispatch::Integration::Session.new(Rails.application)
    %w( / /about /contact /products /products/cool_thing /products/other_cool_thing ).each do |path|
    app.get path
    # Optionally, save all html pages in the same flat directory (/public/cache), by uncommenting this:
    # path = "/cache/#{path.gsub('/', '_')[1,path.length]}"
    ApplicationController.cache_page(app.response.body, path)
    end
    end
    end