簡體   English   中英

如何在軌道上的ruby中啟用背景圖像的緩存?

[英]How to enable caching of background images in ruby on rails?

如何在用戶的瀏覽器中(強制)緩存背景圖像。 設置1周或1個月的到期時間會很棒。

PS:我有一個頁面,顯示基於類別的列表。 每個類別都有自己的背景圖片,我想緩存那些背景圖片。每個圖片大約20-30kb,我有20個類別。

Varnish是一個解決方案, 這里的寶石可以幫助您實現它

安裝此gem需要ruby 1.9

基本安裝

sudo gem install lacquer  
rails generate lacquer:install

配置/初始化/ lacquer.rb

Lacquer.configure do |config|
  # Globally enable/disable cache
  config.enable_cache = true

  # Unless overridden in a controller or action, the default will be used
  config.default_ttl = 1.week

  # Can be :none, :delayed_job, :resque
  config.job_backend = :none

  # Array of Varnish servers to manage
  config.varnish_servers << {
    :host => "0.0.0.0", :port => 6082 # if you have authentication enabled, add :secret => "your secret"
  }

  # Number of retries
  config.retries = 5

  # config handler (optional, if you use Hoptoad or another error tracking service)
  config.command_error_handler = lambda { |s| HoptoadNotifier.notify(s) }

  ### Varnish - 2.x  /  3.x  .. VCL-Changes
  ### https://www.varnish-cache.org/docs/trunk/installation/upgrade.html

  # => Purge Command  ( "url.purge" for Varnish 2.x .. "ban.url" for Varnish 3.x )
  # => purges are now called bans in Varnish 3.x .. purge() and purge_url() are now respectively ban() and ban_url()
  config.purge_command = "ban.url"

  # => VCL_Fetch Pass Command  ( "pass" for Varnish 2.x .. "hit_for_pass" for Varnish 3.x )
  # => pass in vcl_fetch renamed to hit_for_pass in Varnish 3.x   
  config.pass_command = "pass"
end

應用程序/控制器/ application_controller.rb

class ApplicationController < ActionController::Base
  include Lacquer::CacheUtils
end

配置/ varnishd.yml

development:
  listen: localhost:3001
  telnet: localhost:6082
  sbin_path: /usr/local/sbin
  storage: "file,#{Rails.root}/log/varnishd.#{Rails.env}.cache,100MB"

test:
  listen: localhost:3002
  telnet: localhost:6083
  sbin_path: /usr/local/sbin
  storage: "file,#{Rails.root}/log/varnishd.#{Rails.env}.cache,100MB"

production:
  listen: :80
  telnet: localhost:6082
  sbin_path: /usr/local/sbin
  storage: "file,#{Rails.root}/log/varnishd.#{Rails.env}.cache,100MB"
  params:
    overflow_max: 2000          # for Varnish 2.x ... use "queue_max: 2000" for Varnish 3.x
    thread_pool_add_delay: 2
    thread_pools: 4             # <Number of cpu cores>
    thread_pool_min: 200        # <800/number of cpu cores>
    thread_pool_max: 4000

如果清漆僅緩存應用程序的某些URL,Lacquer :: CacheControl將很有幫助。

配置/初始化/ caches.rb

require "lacquer/cache_control"

Lacquer.cache_control.configure do |config|
  config.register :static,              :url => "^/images",                                           
                                        :expires_in => "365d"

  config.register :static,              :url => "^/stylesheets",
                                        :expires_in => "365d"

  config.register :static,              :url => "^/javascripts",                                       
                                        :expires_in => "365d"

  config.register :class_section,       :url => "^(/[a-z]{2})?/(info_screens|class_sections)/%s.*$",   
                                        :args => "[0-9]+", 
                                        :expires_in => "1m"

  config.register :open_scoring,        :url => "^(/[a-z]{2})?/class_sections/%s/open_scoring.*$",
                                        :args => "[0-9]+",
                                        :expires_in => "1m"

end

在掃地機中我們可以做這樣的事情

class_section = ClassSection.find(1)
Lacquer.cache_control.purge(:open_scoring, class_section)

這將清除“ ^(/ [az] {2})?/ class_sections / 1 / open_scoring。* $”(/sv/class_sections/1/open_scoring.js,/sv/class_sections/1/open_scoring.html)

當使用rake任務啟動varnishd時,varnish.vcl是預處理的

rake lacquer:varnishd:start

配置/ varnish.vcl.erb

sub vcl_recv {
  # Lookup requests that we know should be cached
  if (<%= Lacquer.cache_control.to_vcl_conditions %>) {    
    # Clear cookie and authorization headers, set grace time, lookup in the cache
    unset req.http.Cookie;
    unset req.http.Authorization;
    return(lookup);
  }

  # Generates
  #
  # if(req.url ~ "^/images" || 
  #    req.url ~ "^/stylesheets" || 
  #    req.url ~ "^/javascripts" || 
  #    req.url ~ "^(/[a-z]{2})?/(info_screens|class_sections)/[0-9]+.*$" || 
  #    req.url ~ "^(/[a-z]{2})?/class_sections/[0-9]+/open_scoring.*$") {
  #    unset req.http.Cookie;
  #    unset req.http.Authorization;
  #    return(lookup);         
  # }
}

sub vcl_fetch {
  <%= Lacquer.cache_control.to_vcl_override_ttl_urls %>

  # Generates
  #
  # if(req.url ~ "^/images" || req.url ~ "^/stylesheets" || req.url ~ "^/javascripts") {
  #   unset beresp.http.Set-Cookie;
  #   set beresp.ttl = 365d;
  #   return(deliver);
  # }
  #
  # if(req.url ~ "^(/[a-z]{2})?/(info_screens|class_sections)/[0-9]+.*$" || 
  #   req.url ~ "^(/[a-z]{2})?/class_sections/[0-9]+/open_scoring.*$") {
  #   unset beresp.http.Set-Cookie;
  #   set beresp.ttl = 1m;
  #   return(deliver);
  # }
}

這使得執行緩存變得更加簡單,它只在一個地方設置,清除它或只是讓它過期。

用法為控制器設置自定義ttl:

  before_filter { |controller| controller.set_cache_ttl(15.minutes) }

清除緩存:

class Posts < ApplicationController
  after_filter :clear_cache, :only => [ :create, :update, :destroy ]

private

  def clear_cache
    clear_cache_for(
      root_path,
      posts_path,
      post_path(@post))
  end
end

通過以下rake任務對控件進行清漆

rake lacquer:varnishd:start
rake lacquer:varnishd:stop
rake lacquer:varnishd:restart
rake lacquer:varnishd:status
rake lacquer:varnishd:global_purge

陷阱大多數操作的默認TTL設置為0,因為在大多數情況下,您可能想相當清楚地知道清漆會緩存哪些頁面。 默認緩存標頭通常是:

高速緩存控制:max-age = 0,無高速緩存,專用這對於常規的控制器操作非常有用,因為您不想對其進行高速緩存。 如果將某個操作的TTL設置為0,則不會與默認標頭混淆。

這里的關鍵是緩存頁面剝離cookie,因此如果您的應用程序依賴於會話並使用真實性令牌,則用戶將需要在表單操作起作用之前設置會話cookie。 在此處將默認TTL設置為0將確保這些會話cookie不會中斷。

因此,設置可緩存操作所需要做的就是上面的前置過濾器。

在我工作的地方,我們用清漆做這類東西。

如果您正在使用Rails 3.1的資產管道,則Rails已經為您添加了必要的緩存控制標頭。 這意味着客戶端會緩存圖像。 Rails還確保在重新部署后文件名發生更改,因此訪問者不會看到仍在其緩存中的舊圖像。

另見: http//guides.rubyonrails.org/asset_pipeline.html#live-compilation

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM