告诉 Rails 缓存静态文件组

发布于 2024-10-02 01:50:55 字数 462 浏览 2 评论 0原文

是否可以在无需访问网站的情况下告诉rails一起执行application.html.erb页面中所有文件的缓存?

我的页面中有以下代码:

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

当网站首次在生产模式下加载时,这会将所有内容组合到一个 _cached.css 文件中。但是,我希望在网站初始化后重新生成该文件。

因此,步骤是

  • 启动 Rails 应用程序
  • ,删除现有的 _cached.css 文件,
  • 告诉 Rails 根据 application.html.erb 文件网站中的代码重新创建该文件
  • ...

有关如何执行此操作的任何想法吗?可以作为 rake 命令还是其他命令?

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

  • start the rails app
  • remove the existing _cached.css file
  • tell rails to recreate the file based on the code within the application.html.erb file
  • website is live...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生生不灭 2024-10-09 01:50:55

在设置了在网站加载后访问网站文件的 rake 任务后,这就可以了。

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

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
热鲨 2024-10-09 01:50:55

将其放入 lib/tasks/ 文件中:

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

然后将其作为部署脚本或 capistrano 配方的一部分进行调用:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文