lawnchair 0.5.2 → 0.5.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +4 -4
- data/VERSION +1 -1
- data/lawnchair.gemspec +1 -1
- data/lib/lawnchair.rb +12 -6
- data/lib/view/helper.rb +1 -1
- data/spec/lawnchair_spec.rb +4 -4
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -10,7 +10,7 @@ Lawnchair includes a Rails view helper that can be used to cache the rendered vi
|
|
10
10
|
|
11
11
|
== Usage Examples
|
12
12
|
|
13
|
-
All you really need to do is wrap some expensive piece of Ruby code in the Lawnchair
|
13
|
+
All you really need to do is wrap some expensive piece of Ruby code in the Lawnchair.cache method as a block and it will be evaluated and the return value will cached in the given cache key.
|
14
14
|
|
15
15
|
MAKE SURE REDIS SERVER IS RUNNING PRIOR TO TRYING ANYTHING BELOW!!!
|
16
16
|
|
@@ -24,7 +24,7 @@ This will connect to a default database on localhost, if you want to connect to
|
|
24
24
|
|
25
25
|
Obligatory example:
|
26
26
|
|
27
|
-
Lawnchair
|
27
|
+
Lawnchair.cache("cache_key2") do
|
28
28
|
# ideally this would be something a little more computationally expensive, but sleep will have to do
|
29
29
|
(1..3).inject([]) do
|
30
30
|
|set, i| set << Time.now.strftime("%H:%M:%S")
|
@@ -41,7 +41,7 @@ Now, since it is cached, any time this block method is called (for the next 60 m
|
|
41
41
|
|
42
42
|
If an hour is too long, or short for the cache key expiration you can set that to anything you want using the :expires_in hash key and entering a time in seconds
|
43
43
|
|
44
|
-
Lawnchair
|
44
|
+
Lawnchair.cache("cache_key", :expires_in => 1.day) do
|
45
45
|
# expensive code to be cached for 24 hours
|
46
46
|
end
|
47
47
|
|
@@ -55,7 +55,7 @@ Available options:
|
|
55
55
|
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
|
56
56
|
if you are running the Redis server on a different physical machine as all network latency is taken out of the equation, especially if you are hitting a cache key many times on the same request. Also, it's probably best not to store TONS of keys in there, as your ruby process can bloat fairly quickly if you put everything in there. Also, these will persist as long as the process is running, unless you manually expire it.
|
57
57
|
|
58
|
-
Lawnchair
|
58
|
+
Lawnchair.cache("cache_key3", :in_process => true) do
|
59
59
|
# expensive code to be cached in process AND in redis
|
60
60
|
end
|
61
61
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.5.
|
1
|
+
0.5.4
|
data/lawnchair.gemspec
CHANGED
data/lib/lawnchair.rb
CHANGED
@@ -11,8 +11,10 @@ if defined? RAILS_ENV
|
|
11
11
|
end
|
12
12
|
|
13
13
|
module Lawnchair
|
14
|
-
class
|
15
|
-
|
14
|
+
class << self
|
15
|
+
attr_reader :redis
|
16
|
+
|
17
|
+
def cache(key, options={}, &block)
|
16
18
|
if options[:in_process]
|
17
19
|
store = Lawnchair::StorageEngine::Composite.new(:in_process, :redis)
|
18
20
|
else
|
@@ -20,10 +22,6 @@ module Lawnchair
|
|
20
22
|
end
|
21
23
|
store.fetch(key, options, &block)
|
22
24
|
end
|
23
|
-
end
|
24
|
-
|
25
|
-
class << self
|
26
|
-
attr_reader :redis
|
27
25
|
|
28
26
|
def connectdb(redis=nil)
|
29
27
|
@redis = (redis || Redis.new(:db => 11))
|
@@ -33,4 +31,12 @@ module Lawnchair
|
|
33
31
|
redis.flushdb
|
34
32
|
end
|
35
33
|
end
|
34
|
+
|
35
|
+
class Cache
|
36
|
+
# <b>DEPRECATED:</b> Please use <tt>Lawnchair.cache</tt> instead.
|
37
|
+
def self.me(key, options={}, &block)
|
38
|
+
warn "[DEPRECATION] 'Lawnchair::Cache.me' is deprecated. Please use 'Lawnchair.cache' instead."
|
39
|
+
Lawnchair.cache(key, options, &block)
|
40
|
+
end
|
41
|
+
end
|
36
42
|
end
|
data/lib/view/helper.rb
CHANGED
data/spec/lawnchair_spec.rb
CHANGED
@@ -4,15 +4,15 @@ describe "Lawnchair::Cache" do
|
|
4
4
|
describe ".me" do
|
5
5
|
it "returns the value if it exists" do
|
6
6
|
expected_object = [1,2,3,4]
|
7
|
-
Lawnchair
|
7
|
+
Lawnchair.cache("marshalled_array") { expected_object }
|
8
8
|
|
9
|
-
x = Lawnchair
|
9
|
+
x = Lawnchair.cache("marshalled_array") { "JUNK DATA" }
|
10
10
|
x.should == expected_object
|
11
11
|
end
|
12
12
|
|
13
13
|
it "marshalls the object into redis" do
|
14
14
|
expected_object = [1,2,3,4]
|
15
|
-
Lawnchair
|
15
|
+
Lawnchair.cache("marshalled_array") { expected_object }
|
16
16
|
|
17
17
|
Marshal.load(Lawnchair.redis["Lawnchair:marshalled_array"]).should == [1,2,3,4]
|
18
18
|
end
|
@@ -23,7 +23,7 @@ describe "Lawnchair::Cache" do
|
|
23
23
|
Lawnchair::StorageEngine::Composite.stub!(:new).and_return(mock_composite_engine)
|
24
24
|
|
25
25
|
mock_composite_engine.should_receive(:fetch)
|
26
|
-
Lawnchair
|
26
|
+
Lawnchair.cache("mu", :in_process => true, :raw => true) { "fasa" }
|
27
27
|
end
|
28
28
|
end
|
29
29
|
end
|