lacquer 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.2.2
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{lacquer}
8
- s.version = "0.2.1"
8
+ s.version = "0.2.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Russ Smith"]
12
- s.date = %q{2010-06-12}
12
+ s.date = %q{2010-06-14}
13
13
  s.description = %q{Rails drop in for Varnish support.}
14
14
  s.email = %q{russ@bashme.org}
15
15
  s.extra_rdoc_files = [
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
25
25
  "VERSION",
26
26
  "init.rb",
27
27
  "lacquer.gemspec",
28
+ "lib/generators/USAGE",
28
29
  "lib/generators/lacquer_generator.rb",
29
30
  "lib/generators/templates/initializer.rb",
30
31
  "lib/generators/templates/varnish.sample.vcl",
@@ -33,7 +34,7 @@ Gem::Specification.new do |s|
33
34
  "lib/lacquer/configuration.rb",
34
35
  "lib/lacquer/delayed_job_job.rb",
35
36
  "lib/lacquer/resque_job.rb",
36
- "lib/lacquer/varnish_interface.rb",
37
+ "lib/lacquer/varnish.rb",
37
38
  "rails/init.rb",
38
39
  "test/helper.rb",
39
40
  "test/test_cache_utils.rb",
@@ -0,0 +1,5 @@
1
+ Description:
2
+ Installs Lacquer into your Rails application.
3
+
4
+ Example:
5
+ rails generate lacquer
@@ -1,11 +1,9 @@
1
1
  require 'rails/generators'
2
2
 
3
- class LacquerGenerator < Rails::Generators::Base
4
- def self.source_root
5
- File.join(File.dirname(__FILE__), 'templates')
6
- end
3
+ class LacquerGenerator < Rails::Generators::NamedBase
4
+ source_root File.expand_path('../templates', __FILE__)
7
5
 
8
- def install_lacquer
6
+ def install
9
7
  copy_file('varnish.sample.vcl', 'config/varnish.sample.vcl')
10
8
  copy_file('initializer.rb', 'config/initializers/lacquer.rb')
11
9
  end
@@ -1,6 +1,15 @@
1
1
  Lacquer.configure do |config|
2
+ # Globally enable/disable cache
2
3
  config.enable_cache = true
4
+
5
+ # Unless overridden in a controller or action, the default will be used
3
6
  config.default_ttl = 1.week
7
+
8
+ # Can be :none, :delayed_job, :resque
4
9
  config.job_backend = :none
5
- config.varnish_servers << { :host => '0.0.0.0', :port => 6082 }
10
+
11
+ # Array of Varnish servers to manage
12
+ config.varnish_servers << {
13
+ :host => '0.0.0.0', :port => 6082
14
+ }
6
15
  end
@@ -4,7 +4,7 @@ require 'active_support/core_ext'
4
4
 
5
5
  require 'lacquer/configuration'
6
6
  require 'lacquer/cache_utils'
7
- require 'lacquer/varnish_interface'
7
+ require 'lacquer/varnish'
8
8
 
9
9
  module Lacquer
10
10
  class VarnishError < Exception; end
@@ -28,12 +28,12 @@ module Lacquer
28
28
  case Lacquer.configuration.job_backend
29
29
  when :delayed_job
30
30
  require 'lacquer/delayed_job_job'
31
- Delayed::Job.enqueue(DelayedJobJob.new('url.purge ' << path))
31
+ Delayed::Job.enqueue(Lacquer::DelayedJobJob.new('url.purge ' << path))
32
32
  when :resque
33
33
  require 'lacquer/resque_job'
34
- Resque.enqueue(ResqueJob, 'url.purge ' << path)
34
+ Resque.enqueue(Lacquer::ResqueJob, 'url.purge ' << path)
35
35
  when :none
36
- VarnishInterface.send_command('url.purge ' << path)
36
+ Varnish.new.purge('url.purge ' << path)
37
37
  end
38
38
  end
39
39
  end
@@ -1,5 +1,7 @@
1
- class DelayedJobJob < Struct.new(:command)
2
- def perform
3
- VarnishInterface.send_command(command)
1
+ module Lacquer
2
+ class DelayedJobJob < Struct.new(:command)
3
+ def perform
4
+ Varnish.new.purge(command)
5
+ end
4
6
  end
5
7
  end
@@ -1,7 +1,9 @@
1
- class ResqueJob
2
- @queue = :lacquer
3
-
4
- def self.perform(command)
5
- VarnishInterface.send_command(command)
1
+ module Lacquer
2
+ class ResqueJob
3
+ @queue = :lacquer
4
+
5
+ def self.perform(command)
6
+ VarnishInterface.new.purge(command)
7
+ end
6
8
  end
7
9
  end
@@ -1,14 +1,32 @@
1
1
  module Lacquer
2
- class VarnishInterface
2
+ class Varnish
3
+ def stats
4
+ stats = send_command('stats').split("\n")
5
+ stats.shift
6
+
7
+ stats = stats.collect do |stat|
8
+ stat = stat.strip.match(/(\d+)\s+(.+)$/)
9
+ { :key => stat[2], :value => stat[1] }
10
+ end
11
+ end
12
+
13
+ def purge(path)
14
+ (send_command('url.purge ' << path) == '200 0') ? true : false
15
+ end
16
+
17
+ private
18
+
3
19
  # Sends commands over telnet to varnish servers listed in the config.
4
- def self.send_command(command)
20
+ def send_command(command)
5
21
  Lacquer.configuration.varnish_servers.each do |server|
6
22
  begin
7
23
  connection = Net::Telnet.new(
8
24
  'Host' => server[:host],
9
25
  'Port' => server[:port],
10
26
  'Timeout' => server[:timeout] || 5)
11
- connection.puts(command)
27
+ connection.cmd(command) do |c|
28
+ return c.strip
29
+ end
12
30
  rescue Exception => e
13
31
  raise VarnishError.new("Error while trying to connect to #{server[:host]}:#{server[:port]} #{e}")
14
32
  end
@@ -7,8 +7,10 @@ class TestLacquer < ActiveSupport::TestCase
7
7
 
8
8
  context "when job backend is :none" do
9
9
  should "take paths to clear cache for" do
10
+ varnish_stub = mock('Varnish')
11
+ Lacquer::Varnish.stubs('new').returns(varnish_stub)
12
+ varnish_stub.expects(:purge).twice
10
13
  Lacquer.configuration.job_backend = :none
11
- Lacquer::VarnishInterface.expects(:send_command).twice
12
14
  @controller.clear_cache_for('/', '/blog/posts')
13
15
  end
14
16
  end
@@ -15,7 +15,7 @@ class TestLacquer < ActiveSupport::TestCase
15
15
  context "when connection is succesful" do
16
16
  should "send command to varnish server" do
17
17
  Net::Telnet.stubs(:new).returns(@telnet_mock)
18
- Lacquer::VarnishInterface.send_command('url.purge /')
18
+ Lacquer::Varnish.new.purge('/')
19
19
  end
20
20
  end
21
21
 
@@ -23,7 +23,7 @@ class TestLacquer < ActiveSupport::TestCase
23
23
  should "raise timeout exception" do
24
24
  Net::Telnet.stubs(:new).raises(Timeout::Error)
25
25
  assert_raise Lacquer::VarnishError do
26
- Lacquer::VarnishInterface.send_command('url.purge /')
26
+ Lacquer::Varnish.new.purge('/')
27
27
  end
28
28
  end
29
29
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 1
9
- version: 0.2.1
8
+ - 2
9
+ version: 0.2.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Russ Smith
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-06-12 00:00:00 -07:00
17
+ date: 2010-06-14 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -36,6 +36,7 @@ files:
36
36
  - VERSION
37
37
  - init.rb
38
38
  - lacquer.gemspec
39
+ - lib/generators/USAGE
39
40
  - lib/generators/lacquer_generator.rb
40
41
  - lib/generators/templates/initializer.rb
41
42
  - lib/generators/templates/varnish.sample.vcl
@@ -44,7 +45,7 @@ files:
44
45
  - lib/lacquer/configuration.rb
45
46
  - lib/lacquer/delayed_job_job.rb
46
47
  - lib/lacquer/resque_job.rb
47
- - lib/lacquer/varnish_interface.rb
48
+ - lib/lacquer/varnish.rb
48
49
  - rails/init.rb
49
50
  - test/helper.rb
50
51
  - test/test_cache_utils.rb