matsko
matsko

Reputation: 22183

Telling Rails to Cache a Static Group of Files

Is it possible without having to access the website to tell rails to perform a cache of all the files within the application.html.erb page together?

I have the following code in my page:

<%= stylesheet_link_tag 'reset','forms','layout','common','pages', :cache => '_cached' %>

This will combine everything together into a _cached.css file when the website is loaded for the first time in production mode. However, I would like for the file to be regenerated once the website is initialized.

So the steps would be

Any ideas on how todo this? Can be down as a rake command or something?

Upvotes: 0

Views: 170

Answers (2)

matsko
matsko

Reputation: 22183

After setting up a rake task that accesses the website files once the website is loaded, this works.

namespace :assets do

  desc "Removing existing cached files"
  task :clear => :environment do
    FileUtils.rm(Dir['public/javascripts/_cached.js']) 
    FileUtils.rm(Dir['public/javascripts/_ie6.js']) 
    FileUtils.rm(Dir['public/stylesheets/_cached.css'])
  end

  desc "Recreate the javascripts/stylesheets cache."
  task :generate => [:environment, :clear] do
    puts "Recreate the javascripts/stylesheets cache"
    ActionController::Base.perform_caching = true
    app = ActionController::Integration::Session.new(ActionController::Dispatcher.new)
    app.get '/'
  end

end

Upvotes: 1

DGM
DGM

Reputation: 26979

Put this in a lib/tasks/ file:

namespace :tmp do
  namespace :assets do 
   desc "Clears javascripts/cache and stylesheets/cache"
     task :clear => :environment do      
       FileUtils.rm(Dir['public/javascripts/cache.js'])
       FileUtils.rm(Dir['public/stylesheets/cache.css'])
     end
   end
end

and then call it as part of your deployment script or capistrano recipe:

namespace :deploy do
  task :start do ; end
  task :stop do ; end
  task :restart, :roles => :app, :except => { :no_release => true } do
    run "cd #{current_path} && /usr/bin/env rake tmp:assets:clear RAILS_ENV=#{stage}"
    run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
  end
end

Upvotes: 0

Related Questions