lawnchair 0.6.2 → 0.6.4

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -11,6 +11,7 @@ begin
11
11
  gem.homepage = "http://github.com/gizm0duck/lawnchair"
12
12
  gem.authors = ["Shane Wolf"]
13
13
  gem.add_dependency "redis", ">=0.1.2"
14
+ gem.add_dependency "activesupport", ">=2.3.2"
14
15
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
16
  end
16
17
  Jeweler::GemcutterTasks.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.2
1
+ 0.6.4
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.6.2"
8
+ s.version = "0.6.4"
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-06-03}
12
+ s.date = %q{2010-07-13}
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 = [
@@ -23,6 +23,7 @@ Gem::Specification.new do |s|
23
23
  "Rakefile",
24
24
  "VERSION",
25
25
  "lawnchair.gemspec",
26
+ "lib/active_record_extension.rb",
26
27
  "lib/lawnchair.rb",
27
28
  "lib/marshal_extension.rb",
28
29
  "lib/storage_engine/abstract.rb",
@@ -43,7 +44,7 @@ Gem::Specification.new do |s|
43
44
  s.homepage = %q{http://github.com/gizm0duck/lawnchair}
44
45
  s.rdoc_options = ["--charset=UTF-8"]
45
46
  s.require_paths = ["lib"]
46
- s.rubygems_version = %q{1.3.5}
47
+ s.rubygems_version = %q{1.3.7}
47
48
  s.summary = %q{Speed up your app by caching expensive code in Redis or in the ruby process itself}
48
49
  s.test_files = [
49
50
  "spec/lawnchair_spec.rb",
@@ -59,13 +60,16 @@ Gem::Specification.new do |s|
59
60
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
60
61
  s.specification_version = 3
61
62
 
62
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
63
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
63
64
  s.add_runtime_dependency(%q<redis>, [">= 0.1.2"])
65
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.3.2"])
64
66
  else
65
67
  s.add_dependency(%q<redis>, [">= 0.1.2"])
68
+ s.add_dependency(%q<activesupport>, [">= 2.3.2"])
66
69
  end
67
70
  else
68
71
  s.add_dependency(%q<redis>, [">= 0.1.2"])
72
+ s.add_dependency(%q<activesupport>, [">= 2.3.2"])
69
73
  end
70
74
  end
71
75
 
@@ -0,0 +1,31 @@
1
+ require 'activesupport'
2
+ require 'activerecord'
3
+ module ActiveRecord
4
+ module LawnchairExtension
5
+
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+ def lawnchair_cache(method, options={})
12
+ self.class_eval %{
13
+ def #{method}_with_lawnchair(*args)
14
+ ident = lambda { |obj| obj.class.respond_to?(:primary_key) ? obj.send(obj.class.primary_key) : obj.to_s }
15
+ arg_keys = args.map(&ident).join(':')
16
+ key = "#\{self.class.name\}:#{method}:#\{ident.call(self)\}:#\{arg_keys\}"
17
+ Lawnchair.cache(key, #{options.inspect}) do
18
+ self.#{method}_without_lawnchair(*args)
19
+ end
20
+ end
21
+ }
22
+
23
+ alias_method_chain method, :lawnchair
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ ActiveRecord::Base.class_eval do
30
+ include ActiveRecord::LawnchairExtension
31
+ end
data/lib/lawnchair.rb CHANGED
@@ -6,6 +6,7 @@ require 'storage_engine/in_process'
6
6
  require 'storage_engine/composite'
7
7
 
8
8
  if defined? RAILS_ENV
9
+ require 'active_record_extension'
9
10
  require 'marshal_extension' if RAILS_ENV =~ /development/
10
11
  require 'view/helper'
11
12
  end
@@ -40,4 +41,5 @@ module Lawnchair
40
41
  Lawnchair.cache(key, options, &block)
41
42
  end
42
43
  end
43
- end
44
+ end
45
+
@@ -10,11 +10,15 @@ module Lawnchair
10
10
 
11
11
  def fetch(key, options={}, &block)
12
12
  if self.db_connection?
13
+ start_time = Time.now
13
14
  if exists?(key)
14
15
  value = get(key, options)
16
+ log("HIT", key, Time.now-start_time)
17
+ return value
15
18
  else
16
19
  value = block.call
17
20
  set(key, value, options)
21
+ log("MISS", key, Time.now-start_time)
18
22
  return value
19
23
  end
20
24
  else
@@ -40,6 +44,10 @@ module Lawnchair
40
44
  def db_connection?
41
45
  true
42
46
  end
47
+
48
+ def log(message, key, elapsed)
49
+ ActionController::Base.logger.info("Lawnchair Cache: #{message} (%0.6f secs): #{key}" % elapsed) if defined? ::ActionController::Base
50
+ end
43
51
  end
44
52
  end
45
53
  end
@@ -25,7 +25,9 @@ module Lawnchair
25
25
  end
26
26
 
27
27
  def expire!(key)
28
+ start_time = Time.now
28
29
  data_store.delete(computed_key(key))
30
+ log("EXPIRATION", key, Time.now-start_time)
29
31
  end
30
32
  end
31
33
  end
@@ -21,7 +21,9 @@ module Lawnchair
21
21
  end
22
22
 
23
23
  def expire!(key)
24
+ start_time = Time.now
24
25
  data_store.del(computed_key(key))
26
+ log("EXPIRATION", key, Time.now-start_time)
25
27
  end
26
28
 
27
29
  def connection_established!
@@ -32,7 +34,7 @@ module Lawnchair
32
34
  return @db_connection unless @db_connection.nil?
33
35
  verify_db_connection
34
36
  end
35
-
37
+
36
38
  def verify_db_connection
37
39
  begin
38
40
  data_store.info
@@ -1,5 +1,51 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
+ class Fertilize
4
+ attr_reader :size
5
+
6
+ def initialize(size, key)
7
+ @size = size
8
+ @key = key
9
+ end
10
+
11
+ def self.primary_key
12
+ :spread
13
+ end
14
+
15
+ def spread
16
+ @key
17
+ end
18
+ end
19
+
20
+ class Grass
21
+ include ActiveRecord::LawnchairExtension
22
+ attr_reader :length
23
+
24
+ def initialize
25
+ @length = 10
26
+ end
27
+
28
+ def kill
29
+ @length = 0
30
+ end
31
+
32
+ def mow
33
+ @length -= 1
34
+ end
35
+
36
+ def cut(how_much)
37
+ @length -= how_much
38
+ end
39
+
40
+ def weed(puff, *args)
41
+ @length -= puff.size
42
+ end
43
+
44
+ lawnchair_cache :mow
45
+ lawnchair_cache :cut
46
+ lawnchair_cache :weed, :expires_in => 0
47
+ end
48
+
3
49
  describe "Lawnchair::Cache" do
4
50
  describe ".me" do
5
51
  it "returns the value if it exists" do
@@ -26,5 +72,87 @@ describe "Lawnchair::Cache" do
26
72
  Lawnchair.cache("mu", :in_process => true, :raw => true) { "fasa" }
27
73
  end
28
74
  end
75
+
76
+ describe "caching a method" do
77
+ it "should cache the value of a method" do
78
+ grass = Grass.new
79
+ grass.mow.should == 9
80
+ grass.mow.should == 9
81
+ end
82
+
83
+ it "should cache a unique value per instance" do
84
+ grass = Grass.new
85
+ weed = Grass.new
86
+ weed.kill
87
+ grass.mow.should == 9
88
+ grass.mow.should == 9
89
+ weed.mow.should == -1
90
+ weed.mow.should == -1
91
+ end
92
+
93
+ describe "with parameters" do
94
+ it "caches the value by parameter value as well" do
95
+ grass = Grass.new
96
+ grass.cut(2).should == 8
97
+ grass.cut(1).should == 7
98
+ grass.cut(3).should == 4
99
+
100
+ grass.cut(1).should == 7
101
+ grass.cut(2).should == 8
102
+ grass.cut(3).should == 4
103
+
104
+ grass.length.should == 4
105
+ end
106
+ end
107
+
108
+ describe "when the paramter is an ActiveRecord object" do
109
+ before do
110
+ @treatment1 = Fertilize.new(3, 'a')
111
+ @treatment2 = Fertilize.new(3, 'b')
112
+ @treatment3 = Fertilize.new(2, 'c')
113
+ @treatment1_again = Fertilize.new(187, 'a')
114
+ @grass = Grass.new
115
+ end
116
+
117
+ it "uses the primary key in the id" do
118
+ @grass.weed(@treatment1).should == 7
119
+ @grass.weed(@treatment2).should == 4
120
+ @grass.weed(@treatment3).should == 2
121
+
122
+ @grass.weed(@treatment1).should == 7
123
+ @grass.weed(@treatment2).should == 4
124
+ @grass.weed(@treatment3).should == 2
125
+
126
+ @grass.weed(@treatment1_again).should == 7
127
+ end
128
+ end
129
+
130
+ describe "when there are multiple parameters" do
131
+ before do
132
+ @treatment1 = Fertilize.new(3, 'a')
133
+ @grass = Grass.new
134
+ end
135
+
136
+ it "takes all parameters into consideration" do
137
+ @grass.weed(@treatment1, [1,2,3]).should == 7
138
+ @grass.weed(@treatment1, [1,2]).should == 4
139
+ @grass.weed(@treatment1, [1,2,3]).should == 7
140
+ @grass.weed(@treatment1, [1,2]).should == 4
141
+ end
142
+ end
143
+
144
+ describe "when options are passed" do
145
+ before do
146
+ @treatment1 = Fertilize.new(3, 'a')
147
+ @grass = Grass.new
148
+ end
149
+
150
+ it "allows the same options as regular calls to Lawnchair.cache" do
151
+ @grass.weed(@treatment1, [1,2]).should == 7
152
+ sleep 1
153
+ @grass.weed(@treatment1, [1,2]).should == 4
154
+ end
155
+ end
156
+ end
29
157
  end
30
- end
158
+ end
data/spec/spec_helper.rb CHANGED
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
3
  require 'lawnchair'
4
4
  require 'spec'
5
5
  require 'spec/autorun'
6
-
6
+ require 'active_record_extension'
7
7
  Spec::Runner.configure do |config|
8
8
  config.before(:all) { Lawnchair.connectdb(Redis.new(:db => 11)) }
9
9
  config.before(:each) do
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lawnchair
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ hash: 15
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 6
9
+ - 4
10
+ version: 0.6.4
5
11
  platform: ruby
6
12
  authors:
7
13
  - Shane Wolf
@@ -9,19 +15,41 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2010-06-03 00:00:00 -07:00
18
+ date: 2010-07-13 00:00:00 -07:00
13
19
  default_executable:
14
20
  dependencies:
15
21
  - !ruby/object:Gem::Dependency
16
22
  name: redis
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
20
26
  requirements:
21
27
  - - ">="
22
28
  - !ruby/object:Gem::Version
29
+ hash: 31
30
+ segments:
31
+ - 0
32
+ - 1
33
+ - 2
23
34
  version: 0.1.2
24
- version:
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: activesupport
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 7
46
+ segments:
47
+ - 2
48
+ - 3
49
+ - 2
50
+ version: 2.3.2
51
+ type: :runtime
52
+ version_requirements: *id002
25
53
  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
54
  email: shanewolf@gmail.com
27
55
  executables: []
@@ -38,6 +66,7 @@ files:
38
66
  - Rakefile
39
67
  - VERSION
40
68
  - lawnchair.gemspec
69
+ - lib/active_record_extension.rb
41
70
  - lib/lawnchair.rb
42
71
  - lib/marshal_extension.rb
43
72
  - lib/storage_engine/abstract.rb
@@ -64,21 +93,27 @@ rdoc_options:
64
93
  require_paths:
65
94
  - lib
66
95
  required_ruby_version: !ruby/object:Gem::Requirement
96
+ none: false
67
97
  requirements:
68
98
  - - ">="
69
99
  - !ruby/object:Gem::Version
100
+ hash: 3
101
+ segments:
102
+ - 0
70
103
  version: "0"
71
- version:
72
104
  required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
73
106
  requirements:
74
107
  - - ">="
75
108
  - !ruby/object:Gem::Version
109
+ hash: 3
110
+ segments:
111
+ - 0
76
112
  version: "0"
77
- version:
78
113
  requirements: []
79
114
 
80
115
  rubyforge_project:
81
- rubygems_version: 1.3.5
116
+ rubygems_version: 1.3.7
82
117
  signing_key:
83
118
  specification_version: 3
84
119
  summary: Speed up your app by caching expensive code in Redis or in the ruby process itself