lacquer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .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
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Russ Smith
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,21 @@
1
+ = lacquer
2
+
3
+ Rails drop in for Varnish support.
4
+
5
+ == TODO
6
+ * Add DelayedJob/Resque support.
7
+ * Add helpful install generators.
8
+
9
+ == Note on Patches/Pull Requests
10
+
11
+ * Fork the project.
12
+ * Make your feature addition or bug fix.
13
+ * Add tests for it. This is important so I don't break it in a
14
+ future version unintentionally.
15
+ * Commit, do not mess with rakefile, version, or history.
16
+ (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)
17
+ * Send me a pull request. Bonus points for topic branches.
18
+
19
+ == Copyright
20
+
21
+ Copyright (c) 2010 Russ Smith. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "lacquer"
8
+ gem.summary = %Q{Rails drop in for Varnish support.}
9
+ gem.description = %Q{Rails drop in for Varnish support.}
10
+ gem.email = "russ@bashme.org"
11
+ gem.homepage = "http://github.com/russ/lacquer"
12
+ gem.authors = ["Russ Smith"]
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/test_*.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
+
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "lacquer #{version}"
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'lib', 'lacquer')
data/lacquer.gemspec ADDED
@@ -0,0 +1,61 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{lacquer}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Russ Smith"]
12
+ s.date = %q{2010-06-11}
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
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "init.rb",
27
+ "lacquer.gemspec",
28
+ "lib/generators/lacquer_generator.rb",
29
+ "lib/generators/templates/initializer.rb",
30
+ "lib/generators/templates/varnish.sample.vcl",
31
+ "lib/lacquer.rb",
32
+ "lib/lacquer/cache_utils.rb",
33
+ "lib/lacquer/configuration.rb",
34
+ "lib/lacquer/varnish_interface.rb",
35
+ "rails/init.rb",
36
+ "test/helper.rb",
37
+ "test/test_cache_utils.rb",
38
+ "test/test_varnish_interface.rb"
39
+ ]
40
+ s.homepage = %q{http://github.com/russ/lacquer}
41
+ s.rdoc_options = ["--charset=UTF-8"]
42
+ s.require_paths = ["lib"]
43
+ s.rubygems_version = %q{1.3.6}
44
+ s.summary = %q{Rails drop in for Varnish support.}
45
+ s.test_files = [
46
+ "test/helper.rb",
47
+ "test/test_cache_utils.rb",
48
+ "test/test_varnish_interface.rb"
49
+ ]
50
+
51
+ if s.respond_to? :specification_version then
52
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
53
+ s.specification_version = 3
54
+
55
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
56
+ else
57
+ end
58
+ else
59
+ end
60
+ end
61
+
@@ -0,0 +1,12 @@
1
+ require 'rails/generators'
2
+
3
+ class LacquerGenerator < Rails::Generators::Base
4
+ def self.source_root
5
+ File.join(File.dirname(__FILE__), 'templates')
6
+ end
7
+
8
+ def install_lacquer
9
+ copy_file('varnish.sample.vcl', 'config/varnish.sample.vcl')
10
+ copy_file('initializer.rb', 'config/initializers/lacquer.rb')
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ Lacquer.configure do |config|
2
+ config.enable_cache = true
3
+ config.default_ttl = 1.week
4
+ config.varnish_servers << { :host => '0.0.0.0', :port => 6082 }
5
+ end
@@ -0,0 +1,117 @@
1
+ backend default {
2
+ .host = "0.0.0.0";
3
+ .port = "10000";
4
+ }
5
+
6
+
7
+ #
8
+ # Handling of requests that are received from clients.
9
+ # First decide whether or not to lookup data in the cache.
10
+ #
11
+ sub vcl_recv {
12
+ # Pipe requests that are non-RFC2616 or CONNECT which is weird.
13
+ if (req.request != "GET" &&
14
+ req.request != "HEAD" &&
15
+ req.request != "PUT" &&
16
+ req.request != "POST" &&
17
+ req.request != "TRACE" &&
18
+ req.request != "OPTIONS" &&
19
+ req.request != "DELETE") {
20
+ pipe;
21
+ }
22
+
23
+ # Pass requests that are not GET or HEAD
24
+ if (req.request != "GET" && req.request != "HEAD") {
25
+ pass;
26
+ }
27
+
28
+ # Pass requests that we know we aren't caching
29
+ if (req.url ~ "^/admin") {
30
+ pass;
31
+ }
32
+
33
+ #
34
+ # Everything below here should be cached
35
+ #
36
+
37
+ # Handle compression correctly. Varnish treats headers literally, not
38
+ # semantically. So it is very well possible that there are cache misses
39
+ # because the headers sent by different browsers aren't the same.
40
+ # @see: http://varnish.projects.linpro.no/wiki/FAQ/Compression
41
+ if (req.http.Accept-Encoding) {
42
+ if (req.http.Accept-Encoding ~ "gzip") {
43
+ # if the browser supports it, we'll use gzip
44
+ set req.http.Accept-Encoding = "gzip";
45
+ } elsif (req.http.Accept-Encoding ~ "deflate") {
46
+ # next, try deflate if it is supported
47
+ set req.http.Accept-Encoding = "deflate";
48
+ } else {
49
+ # unknown algorithm. Probably junk, remove it
50
+ remove req.http.Accept-Encoding;
51
+ }
52
+ }
53
+
54
+ # Clear cookie and authorization headers, set grace time, lookup in the cache
55
+ unset req.http.Cookie;
56
+ unset req.http.Authorization;
57
+ set req.grace = 30s;
58
+ lookup;
59
+ }
60
+
61
+ #
62
+ # Called when entering pipe mode
63
+ #
64
+ sub vcl_pipe {
65
+ # If we don't set the Connection: close header, any following
66
+ # requests from the client will also be piped through and
67
+ # left untouched by varnish. We don't want that.
68
+ set req.http.connection = "close";
69
+ pipe;
70
+ }
71
+
72
+ #
73
+ # Called when the requested object has been retrieved from the
74
+ # backend, or the request to the backend has failed
75
+ #
76
+ sub vcl_fetch {
77
+ # Do not cache the object if the backend application does not want us to.
78
+ if (obj.http.Cache-Control ~ "(no-cache|no-store|private|must-revalidate)") {
79
+ pass;
80
+ }
81
+
82
+ # Do not cache the object if the status is not in the 200s
83
+ if (obj.status >= 300) {
84
+ # Remove the Set-Cookie header
85
+ remove obj.http.Set-Cookie;
86
+ pass;
87
+ }
88
+
89
+ #
90
+ # Everything below here should be cached
91
+ #
92
+
93
+ # Remove the Set-Cookie header
94
+ remove obj.http.Set-Cookie;
95
+
96
+ # Set the grace time
97
+ set obj.grace = 30s;
98
+
99
+ # Deliver the object
100
+ deliver;
101
+ }
102
+
103
+ #
104
+ # Called before the response is sent back to the client
105
+ #
106
+ sub vcl_deliver {
107
+ # Force browsers and intermediary caches to always check back with us
108
+ set resp.http.Cache-Control = "private, max-age=0, must-revalidate";
109
+ set resp.http.Pragma = "no-cache";
110
+
111
+ # Add a header to indicate a cache HIT/MISS
112
+ if (obj.hits > 0) {
113
+ set resp.http.X-Cache = "HIT";
114
+ } else {
115
+ set resp.http.X-Cache = "MISS";
116
+ }
117
+ }
@@ -0,0 +1,41 @@
1
+ module Lacquer
2
+ module CacheUtils
3
+ def self.included(base)
4
+ base.class_eval do
5
+ attr_reader :cache_ttl
6
+
7
+ before_filter :set_default_cache_ttl
8
+ after_filter :send_cache_control_headers
9
+ end
10
+ end
11
+
12
+ # Instance variable for the action ttl.
13
+ def set_cache_ttl(ttl)
14
+ @cache_ttl = ttl
15
+ end
16
+
17
+ # Called as a before filter to set default ttl
18
+ # for the entire application.
19
+ def set_default_cache_ttl
20
+ set_cache_ttl(Lacquer.configuration.default_ttl)
21
+ end
22
+
23
+ # Sends url.purge command to varnish to clear cache.
24
+ #
25
+ # clear_cache_for(root_path, blog_posts_path, '/other/content/*')
26
+ def clear_cache_for(*paths)
27
+ paths.each do |path|
28
+ VarnishInterface.send_command('url.purge ' << path)
29
+ end
30
+ end
31
+
32
+ # Sends cache control headers with page.
33
+ # These are the headers that varnish responds to
34
+ # to set cache properly.
35
+ def send_cache_control_headers
36
+ if Lacquer.configuration.enable_cache
37
+ expires_in(@cache_ttl, :public => true)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,27 @@
1
+ module Lacquer
2
+ class Configuration
3
+ OPTIONS = [ :varnish_servers ]
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
+ def initialize
15
+ @enable_cache = true
16
+ @varnish_servers = []
17
+ @default_ttl = 1.week
18
+ end
19
+
20
+ # Returns a hash of all configurable options
21
+ def to_hash
22
+ OPTIONS.inject({}) do |hash, option|
23
+ hash.merge(option.to_sym => send(option))
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,17 @@
1
+ module Lacquer
2
+ class VarnishInterface
3
+ def self.send_command(command)
4
+ Lacquer.configuration.varnish_servers.each do |server|
5
+ begin
6
+ connection = Net::Telnet.new(
7
+ 'Host' => server[:host],
8
+ 'Port' => server[:port],
9
+ 'Timeout' => server[:timeout] || 5)
10
+ connection.puts(command)
11
+ rescue Exception => e
12
+ raise VarnishError.new("Error while trying to connect to #{server[:host]}:#{server[:port]} #{e}")
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
data/lib/lacquer.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'net/telnet'
2
+ require 'active_support/time'
3
+
4
+ require 'lacquer/configuration'
5
+ require 'lacquer/cache_utils'
6
+ require 'lacquer/varnish_interface'
7
+
8
+ module Lacquer
9
+ class VarnishError < Exception; end
10
+
11
+ class << self
12
+ attr_accessor :configuration
13
+
14
+ # Call this method to modify defaults in your initailizers.
15
+ #
16
+ # Lacquer.configure do |config|
17
+ # config.varnish_servers << { :host => '0.0.0.0', :port => 6082, :timeout => 5 }
18
+ # end
19
+ def configure
20
+ self.configuration ||= Configuration.new
21
+ yield(configuration)
22
+ end
23
+ end
24
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ puts "testing this here..."
2
+
3
+ require 'lacquer'
data/test/helper.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'test/unit'
4
+ require 'shoulda'
5
+
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
+ require 'lacquer'
9
+
10
+ class Test::Unit::TestCase
11
+ end
@@ -0,0 +1,36 @@
1
+ require 'helper'
2
+
3
+ class ControllerClass
4
+ def self.before_filter(arg); end
5
+ def self.after_filter(arg); end
6
+
7
+ include Lacquer::CacheUtils
8
+ end
9
+
10
+ class TestLacquer < ActiveSupport::TestCase
11
+ setup do
12
+ @controller = ControllerClass.new
13
+ end
14
+
15
+ should "take paths to clear cache for" do
16
+ Lacquer::VarnishInterface.expects(:send_command).twice
17
+ @controller.clear_cache_for('/', '/blog/posts')
18
+ end
19
+
20
+ context "when cache is enabled" do
21
+ should "send cache control headers based on ttl" do
22
+ Lacquer.configuration.enable_cache = true
23
+ @controller.set_cache_ttl(10.week)
24
+ @controller.expects(:expires_in).with(10.week, :public => true)
25
+ @controller.send_cache_control_headers
26
+ end
27
+ end
28
+
29
+ context "when cache is disabled" do
30
+ should "do not send cache control headers" do
31
+ Lacquer.configuration.enable_cache = false
32
+ @controller.expects(:expires_in).never
33
+ @controller.send_cache_control_headers
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,30 @@
1
+ require 'helper'
2
+
3
+ class TestLacquer < ActiveSupport::TestCase
4
+ setup do
5
+ Lacquer.configure do |config|
6
+ config.varnish_servers << { :host => '0.0.0.0', :port => 6082 }
7
+ end
8
+
9
+ @telnet_mock = mock('Net::Telnet')
10
+ @telnet_mock.stubs(:puts)
11
+
12
+ @controller = ControllerClass.new
13
+ end
14
+
15
+ context "when connection is succesful" do
16
+ should "send command to varnish server" do
17
+ Net::Telnet.stubs(:new).returns(@telnet_mock)
18
+ Lacquer::VarnishInterface.send_command('url.purge /')
19
+ end
20
+ end
21
+
22
+ context "when connection is unsuccesful" do
23
+ should "raise timeout exception" do
24
+ Net::Telnet.stubs(:new).raises(Timeout::Error)
25
+ assert_raise Lacquer::VarnishError do
26
+ Lacquer::VarnishInterface.send_command('url.purge /')
27
+ end
28
+ end
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lacquer
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Russ Smith
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-06-11 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Rails drop in for Varnish support.
22
+ email: russ@bashme.org
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - LICENSE
29
+ - README.rdoc
30
+ files:
31
+ - .document
32
+ - .gitignore
33
+ - LICENSE
34
+ - README.rdoc
35
+ - Rakefile
36
+ - VERSION
37
+ - init.rb
38
+ - lacquer.gemspec
39
+ - lib/generators/lacquer_generator.rb
40
+ - lib/generators/templates/initializer.rb
41
+ - lib/generators/templates/varnish.sample.vcl
42
+ - lib/lacquer.rb
43
+ - lib/lacquer/cache_utils.rb
44
+ - lib/lacquer/configuration.rb
45
+ - lib/lacquer/varnish_interface.rb
46
+ - rails/init.rb
47
+ - test/helper.rb
48
+ - test/test_cache_utils.rb
49
+ - test/test_varnish_interface.rb
50
+ has_rdoc: true
51
+ homepage: http://github.com/russ/lacquer
52
+ licenses: []
53
+
54
+ post_install_message:
55
+ rdoc_options:
56
+ - --charset=UTF-8
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ requirements: []
74
+
75
+ rubyforge_project:
76
+ rubygems_version: 1.3.6
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: Rails drop in for Varnish support.
80
+ test_files:
81
+ - test/helper.rb
82
+ - test/test_cache_utils.rb
83
+ - test/test_varnish_interface.rb