lacquer 0.5.4 → 0.5.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -4
- data/README.rdoc +214 -202
- data/lib/generators/lacquer/templates/initializer.rb +33 -21
- data/lib/generators/lacquer/templates/varnish.vcl.erb +102 -102
- data/lib/generators/lacquer/templates/varnishd.yml +26 -26
- data/lib/lacquer/capistrano.rb +1 -0
- data/lib/lacquer/configuration.rb +47 -43
- data/lib/lacquer/version.rb +1 -1
- metadata +14 -20
data/.gitignore
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
*.gem
|
2
|
-
.bundle
|
3
|
-
|
4
|
-
|
1
|
+
*.gem
|
2
|
+
.bundle
|
3
|
+
.DS_Store
|
4
|
+
Gemfile.lock
|
5
|
+
pkg/*
|
data/README.rdoc
CHANGED
@@ -1,202 +1,214 @@
|
|
1
|
-
= lacquer
|
2
|
-
|
3
|
-
Rails drop in for Varnish support.
|
4
|
-
|
5
|
-
== Install
|
6
|
-
This gem requires ruby 1.9
|
7
|
-
|
8
|
-
Basic installation
|
9
|
-
|
10
|
-
sudo gem install lacquer
|
11
|
-
rails generate lacquer:install
|
12
|
-
|
13
|
-
config/initializers/lacquer.rb
|
14
|
-
|
15
|
-
Lacquer.configure do |config|
|
16
|
-
# Globally enable/disable cache
|
17
|
-
config.enable_cache = true
|
18
|
-
|
19
|
-
# Unless overridden in a controller or action, the default will be used
|
20
|
-
config.default_ttl = 1.week
|
21
|
-
|
22
|
-
# Can be :none, :delayed_job, :resque
|
23
|
-
config.job_backend = :none
|
24
|
-
|
25
|
-
# Array of Varnish servers to manage
|
26
|
-
config.varnish_servers << {
|
27
|
-
:host => "0.0.0.0", :port => 6082 # if you have authentication enabled, add :secret => "your secret"
|
28
|
-
}
|
29
|
-
|
30
|
-
# Number of retries
|
31
|
-
config.retries = 5
|
32
|
-
|
33
|
-
# config handler (optional, if you use Hoptoad or another error tracking service)
|
34
|
-
config.command_error_handler = lambda { |s| HoptoadNotifier.notify(s) }
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
listen: :
|
59
|
-
telnet: localhost:6082
|
60
|
-
sbin_path: /usr/local/sbin
|
61
|
-
storage: "file,#{Rails.root}/log/varnishd.#{Rails.env}.cache,100MB"
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
#
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
#
|
134
|
-
#
|
135
|
-
#
|
136
|
-
#
|
137
|
-
#
|
138
|
-
#
|
139
|
-
# }
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
#
|
146
|
-
#
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
==
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
1
|
+
= lacquer
|
2
|
+
|
3
|
+
Rails drop in for Varnish support.
|
4
|
+
|
5
|
+
== Install
|
6
|
+
This gem requires ruby 1.9
|
7
|
+
|
8
|
+
Basic installation
|
9
|
+
|
10
|
+
sudo gem install lacquer
|
11
|
+
rails generate lacquer:install
|
12
|
+
|
13
|
+
config/initializers/lacquer.rb
|
14
|
+
|
15
|
+
Lacquer.configure do |config|
|
16
|
+
# Globally enable/disable cache
|
17
|
+
config.enable_cache = true
|
18
|
+
|
19
|
+
# Unless overridden in a controller or action, the default will be used
|
20
|
+
config.default_ttl = 1.week
|
21
|
+
|
22
|
+
# Can be :none, :delayed_job, :resque
|
23
|
+
config.job_backend = :none
|
24
|
+
|
25
|
+
# Array of Varnish servers to manage
|
26
|
+
config.varnish_servers << {
|
27
|
+
:host => "0.0.0.0", :port => 6082 # if you have authentication enabled, add :secret => "your secret"
|
28
|
+
}
|
29
|
+
|
30
|
+
# Number of retries
|
31
|
+
config.retries = 5
|
32
|
+
|
33
|
+
# config handler (optional, if you use Hoptoad or another error tracking service)
|
34
|
+
config.command_error_handler = lambda { |s| HoptoadNotifier.notify(s) }
|
35
|
+
|
36
|
+
|
37
|
+
### Varnish - 2.x / 3.x .. VCL-Changes
|
38
|
+
### https://www.varnish-cache.org/docs/trunk/installation/upgrade.html
|
39
|
+
|
40
|
+
# => Purge Command ( "url.purge" for Varnish 2.x .. "ban.url" for Varnish 3.x )
|
41
|
+
# => purges are now called bans in Varnish 3.x .. purge() and purge_url() are now respectively ban() and ban_url()
|
42
|
+
config.purge_command = "ban.url"
|
43
|
+
|
44
|
+
# => VCL_Fetch Pass Command ( "pass" for Varnish 2.x .. "hit_for_pass" for Varnish 3.x )
|
45
|
+
# => pass in vcl_fetch renamed to hit_for_pass in Varnish 3.x
|
46
|
+
config.vcl_pass_command = "pass"
|
47
|
+
end
|
48
|
+
|
49
|
+
app/controllers/application_controller.rb
|
50
|
+
|
51
|
+
class ApplicationController < ActionController::Base
|
52
|
+
include Lacquer::CacheUtils
|
53
|
+
end
|
54
|
+
|
55
|
+
config/varnishd.yml
|
56
|
+
|
57
|
+
development:
|
58
|
+
listen: localhost:3001
|
59
|
+
telnet: localhost:6082
|
60
|
+
sbin_path: /usr/local/sbin
|
61
|
+
storage: "file,#{Rails.root}/log/varnishd.#{Rails.env}.cache,100MB"
|
62
|
+
|
63
|
+
test:
|
64
|
+
listen: localhost:3002
|
65
|
+
telnet: localhost:6083
|
66
|
+
sbin_path: /usr/local/sbin
|
67
|
+
storage: "file,#{Rails.root}/log/varnishd.#{Rails.env}.cache,100MB"
|
68
|
+
|
69
|
+
production:
|
70
|
+
listen: :80
|
71
|
+
telnet: localhost:6082
|
72
|
+
sbin_path: /usr/local/sbin
|
73
|
+
storage: "file,#{Rails.root}/log/varnishd.#{Rails.env}.cache,100MB"
|
74
|
+
params:
|
75
|
+
overflow_max: 2000 # for Varnish 2.x ... use "queue_max: 2000" for Varnish 3.x
|
76
|
+
thread_pool_add_delay: 2
|
77
|
+
thread_pools: 4 # <Number of cpu cores>
|
78
|
+
thread_pool_min: 200 # <800/number of cpu cores>
|
79
|
+
thread_pool_max: 4000
|
80
|
+
|
81
|
+
If only some urls of the application should be cached by varnish, Lacquer::CacheControl will be helpful.
|
82
|
+
|
83
|
+
config/initializers/caches.rb
|
84
|
+
|
85
|
+
require "lacquer/cache_control"
|
86
|
+
|
87
|
+
Lacquer.cache_control.configure do |config|
|
88
|
+
config.register :static, :url => "^/images",
|
89
|
+
:expires_in => "365d"
|
90
|
+
|
91
|
+
config.register :static, :url => "^/stylesheets",
|
92
|
+
:expires_in => "365d"
|
93
|
+
|
94
|
+
config.register :static, :url => "^/javascripts",
|
95
|
+
:expires_in => "365d"
|
96
|
+
|
97
|
+
config.register :class_section, :url => "^(/[a-z]{2})?/(info_screens|class_sections)/%s.*$",
|
98
|
+
:args => "[0-9]+",
|
99
|
+
:expires_in => "1m"
|
100
|
+
|
101
|
+
config.register :open_scoring, :url => "^(/[a-z]{2})?/class_sections/%s/open_scoring.*$",
|
102
|
+
:args => "[0-9]+",
|
103
|
+
:expires_in => "1m"
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
In the sweeper we can do something like this
|
108
|
+
|
109
|
+
class_section = ClassSection.find(1)
|
110
|
+
Lacquer.cache_control.purge(:open_scoring, class_section)
|
111
|
+
|
112
|
+
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)
|
113
|
+
|
114
|
+
The varnish.vcl is preprocssed when starting varnishd with the rake tasks
|
115
|
+
|
116
|
+
rake lacquer:varnishd:start
|
117
|
+
|
118
|
+
config/varnish.vcl.erb
|
119
|
+
|
120
|
+
sub vcl_recv {
|
121
|
+
# Lookup requests that we know should be cached
|
122
|
+
if (<%= Lacquer.cache_control.to_vcl_conditions %>) {
|
123
|
+
# Clear cookie and authorization headers, set grace time, lookup in the cache
|
124
|
+
unset req.http.Cookie;
|
125
|
+
unset req.http.Authorization;
|
126
|
+
return(lookup);
|
127
|
+
}
|
128
|
+
|
129
|
+
# Generates
|
130
|
+
#
|
131
|
+
# if(req.url ~ "^/images" ||
|
132
|
+
# req.url ~ "^/stylesheets" ||
|
133
|
+
# req.url ~ "^/javascripts" ||
|
134
|
+
# req.url ~ "^(/[a-z]{2})?/(info_screens|class_sections)/[0-9]+.*$" ||
|
135
|
+
# req.url ~ "^(/[a-z]{2})?/class_sections/[0-9]+/open_scoring.*$") {
|
136
|
+
# unset req.http.Cookie;
|
137
|
+
# unset req.http.Authorization;
|
138
|
+
# return(lookup);
|
139
|
+
# }
|
140
|
+
}
|
141
|
+
|
142
|
+
sub vcl_fetch {
|
143
|
+
<%= Lacquer.cache_control.to_vcl_override_ttl_urls %>
|
144
|
+
|
145
|
+
# Generates
|
146
|
+
#
|
147
|
+
# if(req.url ~ "^/images" || req.url ~ "^/stylesheets" || req.url ~ "^/javascripts") {
|
148
|
+
# unset beresp.http.Set-Cookie;
|
149
|
+
# set beresp.ttl = 365d;
|
150
|
+
# return(deliver);
|
151
|
+
# }
|
152
|
+
#
|
153
|
+
# if(req.url ~ "^(/[a-z]{2})?/(info_screens|class_sections)/[0-9]+.*$" ||
|
154
|
+
# req.url ~ "^(/[a-z]{2})?/class_sections/[0-9]+/open_scoring.*$") {
|
155
|
+
# unset beresp.http.Set-Cookie;
|
156
|
+
# set beresp.ttl = 1m;
|
157
|
+
# return(deliver);
|
158
|
+
# }
|
159
|
+
}
|
160
|
+
|
161
|
+
This makes it much simpler to perform cacheing, it's only setuped in one place, purge it or just let it expire.
|
162
|
+
|
163
|
+
== Usage
|
164
|
+
|
165
|
+
To set a custom ttl for a controller:
|
166
|
+
|
167
|
+
before_filter { |controller| controller.set_cache_ttl(15.minutes) }
|
168
|
+
|
169
|
+
Clearing the cache:
|
170
|
+
|
171
|
+
class Posts < ApplicationController
|
172
|
+
after_filter :clear_cache, :only => [ :create, :update, :destroy ]
|
173
|
+
|
174
|
+
private
|
175
|
+
|
176
|
+
def clear_cache
|
177
|
+
clear_cache_for(
|
178
|
+
root_path,
|
179
|
+
posts_path,
|
180
|
+
post_path(@post))
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
Control varnishd with the following rake tasks
|
185
|
+
|
186
|
+
rake lacquer:varnishd:start
|
187
|
+
rake lacquer:varnishd:stop
|
188
|
+
rake lacquer:varnishd:restart
|
189
|
+
rake lacquer:varnishd:status
|
190
|
+
rake lacquer:varnishd:global_purge
|
191
|
+
|
192
|
+
== Gotchas
|
193
|
+
|
194
|
+
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:
|
195
|
+
|
196
|
+
Cache-Control: max-age=0, no-cache, private
|
197
|
+
|
198
|
+
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.
|
199
|
+
|
200
|
+
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.
|
201
|
+
|
202
|
+
As a result, all you have to do to set a cacheable action is the before filter above.
|
203
|
+
|
204
|
+
== Note on Patches/Pull Requests
|
205
|
+
|
206
|
+
* Fork the project.
|
207
|
+
* Make your feature addition or bug fix.
|
208
|
+
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
209
|
+
* 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)
|
210
|
+
* Send me a pull request. Bonus points for topic branches.
|
211
|
+
|
212
|
+
== Copyright
|
213
|
+
|
214
|
+
Copyright (c) 2010 Russ Smith. See LICENSE for details.
|
@@ -1,21 +1,33 @@
|
|
1
|
-
Lacquer.configure do |config|
|
2
|
-
# Globally enable/disable cache
|
3
|
-
config.enable_cache = true
|
4
|
-
|
5
|
-
# Unless overridden in a controller or action, the default will be used
|
6
|
-
config.default_ttl = 1.week
|
7
|
-
|
8
|
-
# Can be :none, :delayed_job, :resque
|
9
|
-
config.job_backend = :none
|
10
|
-
|
11
|
-
# Array of Varnish servers to manage
|
12
|
-
config.varnish_servers << {
|
13
|
-
:host => "0.0.0.0", :port => 6082 # if you have authentication enabled, add :secret => "your secret"
|
14
|
-
}
|
15
|
-
|
16
|
-
# Number of retries
|
17
|
-
config.retries = 5
|
18
|
-
|
19
|
-
# Config handler (optional, if you use Hoptoad or another error tracking service)
|
20
|
-
config.command_error_handler = lambda { |s| HoptoadNotifier.notify(s) }
|
21
|
-
|
1
|
+
Lacquer.configure do |config|
|
2
|
+
# Globally enable/disable cache
|
3
|
+
config.enable_cache = true
|
4
|
+
|
5
|
+
# Unless overridden in a controller or action, the default will be used
|
6
|
+
config.default_ttl = 1.week
|
7
|
+
|
8
|
+
# Can be :none, :delayed_job, :resque
|
9
|
+
config.job_backend = :none
|
10
|
+
|
11
|
+
# Array of Varnish servers to manage
|
12
|
+
config.varnish_servers << {
|
13
|
+
:host => "0.0.0.0", :port => 6082 # if you have authentication enabled, add :secret => "your secret"
|
14
|
+
}
|
15
|
+
|
16
|
+
# Number of retries
|
17
|
+
config.retries = 5
|
18
|
+
|
19
|
+
# Config handler (optional, if you use Hoptoad or another error tracking service)
|
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"
|
33
|
+
end
|
@@ -1,102 +1,102 @@
|
|
1
|
-
backend default {
|
2
|
-
.host = "<%= backend.split(':').first %>";
|
3
|
-
.port = "<%= backend.split(':').last %>";
|
4
|
-
}
|
5
|
-
|
6
|
-
# Handling of requests that are received from clients.
|
7
|
-
# First decide whether or not to lookup data in the cache.
|
8
|
-
sub vcl_recv {
|
9
|
-
# Pipe requests that are non-RFC2616 or CONNECT which is weird.
|
10
|
-
if (req.request != "GET" &&
|
11
|
-
req.request != "HEAD" &&
|
12
|
-
req.request != "PUT" &&
|
13
|
-
req.request != "POST" &&
|
14
|
-
req.request != "TRACE" &&
|
15
|
-
req.request != "OPTIONS" &&
|
16
|
-
req.request != "DELETE") {
|
17
|
-
return(pipe);
|
18
|
-
}
|
19
|
-
|
20
|
-
if (req.backend.healthy) {
|
21
|
-
set req.grace = 30s;
|
22
|
-
} else {
|
23
|
-
set req.grace = 1h;
|
24
|
-
}
|
25
|
-
|
26
|
-
# Pass requests that are not GET or HEAD
|
27
|
-
if (req.request != "GET" && req.request != "HEAD") {
|
28
|
-
return(pass);
|
29
|
-
}
|
30
|
-
|
31
|
-
# Handle compression correctly. Varnish treats headers literally, not
|
32
|
-
# semantically. So it is very well possible that there are cache misses
|
33
|
-
# because the headers sent by different browsers aren't the same.
|
34
|
-
# @see: http://varnish.projects.linpro.no/wiki/FAQ/Compression
|
35
|
-
if (req.http.Accept-Encoding) {
|
36
|
-
if (req.http.Accept-Encoding ~ "gzip") {
|
37
|
-
# if the browser supports it, we'll use gzip
|
38
|
-
set req.http.Accept-Encoding = "gzip";
|
39
|
-
} elsif (req.http.Accept-Encoding ~ "deflate") {
|
40
|
-
# next, try deflate if it is supported
|
41
|
-
set req.http.Accept-Encoding = "deflate";
|
42
|
-
} else {
|
43
|
-
# unknown algorithm. Probably junk, remove it
|
44
|
-
remove req.http.Accept-Encoding;
|
45
|
-
}
|
46
|
-
}
|
47
|
-
|
48
|
-
# Clear cookie and authorization headers, set grace time, lookup in the cache
|
49
|
-
unset req.http.Cookie;
|
50
|
-
unset req.http.Authorization;
|
51
|
-
return(lookup);
|
52
|
-
}
|
53
|
-
|
54
|
-
# Called when entering pipe mode
|
55
|
-
sub vcl_pipe {
|
56
|
-
# If we don't set the Connection: close header, any following
|
57
|
-
# requests from the client will also be piped through and
|
58
|
-
# left untouched by varnish. We don't want that.
|
59
|
-
set req.http.connection = "close";
|
60
|
-
return(pipe);
|
61
|
-
}
|
62
|
-
|
63
|
-
# Called when the requested object has been retrieved from the
|
64
|
-
# backend, or the request to the backend has failed
|
65
|
-
sub vcl_fetch {
|
66
|
-
# Set the grace time
|
67
|
-
set beresp.grace = 1h;
|
68
|
-
|
69
|
-
# Do not cache the object if the status is not in the 200s
|
70
|
-
if (beresp.status >= 300) {
|
71
|
-
# Remove the Set-Cookie header
|
72
|
-
remove beresp.http.Set-Cookie;
|
73
|
-
return(
|
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(
|
79
|
-
}
|
80
|
-
|
81
|
-
# Everything below here should be cached
|
82
|
-
|
83
|
-
# Remove the Set-Cookie header
|
84
|
-
remove beresp.http.Set-Cookie;
|
85
|
-
|
86
|
-
# Deliver the object
|
87
|
-
return(deliver);
|
88
|
-
}
|
89
|
-
|
90
|
-
# Called before the response is sent back to the client
|
91
|
-
sub vcl_deliver {
|
92
|
-
# Force browsers and intermediary caches to always check back with us
|
93
|
-
set resp.http.Cache-Control = "private, max-age=0, must-revalidate";
|
94
|
-
set resp.http.Pragma = "no-cache";
|
95
|
-
|
96
|
-
# Add a header to indicate a cache HIT/MISS
|
97
|
-
if (obj.hits > 0) {
|
98
|
-
set resp.http.X-Cache = "HIT";
|
99
|
-
} else {
|
100
|
-
set resp.http.X-Cache = "MISS";
|
101
|
-
}
|
102
|
-
}
|
1
|
+
backend default {
|
2
|
+
.host = "<%= backend.split(':').first %>";
|
3
|
+
.port = "<%= backend.split(':').last %>";
|
4
|
+
}
|
5
|
+
|
6
|
+
# Handling of requests that are received from clients.
|
7
|
+
# First decide whether or not to lookup data in the cache.
|
8
|
+
sub vcl_recv {
|
9
|
+
# Pipe requests that are non-RFC2616 or CONNECT which is weird.
|
10
|
+
if (req.request != "GET" &&
|
11
|
+
req.request != "HEAD" &&
|
12
|
+
req.request != "PUT" &&
|
13
|
+
req.request != "POST" &&
|
14
|
+
req.request != "TRACE" &&
|
15
|
+
req.request != "OPTIONS" &&
|
16
|
+
req.request != "DELETE") {
|
17
|
+
return(pipe);
|
18
|
+
}
|
19
|
+
|
20
|
+
if (req.backend.healthy) {
|
21
|
+
set req.grace = 30s;
|
22
|
+
} else {
|
23
|
+
set req.grace = 1h;
|
24
|
+
}
|
25
|
+
|
26
|
+
# Pass requests that are not GET or HEAD
|
27
|
+
if (req.request != "GET" && req.request != "HEAD") {
|
28
|
+
return(pass);
|
29
|
+
}
|
30
|
+
|
31
|
+
# Handle compression correctly. Varnish treats headers literally, not
|
32
|
+
# semantically. So it is very well possible that there are cache misses
|
33
|
+
# because the headers sent by different browsers aren't the same.
|
34
|
+
# @see: http://varnish.projects.linpro.no/wiki/FAQ/Compression
|
35
|
+
if (req.http.Accept-Encoding) {
|
36
|
+
if (req.http.Accept-Encoding ~ "gzip") {
|
37
|
+
# if the browser supports it, we'll use gzip
|
38
|
+
set req.http.Accept-Encoding = "gzip";
|
39
|
+
} elsif (req.http.Accept-Encoding ~ "deflate") {
|
40
|
+
# next, try deflate if it is supported
|
41
|
+
set req.http.Accept-Encoding = "deflate";
|
42
|
+
} else {
|
43
|
+
# unknown algorithm. Probably junk, remove it
|
44
|
+
remove req.http.Accept-Encoding;
|
45
|
+
}
|
46
|
+
}
|
47
|
+
|
48
|
+
# Clear cookie and authorization headers, set grace time, lookup in the cache
|
49
|
+
unset req.http.Cookie;
|
50
|
+
unset req.http.Authorization;
|
51
|
+
return(lookup);
|
52
|
+
}
|
53
|
+
|
54
|
+
# Called when entering pipe mode
|
55
|
+
sub vcl_pipe {
|
56
|
+
# If we don't set the Connection: close header, any following
|
57
|
+
# requests from the client will also be piped through and
|
58
|
+
# left untouched by varnish. We don't want that.
|
59
|
+
set req.http.connection = "close";
|
60
|
+
return(pipe);
|
61
|
+
}
|
62
|
+
|
63
|
+
# Called when the requested object has been retrieved from the
|
64
|
+
# backend, or the request to the backend has failed
|
65
|
+
sub vcl_fetch {
|
66
|
+
# Set the grace time
|
67
|
+
set beresp.grace = 1h;
|
68
|
+
|
69
|
+
# Do not cache the object if the status is not in the 200s
|
70
|
+
if (beresp.status >= 300) {
|
71
|
+
# Remove the Set-Cookie header
|
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 %>);
|
79
|
+
}
|
80
|
+
|
81
|
+
# Everything below here should be cached
|
82
|
+
|
83
|
+
# Remove the Set-Cookie header
|
84
|
+
remove beresp.http.Set-Cookie;
|
85
|
+
|
86
|
+
# Deliver the object
|
87
|
+
return(deliver);
|
88
|
+
}
|
89
|
+
|
90
|
+
# Called before the response is sent back to the client
|
91
|
+
sub vcl_deliver {
|
92
|
+
# Force browsers and intermediary caches to always check back with us
|
93
|
+
set resp.http.Cache-Control = "private, max-age=0, must-revalidate";
|
94
|
+
set resp.http.Pragma = "no-cache";
|
95
|
+
|
96
|
+
# Add a header to indicate a cache HIT/MISS
|
97
|
+
if (obj.hits > 0) {
|
98
|
+
set resp.http.X-Cache = "HIT";
|
99
|
+
} else {
|
100
|
+
set resp.http.X-Cache = "MISS";
|
101
|
+
}
|
102
|
+
}
|
@@ -1,26 +1,26 @@
|
|
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
|
-
storage: "file,#{Rails.root}/log/varnishd.#{Rails.env}.cache,100MB"
|
7
|
-
|
8
|
-
test:
|
9
|
-
listen: 127.0.0.1:3002
|
10
|
-
telnet: 127.0.0.1:6083
|
11
|
-
backend: 127.0.0.1:3000
|
12
|
-
sbin_path: /usr/local/sbin
|
13
|
-
storage: "file,#{Rails.root}/log/varnishd.#{Rails.env}.cache,100MB"
|
14
|
-
|
15
|
-
production:
|
16
|
-
listen: :80
|
17
|
-
telnet: localhost:6082
|
18
|
-
backend: backend_server:8080
|
19
|
-
sbin_path: /usr/local/sbin
|
20
|
-
storage: "file,#{Rails.root}/log/varnishd.#{Rails.env}.cache,100MB"
|
21
|
-
params:
|
22
|
-
overflow_max: 2000
|
23
|
-
thread_pool_add_delay: 2
|
24
|
-
thread_pools: 4 # <Number of cpu cores>
|
25
|
-
thread_pool_min: 200 # <800/number of cpu cores>
|
26
|
-
thread_pool_max: 4000
|
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
|
+
storage: "file,#{Rails.root}/log/varnishd.#{Rails.env}.cache,100MB"
|
7
|
+
|
8
|
+
test:
|
9
|
+
listen: 127.0.0.1:3002
|
10
|
+
telnet: 127.0.0.1:6083
|
11
|
+
backend: 127.0.0.1:3000
|
12
|
+
sbin_path: /usr/local/sbin
|
13
|
+
storage: "file,#{Rails.root}/log/varnishd.#{Rails.env}.cache,100MB"
|
14
|
+
|
15
|
+
production:
|
16
|
+
listen: :80
|
17
|
+
telnet: localhost:6082
|
18
|
+
backend: backend_server:8080
|
19
|
+
sbin_path: /usr/local/sbin
|
20
|
+
storage: "file,#{Rails.root}/log/varnishd.#{Rails.env}.cache,100MB"
|
21
|
+
params:
|
22
|
+
overflow_max: 2000 # for Varnish 2.x ... use "queue_max: 2000" for Varnish 3.x
|
23
|
+
thread_pool_add_delay: 2
|
24
|
+
thread_pools: 4 # <Number of cpu cores>
|
25
|
+
thread_pool_min: 200 # <800/number of cpu cores>
|
26
|
+
thread_pool_max: 4000
|
data/lib/lacquer/capistrano.rb
CHANGED
@@ -11,6 +11,7 @@ Capistrano::Configuration.instance(:must_exit).load do
|
|
11
11
|
|
12
12
|
namespace :lacquer do
|
13
13
|
%w( start stop restart global_purge status ).each do |name|
|
14
|
+
desc "#{name} varnish"
|
14
15
|
task name.to_sym, :roles => lacquer_roles do
|
15
16
|
next if find_servers_for_task(current_task).empty?
|
16
17
|
run "cd #{current_path} && #{rake} lacquer:varnishd:#{name}"
|
@@ -1,43 +1,47 @@
|
|
1
|
-
module Lacquer
|
2
|
-
class Configuration
|
3
|
-
OPTIONS = []
|
4
|
-
|
5
|
-
# Enable cache
|
6
|
-
attr_accessor :enable_cache
|
7
|
-
|
8
|
-
# Varnish servers
|
9
|
-
attr_accessor :varnish_servers
|
10
|
-
|
11
|
-
# Application default ttl
|
12
|
-
attr_accessor :default_ttl
|
13
|
-
|
14
|
-
# Number of retries before failing
|
15
|
-
attr_accessor :retries
|
16
|
-
|
17
|
-
# Job backend
|
18
|
-
attr_accessor :job_backend
|
19
|
-
|
20
|
-
# Error handler
|
21
|
-
attr_accessor :command_error_handler
|
22
|
-
|
23
|
-
# Purge Command
|
24
|
-
attr_accessor :purge_command
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
@
|
31
|
-
@
|
32
|
-
@
|
33
|
-
@
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
1
|
+
module Lacquer
|
2
|
+
class Configuration
|
3
|
+
OPTIONS = []
|
4
|
+
|
5
|
+
# Enable cache
|
6
|
+
attr_accessor :enable_cache
|
7
|
+
|
8
|
+
# Varnish servers
|
9
|
+
attr_accessor :varnish_servers
|
10
|
+
|
11
|
+
# Application default ttl
|
12
|
+
attr_accessor :default_ttl
|
13
|
+
|
14
|
+
# Number of retries before failing
|
15
|
+
attr_accessor :retries
|
16
|
+
|
17
|
+
# Job backend
|
18
|
+
attr_accessor :job_backend
|
19
|
+
|
20
|
+
# Error handler
|
21
|
+
attr_accessor :command_error_handler
|
22
|
+
|
23
|
+
# Purge Command
|
24
|
+
attr_accessor :purge_command
|
25
|
+
|
26
|
+
# Pass Command (in vcl_fetch)
|
27
|
+
attr_accessor :pass_command
|
28
|
+
|
29
|
+
def initialize
|
30
|
+
@enable_cache = true
|
31
|
+
@varnish_servers = []
|
32
|
+
@default_ttl = 0
|
33
|
+
@job_backend = :none
|
34
|
+
@retries = 5
|
35
|
+
@command_error_handler = nil
|
36
|
+
@purge_command = "url.purge"
|
37
|
+
@pass_command = "pass"
|
38
|
+
end
|
39
|
+
|
40
|
+
# Returns a hash of all configurable options
|
41
|
+
def to_hash
|
42
|
+
OPTIONS.inject({}) do |hash, option|
|
43
|
+
hash.merge(option.to_sym => send(option))
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/lacquer/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lacquer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -12,11 +12,11 @@ authors:
|
|
12
12
|
autorequire:
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
|
-
date:
|
15
|
+
date: 2012-01-11 00:00:00.000000000 Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: activesupport
|
19
|
-
requirement: &
|
19
|
+
requirement: &70107447903980 !ruby/object:Gem::Requirement
|
20
20
|
none: false
|
21
21
|
requirements:
|
22
22
|
- - ! '>='
|
@@ -24,10 +24,10 @@ dependencies:
|
|
24
24
|
version: 2.3.10
|
25
25
|
type: :runtime
|
26
26
|
prerelease: false
|
27
|
-
version_requirements: *
|
27
|
+
version_requirements: *70107447903980
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: i18n
|
30
|
-
requirement: &
|
30
|
+
requirement: &70107447914580 !ruby/object:Gem::Requirement
|
31
31
|
none: false
|
32
32
|
requirements:
|
33
33
|
- - ~>
|
@@ -35,10 +35,10 @@ dependencies:
|
|
35
35
|
version: '0.4'
|
36
36
|
type: :runtime
|
37
37
|
prerelease: false
|
38
|
-
version_requirements: *
|
38
|
+
version_requirements: *70107447914580
|
39
39
|
- !ruby/object:Gem::Dependency
|
40
40
|
name: erubis
|
41
|
-
requirement: &
|
41
|
+
requirement: &70107447924320 !ruby/object:Gem::Requirement
|
42
42
|
none: false
|
43
43
|
requirements:
|
44
44
|
- - ! '>='
|
@@ -46,10 +46,10 @@ dependencies:
|
|
46
46
|
version: '0'
|
47
47
|
type: :runtime
|
48
48
|
prerelease: false
|
49
|
-
version_requirements: *
|
49
|
+
version_requirements: *70107447924320
|
50
50
|
- !ruby/object:Gem::Dependency
|
51
51
|
name: rake
|
52
|
-
requirement: &
|
52
|
+
requirement: &70107447921200 !ruby/object:Gem::Requirement
|
53
53
|
none: false
|
54
54
|
requirements:
|
55
55
|
- - ! '>='
|
@@ -57,10 +57,10 @@ dependencies:
|
|
57
57
|
version: '0'
|
58
58
|
type: :development
|
59
59
|
prerelease: false
|
60
|
-
version_requirements: *
|
60
|
+
version_requirements: *70107447921200
|
61
61
|
- !ruby/object:Gem::Dependency
|
62
62
|
name: rspec
|
63
|
-
requirement: &
|
63
|
+
requirement: &70107447934240 !ruby/object:Gem::Requirement
|
64
64
|
none: false
|
65
65
|
requirements:
|
66
66
|
- - ~>
|
@@ -68,10 +68,10 @@ dependencies:
|
|
68
68
|
version: '2.5'
|
69
69
|
type: :development
|
70
70
|
prerelease: false
|
71
|
-
version_requirements: *
|
71
|
+
version_requirements: *70107447934240
|
72
72
|
- !ruby/object:Gem::Dependency
|
73
73
|
name: yard
|
74
|
-
requirement: &
|
74
|
+
requirement: &70107447929060 !ruby/object:Gem::Requirement
|
75
75
|
none: false
|
76
76
|
requirements:
|
77
77
|
- - ! '>='
|
@@ -79,7 +79,7 @@ dependencies:
|
|
79
79
|
version: '0'
|
80
80
|
type: :development
|
81
81
|
prerelease: false
|
82
|
-
version_requirements: *
|
82
|
+
version_requirements: *70107447929060
|
83
83
|
description: Rails drop in for Varnish support.
|
84
84
|
email: russ@bashme.org
|
85
85
|
executables: []
|
@@ -138,18 +138,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
138
138
|
- - ! '>='
|
139
139
|
- !ruby/object:Gem::Version
|
140
140
|
version: '0'
|
141
|
-
segments:
|
142
|
-
- 0
|
143
|
-
hash: -1420781862807341168
|
144
141
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
145
142
|
none: false
|
146
143
|
requirements:
|
147
144
|
- - ! '>='
|
148
145
|
- !ruby/object:Gem::Version
|
149
146
|
version: '0'
|
150
|
-
segments:
|
151
|
-
- 0
|
152
|
-
hash: -1420781862807341168
|
153
147
|
requirements: []
|
154
148
|
rubyforge_project: lacquer
|
155
149
|
rubygems_version: 1.8.10
|