lawnchair 0.5.4 → 0.5.5

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -10,7 +10,7 @@ begin
10
10
  gem.email = "shanewolf@gmail.com"
11
11
  gem.homepage = "http://github.com/gizm0duck/lawnchair"
12
12
  gem.authors = ["Shane Wolf"]
13
- gem.add_dependency "redis"
13
+ gem.add_dependency "redis", ">=0.1.2"
14
14
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
15
  end
16
16
  Jeweler::GemcutterTasks.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.4
1
+ 0.5.5
data/lawnchair.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{lawnchair}
8
- s.version = "0.5.4"
8
+ s.version = "0.5.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Shane Wolf"]
12
- s.date = %q{2010-02-14}
12
+ s.date = %q{2010-02-17}
13
13
  s.description = %q{Fully featured caching mechanism for arbitrary pieces of resource expensive ruby code using Redis while being able to optionally store data in the Ruby process itself for maximum efficiency.}
14
14
  s.email = %q{shanewolf@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -60,12 +60,12 @@ Gem::Specification.new do |s|
60
60
  s.specification_version = 3
61
61
 
62
62
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
63
- s.add_runtime_dependency(%q<redis>, [">= 0"])
63
+ s.add_runtime_dependency(%q<redis>, [">= 0.1.2"])
64
64
  else
65
- s.add_dependency(%q<redis>, [">= 0"])
65
+ s.add_dependency(%q<redis>, [">= 0.1.2"])
66
66
  end
67
67
  else
68
- s.add_dependency(%q<redis>, [">= 0"])
68
+ s.add_dependency(%q<redis>, [">= 0.1.2"])
69
69
  end
70
70
  end
71
71
 
data/lib/lawnchair.rb CHANGED
@@ -25,6 +25,7 @@ module Lawnchair
25
25
 
26
26
  def connectdb(redis=nil)
27
27
  @redis = (redis || Redis.new(:db => 11))
28
+ Lawnchair::StorageEngine::Redis.verify_db_connection
28
29
  end
29
30
 
30
31
  def flushdb
@@ -8,14 +8,18 @@ module Lawnchair
8
8
  @data_store ||= {}
9
9
  end
10
10
 
11
- def fetch(key, options, &block)
12
- if exists?(key)
13
- value = get(key, options)
11
+ def fetch(key, options={}, &block)
12
+ if self.db_connection?
13
+ if exists?(key)
14
+ value = get(key, options)
15
+ else
16
+ value = block.call
17
+ set(key, value, options)
18
+ return value
19
+ end
14
20
  else
15
- value = block.call
16
- set(key, value, options)
21
+ block.call
17
22
  end
18
- value
19
23
  end
20
24
 
21
25
  def get(key, options={})
@@ -31,6 +35,10 @@ module Lawnchair
31
35
  prefix = "Lawnchair"
32
36
  "#{prefix}:#{key}"
33
37
  end
38
+
39
+ def db_connection?
40
+ true
41
+ end
34
42
  end
35
43
  end
36
44
  end
@@ -17,7 +17,6 @@ module Lawnchair
17
17
 
18
18
  def fetch(key, options, &block)
19
19
  raise "No Storage Engines Configured" if storage_engines.empty?
20
-
21
20
  value, index = find_in_storage(key, options)
22
21
  value ||= yield
23
22
  place_in_storage(key, value, options, index)
@@ -28,10 +27,15 @@ module Lawnchair
28
27
  def find_in_storage(key, options)
29
28
  value, index = nil, nil
30
29
  storage_engines.each_with_index do |storage_engine, i|
31
- if storage_engine.exists?(key)
32
- value = storage_engine.get(key, options)
30
+ if storage_engine.db_connection?
31
+ if storage_engine.exists?(key)
32
+ value = storage_engine.get(key, options)
33
+ index = i
34
+ break
35
+ end
36
+ else
37
+ value = nil
33
38
  index = i
34
- break
35
39
  end
36
40
  end
37
41
  return value, index
@@ -40,7 +44,9 @@ module Lawnchair
40
44
  def place_in_storage(key, value, options, index)
41
45
  storage_engines.each_with_index do |storage_engine, i|
42
46
  break if i == index
43
- storage_engine.set(key, value, options)
47
+ if storage_engine.db_connection?
48
+ storage_engine.set(key, value, options)
49
+ end
44
50
  end
45
51
  value
46
52
  end
@@ -7,6 +7,10 @@ module Lawnchair
7
7
  def data_store
8
8
  @@data_store
9
9
  end
10
+
11
+ def db_connection?
12
+ true
13
+ end
10
14
 
11
15
  def set(key, value, options={})
12
16
  if options[:raw]
@@ -2,6 +2,8 @@ module Lawnchair
2
2
  module StorageEngine
3
3
  class Redis < Abstract
4
4
  class << self
5
+ attr_reader :db_connection
6
+
5
7
  def data_store
6
8
  Lawnchair.redis
7
9
  end
@@ -22,6 +24,26 @@ module Lawnchair
22
24
  def expire!(key)
23
25
  data_store.del(computed_key(key))
24
26
  end
27
+
28
+ def connection_established!
29
+ verify_db_connection
30
+ end
31
+
32
+ def db_connection?
33
+ return @db_connection unless @db_connection.nil?
34
+ verify_db_connection
35
+ end
36
+
37
+ def verify_db_connection
38
+ begin
39
+ data_store.info
40
+ @db_connection = true
41
+ rescue Exception => e
42
+ @db_connection = false
43
+ ensure
44
+ return @db_connection
45
+ end
46
+ end
25
47
  end
26
48
  end
27
49
  end
data/spec/speed.rb CHANGED
@@ -26,7 +26,7 @@ end
26
26
  Benchmark.bm(7) do |x|
27
27
  x.report("cached:\t\t") do
28
28
  (1..n).each do |i|
29
- Lawnchair::Cache.me("redis_cache") do
29
+ Lawnchair.cache("redis_cache") do
30
30
  expensive_stuff
31
31
  end
32
32
  end
@@ -34,7 +34,7 @@ Benchmark.bm(7) do |x|
34
34
 
35
35
  x.report("in process cached:") do
36
36
  (1..n).each do |i|
37
- Lawnchair::Cache.me("in_process_cache", :in_process => true) do
37
+ Lawnchair.cache("in_process_cache", :in_process => true) do
38
38
  expensive_stuff
39
39
  end
40
40
  end
@@ -58,4 +58,10 @@ describe "Lawnchair::StorageEngine::InProcessStore" do
58
58
  in_process_store.data_store["Lawnchair:sim"].should == "ba"
59
59
  end
60
60
  end
61
+
62
+ describe "#db_connection?" do
63
+ it "should return true" do
64
+ in_process_store.should be_db_connection
65
+ end
66
+ end
61
67
  end
@@ -19,10 +19,10 @@ describe "Lawnchair::StorageEngine::RedisStore" do
19
19
  redis_store.set("mu", "fasa")
20
20
  Lawnchair.redis.ttl("Lawnchair:mu").should == 3600 # seconds
21
21
  end
22
-
22
+
23
23
  it "allows you to override the default ttl" do
24
24
  redis_store.set("mu", "fasa", :expires_in => 600)
25
-
25
+
26
26
  Lawnchair.redis.ttl("Lawnchair:mu").should == 600 # seconds
27
27
  end
28
28
 
@@ -51,7 +51,7 @@ describe "Lawnchair::StorageEngine::RedisStore" do
51
51
  Lawnchair.redis.keys('*').should_not include("Lawnchair:mu")
52
52
  redis_store.exists?("mu").should be_false
53
53
  end
54
-
54
+
55
55
  it "returns true when the key exists" do
56
56
  Lawnchair.redis["Lawnchair:mu"] = "fasa"
57
57
  Lawnchair.redis.keys('*').should include("Lawnchair:mu")
@@ -63,10 +63,64 @@ describe "Lawnchair::StorageEngine::RedisStore" do
63
63
  it "should only expire the key specified" do
64
64
  Lawnchair.redis["Lawnchair:mu"] = "fasa"
65
65
  Lawnchair.redis["Lawnchair:sim"] = "ba"
66
-
66
+
67
67
  redis_store.expire!("mu")
68
68
  Lawnchair.redis["Lawnchair:mu"].should be_nil
69
69
  Lawnchair.redis["Lawnchair:sim"].should == "ba"
70
70
  end
71
71
  end
72
+
73
+ describe "db_connection?" do
74
+ before do
75
+ redis_store.stub!(:verify_db_connection).and_return(true)
76
+ end
77
+
78
+ context "when db_connection has been set" do
79
+ it "returns the value in db_connection" do
80
+ redis_store.instance_variable_set("@db_connection", false)
81
+ redis_store.db_connection?.should be_false
82
+ end
83
+ end
84
+
85
+ context "when db_connection has NOT been set" do
86
+ it "returns the result from verify_db_connection" do
87
+ redis_store.instance_variable_set("@db_connection", nil)
88
+ redis_store.db_connection?.should == redis_store.verify_db_connection
89
+ end
90
+ end
91
+ end
92
+
93
+ describe ".verify_db_connection" do
94
+ context "when we have established a connection to redis-server" do
95
+ it "should return true" do
96
+ lambda {redis_store.data_store}.should_not raise_error
97
+ redis_store.db_connection?.should be_true
98
+ end
99
+ end
100
+
101
+ context "when we have NOT established a connection to redis-server" do
102
+ before do
103
+ data_store = mock(:data_store)
104
+ data_store.stub!(:info).and_raise("BOOM!")
105
+ redis_store.stub!(:data_store).and_return(data_store)
106
+ end
107
+
108
+ it "returns false" do
109
+ redis_store.verify_db_connection
110
+ redis_store.db_connection?.should be_false
111
+ end
112
+ end
113
+ end
114
+
115
+ describe ".connection_established!" do
116
+ before do
117
+ redis_store.instance_variable_set("@db_connection", nil)
118
+ redis_store.db_connection.should be_nil
119
+ end
120
+
121
+ it "re-verifies the connection" do
122
+ redis_store.connection_established!
123
+ redis_store.db_connection.should be_true
124
+ end
125
+ end
72
126
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lawnchair
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.4
4
+ version: 0.5.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane Wolf
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-14 00:00:00 -08:00
12
+ date: 2010-02-17 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: "0"
23
+ version: 0.1.2
24
24
  version:
25
25
  description: Fully featured caching mechanism for arbitrary pieces of resource expensive ruby code using Redis while being able to optionally store data in the Ruby process itself for maximum efficiency.
26
26
  email: shanewolf@gmail.com