lacquer 0.4.0 → 0.6.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +4 -20
  3. data/.travis.yml +3 -0
  4. data/Gemfile +2 -7
  5. data/README.md +260 -0
  6. data/Rakefile +11 -33
  7. data/init.rb +1 -1
  8. data/lacquer.gemspec +19 -69
  9. data/lib/generators/lacquer/install_generator.rb +5 -1
  10. data/lib/generators/lacquer/templates/initializer.rb +15 -3
  11. data/lib/generators/lacquer/templates/{varnish.sample.vcl → varnish.vcl.erb} +27 -27
  12. data/lib/generators/lacquer/templates/varnishd.yml +31 -0
  13. data/lib/lacquer/cache_control.rb +75 -0
  14. data/lib/lacquer/cache_utils.rb +14 -12
  15. data/lib/lacquer/capistrano/v2/hooks.rb +21 -0
  16. data/lib/lacquer/capistrano/v3/tasks/lacquer.rake +24 -0
  17. data/lib/lacquer/capistrano.rb +7 -0
  18. data/lib/lacquer/configuration.rb +12 -0
  19. data/lib/lacquer/delayed_job_job.rb +2 -2
  20. data/lib/lacquer/railtie.rb +9 -0
  21. data/lib/lacquer/recipes.rb +62 -0
  22. data/lib/lacquer/resque_job.rb +2 -2
  23. data/lib/lacquer/sidekiq_worker.rb +11 -0
  24. data/lib/lacquer/tasks.rb +48 -0
  25. data/lib/lacquer/varnish.rb +35 -4
  26. data/lib/lacquer/varnishd.rb +152 -0
  27. data/lib/lacquer/version.rb +3 -0
  28. data/lib/lacquer.rb +9 -0
  29. data/rails/init.rb +1 -1
  30. data/recipes/lacquer.rb +1 -0
  31. data/spec/config/generate.vcl.erb +15 -0
  32. data/spec/config/varnish.vcl +15 -0
  33. data/spec/config/varnishd.yml +12 -0
  34. data/spec/lacquer/cache_control_spec.rb +74 -0
  35. data/spec/lacquer/cache_utils_spec.rb +20 -8
  36. data/spec/lacquer/delayed_job_job_spec.rb +4 -2
  37. data/spec/lacquer/resque_job_spec.rb +4 -2
  38. data/spec/lacquer/sidekiq_worker_spec.rb +15 -0
  39. data/spec/lacquer/varnish_spec.rb +62 -18
  40. data/spec/lacquer/varnishd_spec.rb +122 -0
  41. data/spec/spec_helper.rb +13 -1
  42. data/tasks/lacquer.rake +1 -0
  43. metadata +135 -66
  44. data/.bundle/config +0 -2
  45. data/Gemfile.lock +0 -39
  46. data/README.rdoc +0 -83
  47. data/VERSION +0 -1
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e706da67a272b82cee6c8da31abe6c8d9e78358c
4
+ data.tar.gz: 60c69ff19d95e669f1f5201b0ae78a6e69480501
5
+ SHA512:
6
+ metadata.gz: 1ca0ce46286b668c6617e8d1a619c56abc583ad649d4f321927e2247cf16b79c50e8be20fac4526afd5ca56d670ef0361b3832edc2d43d82e5c2b42eee08d4ca
7
+ data.tar.gz: f40f3deffabd878d515c46538c6012179c386c143b5b034767c873c652abc12e1751b0c15707818af0deec3d6ed1734a6f025af2eacce6d980a993f94de7a208
data/.gitignore CHANGED
@@ -1,21 +1,5 @@
1
- ## MAC OS
1
+ *.gem
2
+ .bundle
2
3
  .DS_Store
3
-
4
- ## TEXTMATE
5
- *.tmproj
6
- tmtags
7
-
8
- ## EMACS
9
- *~
10
- \#*
11
- .\#*
12
-
13
- ## VIM
14
- *.swp
15
-
16
- ## PROJECT::GENERAL
17
- coverage
18
- rdoc
19
- pkg
20
- doc
21
- .yardoc
4
+ Gemfile.lock
5
+ pkg/*
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ rvm:
2
+ - 1.9.2
3
+ - 1.9.3
data/Gemfile CHANGED
@@ -1,8 +1,3 @@
1
- source :gemcutter
1
+ source "http://rubygems.org"
2
2
 
3
- gem "activesupport", "3.0.1"
4
- gem "i18n", "0.4.1"
5
- gem "jeweler"
6
- gem "rake"
7
- gem "rspec", "2.0.0"
8
- gem "yard"
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,260 @@
1
+ ![http://travis-ci.org/russ/lacquer](https://secure.travis-ci.org/russ/lacquer.png?branch=master)
2
+
3
+ # Lacquer
4
+
5
+ Rails drop in for [Varnish](https://www.varnish-cache.org/) support.
6
+
7
+ ## Install
8
+ This gem requires ruby 1.9+
9
+
10
+ ```text
11
+ gem install lacquer
12
+ rails generate lacquer:install
13
+ ```
14
+
15
+ **config/initializers/lacquer.rb**
16
+
17
+ ```ruby
18
+ Lacquer.configure do |config|
19
+ # Globally enable/disable cache
20
+ config.enable_cache = true
21
+
22
+ # Unless overridden in a controller or action, the default will be used
23
+ config.default_ttl = 1.week
24
+
25
+ # Can be :none, :delayed_job, :resque, :sidekiq
26
+ config.job_backend = :none
27
+
28
+ # Array of Varnish servers to manage
29
+ config.varnish_servers << {
30
+ :host => "0.0.0.0", :port => 6082 # if you have authentication enabled, add :secret => "your secret"
31
+ }
32
+
33
+ # Number of retries
34
+ config.retries = 5
35
+
36
+ # config handler (optional, if you use Hoptoad or another error tracking service)
37
+ config.command_error_handler = lambda { |s| HoptoadNotifier.notify(s) }
38
+
39
+
40
+ ### Varnish - 2.x / 3.x .. VCL-Changes
41
+ ### https://www.varnish-cache.org/docs/trunk/installation/upgrade.html
42
+
43
+ # => Purge Command ( "url.purge" for Varnish 2.x .. "ban.url" for Varnish 3.x )
44
+ # => purges are now called bans in Varnish 3.x .. purge() and purge_url() are now respectively ban() and ban_url()
45
+ config.purge_command = "ban.url"
46
+
47
+ # => VCL_Fetch Pass Command ( "pass" for Varnish 2.x .. "hit_for_pass" for Varnish 3.x )
48
+ # => pass in vcl_fetch renamed to hit_for_pass in Varnish 3.x
49
+ config.pass_command = "pass"
50
+ end
51
+ ```
52
+
53
+ **app/controllers/application_controller.rb**
54
+
55
+ ```ruby
56
+ class ApplicationController < ActionController::Base
57
+ include Lacquer::CacheUtils
58
+ end
59
+ ```
60
+
61
+ **config/varnishd.yml**
62
+ ```yaml
63
+ development:
64
+ listen: localhost:3001
65
+ telnet: localhost:6082
66
+ sbin_path: /usr/local/sbin # path to varnishd
67
+ bin_path: /usr/local/bin # path to varnishadm
68
+ storage: "file,#{Rails.root}/log/varnishd.#{Rails.env}.cache,100MB"
69
+
70
+ test:
71
+ listen: localhost:3002
72
+ telnet: localhost:6083
73
+ sbin_path: /usr/local/sbin
74
+ bin_path: /usr/local/bin
75
+ storage: "file,#{Rails.root}/log/varnishd.#{Rails.env}.cache,100MB"
76
+
77
+ production:
78
+ listen: :80
79
+ telnet: localhost:6082
80
+ sbin_path: /usr/local/sbin
81
+ bin_path: /usr/local/bin
82
+ storage: "file,#{Rails.root}/log/varnishd.#{Rails.env}.cache,100MB"
83
+ params:
84
+ overflow_max: 2000 # for Varnish 2.x ... use "queue_max: 2000" for Varnish 3.x
85
+ thread_pool_add_delay: 2
86
+ thread_pools: 4 # <Number of cpu cores>
87
+ thread_pool_min: 200 # <800/number of cpu cores>
88
+ thread_pool_max: 4000
89
+ ```
90
+
91
+ If only some urls of the application should be cached by varnish, Lacquer::CacheControl will be helpful.
92
+
93
+ **config/initializers/caches.rb**
94
+ ```ruby
95
+ require "lacquer/cache_control"
96
+
97
+ Lacquer.cache_control.configure do |config|
98
+ config.register :static, :url => "^/images",
99
+ :expires_in => "365d"
100
+
101
+ config.register :static, :url => "^/stylesheets",
102
+ :expires_in => "365d"
103
+
104
+ config.register :static, :url => "^/javascripts",
105
+ :expires_in => "365d"
106
+
107
+ config.register :class_section, :url => "^(/[a-z]{2})?/(info_screens|class_sections)/%s.*$",
108
+ :args => "[0-9]+",
109
+ :expires_in => "1m"
110
+
111
+ config.register :open_scoring, :url => "^(/[a-z]{2})?/class_sections/%s/open_scoring.*$",
112
+ :args => "[0-9]+",
113
+ :expires_in => "1m"
114
+
115
+ end
116
+ ```
117
+
118
+ In the sweeper we can do something like this
119
+
120
+ ```ruby
121
+ class_section = ClassSection.find(1)
122
+ Lacquer.cache_control.purge(:open_scoring, class_section)
123
+ ```
124
+
125
+ This will purge `"^(/[a-z]{2})?/class_sections/1/open_scoring.*$" (/sv/class_sections/1/open_scoring.js, /sv/class_sections/1/open_scoring.html)`
126
+
127
+ The varnish.vcl is preprocssed when starting varnishd with the rake tasks
128
+
129
+ `rake lacquer:varnishd:start`
130
+
131
+ **config/varnish.vcl.erb**
132
+
133
+ ```vcl
134
+ sub vcl_recv {
135
+ # Lookup requests that we know should be cached
136
+ if (<%= Lacquer.cache_control.to_vcl_conditions %>) {
137
+ # Clear cookie and authorization headers, set grace time, lookup in the cache
138
+ unset req.http.Cookie;
139
+ unset req.http.Authorization;
140
+ return(lookup);
141
+ }
142
+
143
+ # Generates
144
+ #
145
+ # if(req.url ~ "^/images" ||
146
+ # req.url ~ "^/stylesheets" ||
147
+ # req.url ~ "^/javascripts" ||
148
+ # req.url ~ "^(/[a-z]{2})?/(info_screens|class_sections)/[0-9]+.*$" ||
149
+ # req.url ~ "^(/[a-z]{2})?/class_sections/[0-9]+/open_scoring.*$") {
150
+ # unset req.http.Cookie;
151
+ # unset req.http.Authorization;
152
+ # return(lookup);
153
+ # }
154
+ }
155
+
156
+ sub vcl_fetch {
157
+ <%= Lacquer.cache_control.to_vcl_override_ttl_urls %>
158
+
159
+ # Generates
160
+ #
161
+ # if(req.url ~ "^/images" || req.url ~ "^/stylesheets" || req.url ~ "^/javascripts") {
162
+ # unset beresp.http.Set-Cookie;
163
+ # set beresp.ttl = 365d;
164
+ # return(deliver);
165
+ # }
166
+ #
167
+ # if(req.url ~ "^(/[a-z]{2})?/(info_screens|class_sections)/[0-9]+.*$" ||
168
+ # req.url ~ "^(/[a-z]{2})?/class_sections/[0-9]+/open_scoring.*$") {
169
+ # unset beresp.http.Set-Cookie;
170
+ # set beresp.ttl = 1m;
171
+ # return(deliver);
172
+ # }
173
+ }
174
+ ```
175
+
176
+ This makes it much simpler to perform cacheing, it's only setuped in one place, purge it or just let it expire.
177
+
178
+ ## Usage
179
+
180
+ To set a custom ttl for a controller:
181
+
182
+ `before_filter { |controller| controller.set_cache_ttl(15.minutes) }`
183
+
184
+ Clearing the cache:
185
+
186
+ ```ruby
187
+ class Posts < ApplicationController
188
+ after_filter :clear_cache, :only => [ :create, :update, :destroy ]
189
+
190
+ private
191
+
192
+ def clear_cache
193
+ clear_cache_for(
194
+ root_path,
195
+ posts_path,
196
+ post_path(@post))
197
+ end
198
+ end
199
+ ```
200
+
201
+ Control varnishd with the following rake tasks
202
+
203
+ ```text
204
+ rake lacquer:varnishd:start
205
+ rake lacquer:varnishd:stop
206
+ rake lacquer:varnishd:restart
207
+ rake lacquer:varnishd:reload
208
+ rake lacquer:varnishd:status
209
+ rake lacquer:varnishd:global_purge
210
+ ```
211
+
212
+ ## Deployment
213
+ Lacquer supports Capistrano 2.5+
214
+
215
+ For Capistrano 2.5 just add `require 'lacquer/capistrano'` to your `config/deploy.rb` file.
216
+
217
+ For Capistrano 3+ add `require 'lacquer/capistrano'` to your `Capfile`.
218
+
219
+ ```ruby
220
+ # Capfile
221
+ require 'capistrano/setup'
222
+ require 'capistrano/deploy'
223
+ require 'capistrano/rails/assets'
224
+ require 'capistrano/rails/migrations'
225
+ require 'lacquer/capistrano'
226
+ ```
227
+
228
+ Adding this will give you the following cap tasks
229
+
230
+ ```text
231
+ cap lacquer:global_purge # global_purge varnish
232
+ cap lacquer:restart # restart varnish
233
+ cap lacquer:start # start varnish
234
+ cap lacquer:status # status varnish
235
+ cap lacquer:stop # stop varnish
236
+ ```
237
+
238
+ ## Gotchas
239
+
240
+ The default TTL for most actions is set to 0, since for most cases you'll probably want to be fairly explicit about what pages do get cached by varnish. The default cache header is typically:
241
+
242
+ Cache-Control: max-age=0, no-cache, private
243
+
244
+ This is good for normal controller actions, since you won't want to cache them. If TTL for an action is set to 0, it won't mess with the default header.
245
+
246
+ The key gotcha here is that cached pages strip cookies, so if your application relies on sessions and uses authenticity tokens, the user will need a session cookie set before form actions will work. Setting default TTL to 0 here will make sure these session cookies won't break.
247
+
248
+ As a result, all you have to do to set a cacheable action is the before filter above.
249
+
250
+ ## Note on Patches/Pull Requests
251
+
252
+ * Fork the project.
253
+ * Make your feature addition or bug fix.
254
+ * Add tests for it. This is important so I don't break it in a future version unintentionally.
255
+ * Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
256
+ * Send me a pull request. Bonus points for topic branches.
257
+
258
+ ## Copyright
259
+
260
+ Copyright (c) 2015 Russ Smith. See LICENSE for details.
data/Rakefile CHANGED
@@ -1,37 +1,15 @@
1
- require "rubygems"
2
- require "bundler/setup"
3
-
4
- require "rake"
5
- require "yard"
6
- require "rspec"
7
- require "rspec/core/rake_task"
8
-
9
- begin
10
- require 'jeweler'
11
- Jeweler::Tasks.new do |gem|
12
- gem.name = "lacquer"
13
- gem.summary = %Q{Rails drop in for Varnish support.}
14
- gem.description = %Q{Rails drop in for Varnish support.}
15
- gem.email = "russ@bashme.org"
16
- gem.homepage = "http://github.com/russ/lacquer"
17
- gem.authors = ["Russ Smith (russ@bashme.org)", "Ryan Johns", "Garry Tan (garry@posterous.com), Gabe da Silveira (gabe@websaviour.com)"]
18
- gem.add_development_dependency "rspec", "~> 1.3.0"
19
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
1
+ require 'bundler/gem_tasks'
2
+ require 'rake'
3
+ require 'rspec/core/rake_task'
4
+
5
+ namespace :spec do
6
+ RSpec::Core::RakeTask.new(:normal) do |t|
7
+ t.pattern ='spec/**/*_spec.rb'
8
+ t.rcov = false
20
9
  end
21
- Jeweler::GemcutterTasks.new
22
- rescue LoadError
23
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
24
10
  end
25
11
 
26
- desc "Run all examples"
27
- RSpec::Core::RakeTask.new(:spec) do |t|
28
- t.rspec_path = "rspec"
29
- t.rspec_opts = %w[--color]
30
- end
31
-
32
- YARD::Rake::YardocTask.new do |t|
33
- t.files = [ "lib/**/*.rb" ]
34
- t.options = [ "--no-private" ]
35
- end
12
+ desc "RSpec tests"
13
+ task "spec" => "spec:normal"
36
14
 
37
- task :default => [ :spec ]
15
+ task "default" => "spec"
data/init.rb CHANGED
@@ -1 +1 @@
1
- require File.join(File.dirname(__FILE__), 'lib', 'lacquer')
1
+ require File.join(File.dirname(__FILE__), 'lib', 'lacquer')
data/lacquer.gemspec CHANGED
@@ -1,74 +1,24 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
1
  # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "lacquer/version"
5
4
 
6
5
  Gem::Specification.new do |s|
7
- s.name = %q{lacquer}
8
- s.version = "0.4.0"
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Russ Smith (russ@bashme.org)", "Ryan Johns", "Garry Tan (garry@posterous.com), Gabe da Silveira (gabe@websaviour.com)"]
12
- s.date = %q{2010-11-03}
13
- s.description = %q{Rails drop in for Varnish support.}
14
- s.email = %q{russ@bashme.org}
15
- s.extra_rdoc_files = [
16
- "LICENSE",
17
- "README.rdoc"
18
- ]
19
- s.files = [
20
- ".bundle/config",
21
- ".document",
22
- ".gitignore",
23
- "Gemfile",
24
- "Gemfile.lock",
25
- "LICENSE",
26
- "README.rdoc",
27
- "Rakefile",
28
- "VERSION",
29
- "init.rb",
30
- "lacquer.gemspec",
31
- "lib/generators/lacquer/USAGE",
32
- "lib/generators/lacquer/install_generator.rb",
33
- "lib/generators/lacquer/templates/initializer.rb",
34
- "lib/generators/lacquer/templates/varnish.sample.vcl",
35
- "lib/lacquer.rb",
36
- "lib/lacquer/cache_utils.rb",
37
- "lib/lacquer/configuration.rb",
38
- "lib/lacquer/delayed_job_job.rb",
39
- "lib/lacquer/resque_job.rb",
40
- "lib/lacquer/varnish.rb",
41
- "rails/init.rb",
42
- "spec/lacquer/cache_utils_spec.rb",
43
- "spec/lacquer/delayed_job_job_spec.rb",
44
- "spec/lacquer/resque_job_spec.rb",
45
- "spec/lacquer/varnish_spec.rb",
46
- "spec/spec_helper.rb"
47
- ]
48
- s.homepage = %q{http://github.com/russ/lacquer}
49
- s.rdoc_options = ["--charset=UTF-8"]
6
+ s.name = "lacquer"
7
+ s.version = Lacquer::VERSION
8
+ s.authors = ["Russ Smith (russ@bashme.org)", "Ryan Johns", "Garry Tan (garry@posterous.com), Gabe da Silveira (gabe@websaviour.com)", "H\u{e5}kon Lerring"]
9
+ s.email = "russ@bashme.org"
10
+ s.homepage = "http://github.com/russ/lacquer"
11
+ s.summary = "Rails drop in for Varnish support."
12
+ s.description = "Rails drop in for Varnish support."
13
+ s.rubyforge_project = "lacquer"
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
50
17
  s.require_paths = ["lib"]
51
- s.rubygems_version = %q{1.3.7}
52
- s.summary = %q{Rails drop in for Varnish support.}
53
- s.test_files = [
54
- "spec/lacquer/cache_utils_spec.rb",
55
- "spec/lacquer/delayed_job_job_spec.rb",
56
- "spec/lacquer/resque_job_spec.rb",
57
- "spec/lacquer/varnish_spec.rb",
58
- "spec/spec_helper.rb"
59
- ]
60
-
61
- if s.respond_to? :specification_version then
62
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
63
- s.specification_version = 3
64
-
65
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
66
- s.add_development_dependency(%q<rspec>, ["~> 1.3.0"])
67
- else
68
- s.add_dependency(%q<rspec>, ["~> 1.3.0"])
69
- end
70
- else
71
- s.add_dependency(%q<rspec>, ["~> 1.3.0"])
72
- end
18
+ s.add_dependency('activesupport', '>= 2.3.10')
19
+ s.add_dependency('i18n', '~> 0.4')
20
+ s.add_dependency('erubis')
21
+ s.add_development_dependency('rake')
22
+ s.add_development_dependency('rspec', '~> 2.5')
23
+ s.add_development_dependency('yard')
73
24
  end
74
-
@@ -8,7 +8,11 @@ module Lacquer
8
8
  end
9
9
 
10
10
  def copy_vcl
11
- copy_file("varnish.sample.vcl", "config/varnish.sample.vcl")
11
+ copy_file("varnish.vcl.erb", "config/varnish.vcl.erb")
12
+ end
13
+
14
+ def copy_varnishd_config
15
+ copy_file("varnishd.yml", "config/varnishd.yml")
12
16
  end
13
17
  end
14
18
  end
@@ -5,17 +5,29 @@ Lacquer.configure do |config|
5
5
  # Unless overridden in a controller or action, the default will be used
6
6
  config.default_ttl = 1.week
7
7
 
8
- # Can be :none, :delayed_job, :resque
8
+ # Can be :none, :delayed_job, :resque, :sidekiq
9
9
  config.job_backend = :none
10
10
 
11
11
  # Array of Varnish servers to manage
12
12
  config.varnish_servers << {
13
- :host => "0.0.0.0", :port => 6082
13
+ :host => "0.0.0.0", :port => 6082 # if you have authentication enabled, add :secret => "your secret"
14
14
  }
15
15
 
16
16
  # Number of retries
17
17
  config.retries = 5
18
18
 
19
19
  # Config handler (optional, if you use Hoptoad or another error tracking service)
20
- config.command_error_handler = lambda { |s| HoptoadNotifier.notify(s) }
20
+ # config.command_error_handler = lambda { |s| HoptoadNotifier.notify(s) }
21
+
22
+
23
+ ### Varnish - 2.x / 3.x .. VCL-Changes
24
+ ### https://www.varnish-cache.org/docs/trunk/installation/upgrade.html
25
+
26
+ # => Purge Command ( "url.purge" for Varnish 2.x .. "ban.url" for Varnish 3.x )
27
+ # => purges are now called bans in Varnish 3.x .. purge() and purge_url() are now respectively ban() and ban_url()
28
+ config.purge_command = "url.purge"
29
+
30
+ # => VCL_Fetch Pass Command ( "pass" for Varnish 2.x .. "hit_for_pass" for Varnish 3.x )
31
+ # => pass in vcl_fetch renamed to hit_for_pass in Varnish 3.x
32
+ config.pass_command = "pass"
21
33
  end
@@ -1,6 +1,6 @@
1
1
  backend default {
2
- .host = "0.0.0.0";
3
- .port = "10000";
2
+ .host = "<%= backend.split(':').first %>";
3
+ .port = "<%= backend.split(':').last %>";
4
4
  }
5
5
 
6
6
  # Handling of requests that are received from clients.
@@ -14,19 +14,20 @@ sub vcl_recv {
14
14
  req.request != "TRACE" &&
15
15
  req.request != "OPTIONS" &&
16
16
  req.request != "DELETE") {
17
- pipe;
17
+ return(pipe);
18
18
  }
19
-
19
+
20
+ if (req.backend.healthy) {
21
+ set req.grace = 30s;
22
+ } else {
23
+ set req.grace = 1h;
24
+ }
25
+
20
26
  # Pass requests that are not GET or HEAD
21
27
  if (req.request != "GET" && req.request != "HEAD") {
22
- pass;
23
- }
24
-
25
- # Pass requests that we know we aren't caching
26
- if (req.url ~ "^/admin") {
27
- pass;
28
+ return(pass);
28
29
  }
29
-
30
+
30
31
  # Handle compression correctly. Varnish treats headers literally, not
31
32
  # semantically. So it is very well possible that there are cache misses
32
33
  # because the headers sent by different browsers aren't the same.
@@ -47,8 +48,7 @@ sub vcl_recv {
47
48
  # Clear cookie and authorization headers, set grace time, lookup in the cache
48
49
  unset req.http.Cookie;
49
50
  unset req.http.Authorization;
50
- set req.grace = 30s;
51
- lookup;
51
+ return(lookup);
52
52
  }
53
53
 
54
54
  # Called when entering pipe mode
@@ -57,34 +57,34 @@ sub vcl_pipe {
57
57
  # requests from the client will also be piped through and
58
58
  # left untouched by varnish. We don't want that.
59
59
  set req.http.connection = "close";
60
- pipe;
60
+ return(pipe);
61
61
  }
62
62
 
63
63
  # Called when the requested object has been retrieved from the
64
64
  # backend, or the request to the backend has failed
65
65
  sub vcl_fetch {
66
- # Do not cache the object if the backend application does not want us to.
67
- if (obj.http.Cache-Control ~ "(no-cache|no-store|private|must-revalidate)") {
68
- pass;
69
- }
70
-
66
+ # Set the grace time
67
+ set beresp.grace = 1h;
68
+
71
69
  # Do not cache the object if the status is not in the 200s
72
- if (obj.status >= 300) {
70
+ if (beresp.status >= 300) {
73
71
  # Remove the Set-Cookie header
74
- remove obj.http.Set-Cookie;
75
- pass;
72
+ remove beresp.http.Set-Cookie;
73
+ return(<%= Lacquer.configuration.pass_command %>);
74
+ }
75
+
76
+ # Do not cache the object if the backend application does not want us to.
77
+ if (beresp.http.Cache-Control ~ "(no-cache|no-store|private|must-revalidate)") {
78
+ return(<%= Lacquer.configuration.pass_command %>);
76
79
  }
77
80
 
78
81
  # Everything below here should be cached
79
82
 
80
83
  # Remove the Set-Cookie header
81
- remove obj.http.Set-Cookie;
82
-
83
- # Set the grace time
84
- set obj.grace = 30s;
84
+ remove beresp.http.Set-Cookie;
85
85
 
86
86
  # Deliver the object
87
- deliver;
87
+ return(deliver);
88
88
  }
89
89
 
90
90
  # Called before the response is sent back to the client
@@ -0,0 +1,31 @@
1
+ development:
2
+ listen: 127.0.0.1:3001
3
+ telnet: 127.0.0.1:6082
4
+ backend: 127.0.0.1:3000
5
+ sbin_path: /usr/local/sbin
6
+ bin_path: /usr/local/bin
7
+ storage: "file,#{Rails.root}/log/varnishd.#{Rails.env}.cache,100MB"
8
+
9
+ test:
10
+ listen: 127.0.0.1:3002
11
+ telnet: 127.0.0.1:6083
12
+ backend: 127.0.0.1:3000
13
+ sbin_path: /usr/local/sbin
14
+ bin_path: /usr/local/bin
15
+ storage: "file,#{Rails.root}/log/varnishd.#{Rails.env}.cache,100MB"
16
+
17
+ production:
18
+ listen: :80
19
+ telnet: localhost:6082
20
+ backend: backend_server:8080
21
+ use_sudo: false
22
+ sbin_path: /usr/local/sbin
23
+ bin_path: /usr/local/bin
24
+ pid_path: <%= Rails.root.join('log') %>
25
+ storage: "file,#{Rails.root}/log/varnishd.#{Rails.env}.cache,100MB"
26
+ params:
27
+ overflow_max: 2000 # for Varnish 2.x ... use "queue_max: 2000" for Varnish 3.x
28
+ thread_pool_add_delay: 2
29
+ thread_pools: 4 # <Number of cpu cores>
30
+ thread_pool_min: 200 # <800/number of cpu cores>
31
+ thread_pool_max: 4000