posterous-lacquer 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -38,6 +38,7 @@ app/controllers/application_controller.rb
38
38
  end
39
39
 
40
40
  == Usage
41
+
41
42
  To set a custom ttl for a controller:
42
43
 
43
44
  before_filter { |controller| controller.set_cache_ttl(15.minutes) }
@@ -56,6 +57,185 @@ Clearing the cache:
56
57
  post_path(@post))
57
58
  end
58
59
  end
60
+
61
+ == Gotchas
62
+
63
+ 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
64
+
65
+ Cache-Control: max-age=0, no-cache, private
66
+
67
+ 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.
68
+
69
+ 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.
70
+
71
+ As a result, all you have to do to set a cacheable action is the before filter above.
72
+
73
+
74
+
75
+ == VCL config
76
+
77
+ backend default {
78
+ .host = "127.0.0.1";
79
+ .port = "8282";
80
+ .max_connections = 2000;
81
+ .connect_timeout = 600s;
82
+ .first_byte_timeout = 600s;
83
+ .between_bytes_timeout = 600s;
84
+ }
85
+
86
+ sub vcl_hash {
87
+ ### these 2 entries are the default ones used for vcl. Below we add our own.
88
+ set req.hash += req.url;
89
+ set req.hash += req.http.host;
90
+
91
+ if (req.http.cookie ~ "mobile_view=true") {
92
+ set req.hash += "mobile";
93
+ }
94
+ if (req.http.user-agent ~ "(?i)palm|blackberry|nokia|phone|midp|mobi|symbian|chtml|ericsson|minimo|audiovox|motorola|samsung|telit|upg1|windows ce|ucweb|astel|plucker|x320|x240|j2me|sgh|portable|sprint|docomo|kddi|softbank|android|mmp|pdxgw|netfront|xiino|vodafone|portalmmm|sagem|mot-|sie-|ipod|up\\.b|webos|amoi|novarra|cdm|alcatel|pocket|iphone|mobileexplorer|mobile" && !(req.http.user-agent ~ "(?i)ipad") && !(req.http.cookie ~ "full_site=true")) {
95
+ set req.hash += "mobile";
96
+ }
97
+
98
+ return(hash);
99
+ }
100
+
101
+ #
102
+ # Handling of requests that are received from clients.
103
+ # First decide whether or not to lookup data in the cache.
104
+ #
105
+ sub vcl_recv {
106
+ # Pipe requests that are non-RFC2616 or CONNECT which is weird.
107
+ if (req.request != "GET" &&
108
+ req.request != "HEAD" &&
109
+ req.request != "PUT" &&
110
+ req.request != "POST" &&
111
+ req.request != "TRACE" &&
112
+ req.request != "OPTIONS" &&
113
+ req.request != "DELETE") {
114
+ return(pipe);
115
+ }
116
+
117
+ # Pass requests that are not GET or HEAD
118
+ if (req.request != "GET" && req.request != "HEAD") {
119
+ return(pass);
120
+ }
121
+
122
+ # Pass requests for blog pages greater than page 3
123
+ if (req.url ~ "page=([4-9]|[1-9][0-9]+)$") {
124
+ return(pass);
125
+ }
126
+
127
+ # Pass requests that we know we aren't caching
128
+ if (req.url ~ "^/admin") {
129
+ return(pass);
130
+ }
131
+
132
+ if (req.url ~ "\/private\/") {
133
+ return(pass);
134
+ }
135
+
136
+ #
137
+ # Everything below here should be cached
138
+ #
139
+
140
+ # Handle compression correctly. Varnish treats headers literally, not
141
+ # semantically. So it is very well possible that there are cache misses
142
+ # because the headers sent by different browsers aren't the same.
143
+ # @see: http://varnish.projects.linpro.no/wiki/FAQ/Compression
144
+ if (req.http.Accept-Encoding) {
145
+ if (req.http.Accept-Encoding ~ "gzip") {
146
+ # if the browser supports it, we'll use gzip
147
+ set req.http.Accept-Encoding = "gzip";
148
+ } elsif (req.http.Accept-Encoding ~ "deflate") {
149
+ # next, try deflate if it is supported
150
+ set req.http.Accept-Encoding = "deflate";
151
+ } else {
152
+ # unknown algorithm. Probably junk, remove it
153
+ remove req.http.Accept-Encoding;
154
+ }
155
+ }
156
+
157
+ # Set grace time, lookup in the cache
158
+ set req.grace = 1s;
159
+ return(lookup);
160
+ }
161
+
162
+ #
163
+ # Called when entering pipe mode
164
+ #
165
+ sub vcl_pipe {
166
+ # If we don't set the Connection: close header, any following
167
+ # requests from the client will also be piped through and
168
+ # left untouched by varnish. We don't want that.
169
+ set req.http.connection = "close";
170
+ return(pipe);
171
+ }
172
+
173
+
174
+ #
175
+ # Called when the requested object has been retrieved from the
176
+ # backend, or the request to the backend has failed
177
+ #
178
+ sub vcl_fetch {
179
+ esi;
180
+ # Do not cache the object if the backend application does not want us to.
181
+ if (beresp.http.Cache-Control ~ "(no-cache|no-store|private|must-revalidate)") {
182
+ return(pass);
183
+ }
184
+
185
+ # Do not cache the object if the status is not in the 200s
186
+ if (beresp.status >= 300) {
187
+ return(pass);
188
+ }
189
+
190
+ #
191
+ # Everything below here should be cached
192
+ #
193
+ if (req.url ~ "\/posts\/comments") {
194
+ set beresp.ttl = 0s;
195
+ }
196
+
197
+ # Remove the Set-Cookie header
198
+ remove beresp.http.Set-Cookie;
199
+
200
+ # Set the grace time
201
+ set beresp.grace = 1s;
202
+
203
+ if (req.url ~ "\.(css|js|jpg|jpeg|gif|ico|png)\??\d*$") {
204
+ /* Remove Expires from backend, it's not long enough */
205
+ unset beresp.http.expires;
206
+
207
+ /* Set the clients TTL on this object */
208
+ set beresp.http.cache-control = "public, max-age=31536000";
209
+
210
+ /* marker for vcl_deliver to reset Age: */
211
+ set beresp.http.magicmarker = "1";
212
+ } else {
213
+ set beresp.http.Cache-Control = "private, max-age=0, must-revalidate";
214
+ set beresp.http.Pragma = "no-cache";
215
+ }
216
+
217
+ # return(deliver); the object
218
+ return(deliver);
219
+ }
220
+
221
+ sub vcl_deliver {
222
+ if (resp.http.magicmarker) {
223
+ /* Remove the magic marker */
224
+ unset resp.http.magicmarker;
225
+
226
+ /* By definition we have a fresh object */
227
+ set resp.http.age = "0";
228
+ }
229
+
230
+ # Add a header to indicate a cache HIT/MISS
231
+ if (obj.hits > 0) {
232
+ set resp.http.X-Cache = "HIT";
233
+ } else {
234
+ set resp.http.X-Cache = "MISS";
235
+ }
236
+ }
237
+
238
+
59
239
 
60
240
  == Note on Patches/Pull Requests
61
241
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.4
1
+ 0.2.5
@@ -42,8 +42,8 @@ module Lacquer
42
42
  # These are the headers that varnish responds to
43
43
  # to set cache properly.
44
44
  def send_cache_control_headers
45
- if Lacquer.configuration.enable_cache
46
- expires_in(@cache_ttl, :public => true)
45
+ if Lacquer.configuration.enable_cache && @cache_ttl && @cache_ttl != 0
46
+ expires_in(@cache_ttl, :public => true, :private => false)
47
47
  end
48
48
  end
49
49
  end
@@ -23,7 +23,7 @@ module Lacquer
23
23
  def initialize
24
24
  @enable_cache = true
25
25
  @varnish_servers = []
26
- @default_ttl = 1.week
26
+ @default_ttl = 0
27
27
  @job_backend = :none
28
28
  @retries = 5
29
29
  @command_error_handler = nil
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{posterous-lacquer}
8
- s.version = "0.2.4"
8
+ s.version = "0.2.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Garry Tan", "Russ Smith"]
12
- s.date = %q{2010-09-03}
12
+ s.date = %q{2010-09-09}
13
13
  s.description = %q{Rails drop in for Varnish support.}
14
14
  s.email = %q{garry@posterous.com}
15
15
  s.extra_rdoc_files = [
@@ -44,7 +44,7 @@ Gem::Specification.new do |s|
44
44
  s.homepage = %q{http://github.com/russ/lacquer}
45
45
  s.rdoc_options = ["--charset=UTF-8"]
46
46
  s.require_paths = ["lib"]
47
- s.rubygems_version = %q{1.3.7}
47
+ s.rubygems_version = %q{1.3.5}
48
48
  s.summary = %q{Rails drop in for Varnish support.}
49
49
  s.test_files = [
50
50
  "spec/lacquer/cache_utils_spec.rb",
@@ -56,7 +56,7 @@ Gem::Specification.new do |s|
56
56
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
57
57
  s.specification_version = 3
58
58
 
59
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
59
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
60
60
  s.add_development_dependency(%q<rspec>, [">= 1.3.0"])
61
61
  else
62
62
  s.add_dependency(%q<rspec>, [">= 1.3.0"])
metadata CHANGED
@@ -1,13 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: posterous-lacquer
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 2
9
- - 4
10
- version: 0.2.4
4
+ version: 0.2.5
11
5
  platform: ruby
12
6
  authors:
13
7
  - Garry Tan
@@ -16,25 +10,19 @@ autorequire:
16
10
  bindir: bin
17
11
  cert_chain: []
18
12
 
19
- date: 2010-09-03 00:00:00 -07:00
13
+ date: 2010-09-09 00:00:00 -07:00
20
14
  default_executable:
21
15
  dependencies:
22
16
  - !ruby/object:Gem::Dependency
23
17
  name: rspec
24
- prerelease: false
25
- requirement: &id001 !ruby/object:Gem::Requirement
26
- none: false
18
+ type: :development
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
27
21
  requirements:
28
22
  - - ">="
29
23
  - !ruby/object:Gem::Version
30
- hash: 27
31
- segments:
32
- - 1
33
- - 3
34
- - 0
35
24
  version: 1.3.0
36
- type: :development
37
- version_requirements: *id001
25
+ version:
38
26
  description: Rails drop in for Varnish support.
39
27
  email: garry@posterous.com
40
28
  executables: []
@@ -78,27 +66,21 @@ rdoc_options:
78
66
  require_paths:
79
67
  - lib
80
68
  required_ruby_version: !ruby/object:Gem::Requirement
81
- none: false
82
69
  requirements:
83
70
  - - ">="
84
71
  - !ruby/object:Gem::Version
85
- hash: 3
86
- segments:
87
- - 0
88
72
  version: "0"
73
+ version:
89
74
  required_rubygems_version: !ruby/object:Gem::Requirement
90
- none: false
91
75
  requirements:
92
76
  - - ">="
93
77
  - !ruby/object:Gem::Version
94
- hash: 3
95
- segments:
96
- - 0
97
78
  version: "0"
79
+ version:
98
80
  requirements: []
99
81
 
100
82
  rubyforge_project:
101
- rubygems_version: 1.3.7
83
+ rubygems_version: 1.3.5
102
84
  signing_key:
103
85
  specification_version: 3
104
86
  summary: Rails drop in for Varnish support.