Skip to content

Commit

Permalink
Adding to_vcl_pass_urls.
Browse files Browse the repository at this point in the history
  • Loading branch information
russ committed May 26, 2011
1 parent 1fd86b5 commit 0e53384
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .bundle/config
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
BUNDLE_DISABLE_SHARED_GEMS: "1"
--- {}

15 changes: 11 additions & 4 deletions lib/lacquer/cache_control.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize
def register(group, options = {})
options[:group] = group
options[:args] = Array(options[:args]).compact
store << options
store << options
end

def configure
Expand Down Expand Up @@ -46,16 +46,23 @@ def to_vcl_override_ttl_urls
CODE
end.join("\n")
end

def to_vcl_pass_urls
<<-CODE.strip_heredoc
if(#{to_vcl_conditions(urls_by(:pass))}) {
return(pass);
}
CODE
end

protected
protected

def urls_grouped_by_expires
store.group_by { |opt| opt[:expires_in] }.select { |expires_in, list| expires_in }
end

def urls_by(group)
store.select { |opt| opt[:group] == group }
store.select { |opt| opt[:group] == group }
end

end
end
22 changes: 8 additions & 14 deletions spec/lacquer/cache_control_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,25 @@
require 'lacquer/cache_control'

describe Lacquer::CacheControl do

describe "#register" do

it "persists cache settings for url" do
cache_control = described_class.new
cache_control.register :class_section, :url => "^/sv/class_sections/%s.*$", :args => "[0-9]+"
cache_control.store.first[:group].should == :class_section
cache_control.store.first[:url].should == "^/sv/class_sections/%s.*$"
cache_control.store.first[:args].should == ["[0-9]+"]
end

end

describe "#urls_for" do

it "returns urls to expire for object" do
cache_control = described_class.new
cache_control.register :class_section, :url => "^/sv/class_sections/%s.*$", :args => "[0-9]+"
cache_control.urls_for(:class_section, mock("ClassSection", :to_param => 1)).should == ["^/sv/class_sections/1.*$"]
end

end

context "vcl" do

it "returns all urls as vcl conditions" do
cache_control = described_class.new
cache_control.register :class_section, :url => "^/sv/class_sections/%s.*$", :args => "[0-9]+"
Expand All @@ -37,12 +31,14 @@
conditions.should include("||")
conditions.should include("req.url ~ \"^/sv/info_screens/[0-9]+.*$\"")
end

# if (req.url ~ "^/images" || req.url ~ "^/stylesheets" || req.url ~ "^/javascripts") {
# unset beresp.http.Set-Cookie;
# set beresp.ttl = 7d;
# return(deliver);
# }

it "returns vcl for pass urls" do
cache_control = described_class.new
cache_control.register :pass, :url => "^/admin"
pass_urls = cache_control.to_vcl_pass_urls
pass_urls.should include('if(req.url ~ "^/admin")')
pass_urls.should include('return(pass)')
end

it "returns vcl for override ttl on beresp" do
cache_control = described_class.new
Expand All @@ -66,7 +62,5 @@
override_ttl.should include('set beresp.ttl = 2d')
override_ttl.should_not include('info_screen')
end

end

end

0 comments on commit 0e53384

Please sign in to comment.