lawnchair 0.6.6 → 0.6.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -49,7 +49,25 @@ Available options:
49
49
  * :expires_in - takes a time in seconds and will be set as the ttl on the key (only works for Redis store)
50
50
  * :raw - tells the cache not to marshall the data, if you are storing a string value, this is the way to go
51
51
  * :in_process - stores the value in memory for as long as the ruby process is running as well as in redis
52
+ * :interpolate - allows you to cache large pieces of data but override sections of it with dynamic content
53
+
54
+ == Interpolating Data
55
+
56
+ Caching large pieces of the page can prove to be difficult in a highly dynamic environment. Content interpolation
57
+ makes this easier to deal with common things such as user names, timestamps, flash messages and more.
58
+
59
+ cached_result = Lawnchair.cache("cached_time") { "current time: __TIME__" }
60
+ => "current time: __TIME__"
61
+
62
+ Now let's interpolate this data with dynamic content.
63
+
64
+ cached_result = Lawnchair.cache("cached_time", :interpolate => {"__TIME__" => Time.now.to_s}) { "current time: __TIME__" }
65
+ => "current time: Tue Aug 24 16:13:40 -0700 2010"
66
+ cached_result = Lawnchair.cache("cached_time", :interpolate => {"__TIME__" => Time.now.to_s}) { "current time: __TIME__" }
67
+ => "current time: Tue Aug 24 16:13:42 -0700 2010"
52
68
 
69
+ Notice that the timestamp is different for each call, even though the data is coming out of the cache.
70
+
53
71
  == In Process Caching
54
72
 
55
73
  If you want to get really fancy you can cache the values in process as well as in Redis. This can be a fairly significant win
@@ -89,7 +107,6 @@ If you need to flush all the values in the Redis database
89
107
 
90
108
  Lawnchair.flushdb
91
109
 
92
-
93
110
  == Note on Patches/Pull Requests
94
111
 
95
112
  * Fork the project.
@@ -100,3 +117,5 @@ If you need to flush all the values in the Redis database
100
117
  == Copyright
101
118
 
102
119
  Copyright (c) 2010 Shane Wolf. See LICENSE for details.
120
+
121
+ Thanks to Tyler Kovacs for the inspiration for content interpolation.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.6
1
+ 0.6.7
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{lawnchair}
8
- s.version = "0.6.6"
8
+ s.version = "0.6.7"
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-08-10}
12
+ s.date = %q{2010-08-24}
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 = [
@@ -21,7 +21,9 @@ module Lawnchair
21
21
  else
22
22
  store = Lawnchair::StorageEngine::Redis
23
23
  end
24
- store.fetch(key, options, &block)
24
+ interpolate(options[:interpolate]) do
25
+ store.fetch(key, options, &block)
26
+ end
25
27
  end
26
28
 
27
29
  def connectdb(redis=nil)
@@ -31,6 +33,11 @@ module Lawnchair
31
33
  def flushdb
32
34
  redis.flushdb
33
35
  end
36
+
37
+ def interpolate(interpolations, &block)
38
+ interpolations ||= {}
39
+ interpolations.inject(block.call){|cached_data, interpolated_data| cached_data.gsub!(interpolated_data.first, interpolated_data.last) }
40
+ end
34
41
  end
35
42
 
36
43
  class Cache
@@ -9,6 +9,7 @@ module Lawnchair
9
9
  end
10
10
 
11
11
  def fetch(key, options={}, &block)
12
+
12
13
  start_time = Time.now
13
14
  if exists?(key)
14
15
  value = get(key, options)
@@ -47,7 +47,7 @@ class Grass
47
47
  end
48
48
 
49
49
  describe "Lawnchair::Cache" do
50
- describe ".me" do
50
+ describe ".cache" do
51
51
  it "returns the value if it exists" do
52
52
  expected_object = [1,2,3,4]
53
53
  Lawnchair.cache("marshalled_array") { expected_object }
@@ -63,7 +63,7 @@ describe "Lawnchair::Cache" do
63
63
  Marshal.load(Lawnchair.redis["Lawnchair:marshalled_array"]).should == [1,2,3,4]
64
64
  end
65
65
 
66
- describe "when in_process => true" do
66
+ context "when in_process => true" do
67
67
  it "fetches the value to/from the composite store" do
68
68
  mock_composite_engine = Lawnchair::StorageEngine::Composite.new
69
69
  Lawnchair::StorageEngine::Composite.stub!(:new).and_return(mock_composite_engine)
@@ -73,6 +73,33 @@ describe "Lawnchair::Cache" do
73
73
  end
74
74
  end
75
75
 
76
+ context "when there is content to be interpolated" do
77
+ context "when the key exists in the cache" do
78
+ before do
79
+ Lawnchair.cache("mu") { "current time: __TIME__" }
80
+ Lawnchair.redis.exists("Lawnchair:mu").should be_true
81
+ end
82
+
83
+ it "replaces the key with the given data" do
84
+ now = Time.now.to_s(:db)
85
+ cached_result = Lawnchair.cache("mu", :interpolate => {"__TIME__" => now}) { "current time: __TIME__" }
86
+ cached_result.should == "current time: #{now}"
87
+ end
88
+ end
89
+
90
+ context "when the key does NOT exist in the cache" do
91
+ before do
92
+ Lawnchair.redis.exists("mu").should be_false
93
+ end
94
+
95
+ it "replaces the key with the given data" do
96
+ now = Time.now.to_s(:db)
97
+ cached_result = Lawnchair.cache("mu", :interpolate => {"__TIME__" => now}) { "current time: __TIME__" }
98
+ cached_result.should == "current time: #{now}"
99
+ end
100
+ end
101
+ end
102
+
76
103
  describe "caching a method" do
77
104
  it "should cache the value of a method" do
78
105
  grass = Grass.new
@@ -105,41 +132,41 @@ describe "Lawnchair::Cache" do
105
132
  end
106
133
  end
107
134
 
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
135
+ # describe "when the parameter is an ActiveRecord object" do
136
+ # before do
137
+ # @treatment1 = Fertilize.new(3, 'a')
138
+ # @treatment2 = Fertilize.new(3, 'b')
139
+ # @treatment3 = Fertilize.new(2, 'c')
140
+ # @treatment1_again = Fertilize.new(187, 'a')
141
+ # @grass = Grass.new
142
+ # end
143
+ #
144
+ # it "uses the primary key in the id" do
145
+ # @grass.weed(@treatment1).should == 7
146
+ # @grass.weed(@treatment2).should == 4
147
+ # @grass.weed(@treatment3).should == 2
148
+ #
149
+ # @grass.weed(@treatment1).should == 7
150
+ # @grass.weed(@treatment2).should == 4
151
+ # @grass.weed(@treatment3).should == 2
152
+ #
153
+ # @grass.weed(@treatment1_again).should == 7
154
+ # end
155
+ # end
129
156
 
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
157
+ # describe "when there are multiple parameters" do
158
+ # before do
159
+ # @treatment1 = Fertilize.new(3, 'a')
160
+ # @grass = Grass.new
161
+ # end
162
+ #
163
+ # it "takes all parameters into consideration" do
164
+ # @grass.weed(@treatment1, [1,2,3]).should == 7
165
+ # @grass.weed(@treatment1, [1,2]).should == 4
166
+ # @grass.weed(@treatment1, [1,2,3]).should == 7
167
+ # @grass.weed(@treatment1, [1,2]).should == 4
168
+ # end
169
+ # end
143
170
 
144
171
  describe "when options are passed" do
145
172
  before do
@@ -6,7 +6,7 @@ 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
- config.before(:each) do
9
+ config.before(:each) do
10
10
  Lawnchair.flushdb
11
11
  abstract_store = Lawnchair::StorageEngine::Abstract
12
12
  abstract_store.data_store.keys.each {|k| abstract_store.data_store.delete(k)}
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lawnchair
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 9
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 6
9
- - 6
10
- version: 0.6.6
9
+ - 7
10
+ version: 0.6.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Shane Wolf
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-10 00:00:00 -07:00
18
+ date: 2010-08-24 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency