itrigga-cache 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -27,6 +27,40 @@ This will
27
27
  - render the content
28
28
  - cache the *rendered* output
29
29
  - set the :content_type according to the pasted in value (defaults to 'text/html')
30
+
31
+ == Content-type, respond_to and response status codes
32
+ With controller caching, there are a couple of gotchas.
33
+ - If you need to return a non-200 HTTP status code, set it in @status
34
+ - The cached value will be the *return value* of the block
35
+ - respond_to doesn't return anything - so it can break the nice and simple pattern
36
+
37
+ So, if you're not using an explicit render statement, you'll need to render_to_string as the last action of your cache block:
38
+
39
+ def tracker_stats
40
+ with_controller_cache( :key=>default_cache_key ) {
41
+ do_some_stuff_that_takes_a_long_time
42
+ @title = "#{@client.name} Tracker Stats"
43
+ @header = "#{@client.name} Tracker Stats"
44
+
45
+ render_to_string # <- this will be the cached value
46
+ }
47
+ end
48
+
49
+ If you're using respond_to, you need to do it like this:
50
+
51
+ with_controller_cache(opts) do
52
+ respond_to do |format|
53
+ format.html{
54
+ @content = render_to_string
55
+ }
56
+ format.js {
57
+ @content_type = 'text/javascript'
58
+ @content = render_to_string
59
+ }
60
+ end
61
+ @content
62
+ end
63
+
30
64
  To bypass the cache just have the "freshen" => true key in the params (ie as a query string param). This will force fresh the cache
31
65
 
32
66
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
@@ -4,7 +4,7 @@ require 'trigga/param_fu'
4
4
  module Itrigga
5
5
  module Cache
6
6
 
7
- @@ITRIGGA_CACHE_TYPE = :filecache
7
+ @default_cache_type = :filecache
8
8
 
9
9
  def self.included(base)
10
10
  base.extend(ClassMethods)
@@ -14,8 +14,8 @@ module Itrigga
14
14
  end
15
15
 
16
16
  def self.setup!(opts ={})
17
+ @default_cache_type = opts[:backend] if opts[:backend]
17
18
  if opts[:backend] == :memcached
18
- @@ITRIGGA_CACHE_TYPE = :memcached
19
19
  Itrigga::Cache::Memcache.setup!(opts)
20
20
  else
21
21
  Itrigga::Cache::Filecache.setup!(opts)
@@ -23,7 +23,7 @@ module Itrigga
23
23
  end
24
24
 
25
25
  def self.instance(opts = {})
26
- case ( opts[:backend] ||= @@ITRIGGA_CACHE_TYPE )
26
+ case ( opts[:backend] ||= @default_cache_type )
27
27
  when :memcached
28
28
  Itrigga::Cache::Memcache.instance
29
29
  when :filecache
@@ -102,7 +102,7 @@ module Itrigga
102
102
 
103
103
  def get_from_cache(key, opts = {})
104
104
  return nil unless caching_enabled?(opts)
105
- cache_log "get_from_cache key: #{key}, opts: #{opts.inspect}" #if opts[:debug]
105
+ cache_log "get_from_cache key: #{key}, opts: #{opts.inspect}" if opts[:debug]
106
106
  begin
107
107
  Itrigga::Cache.instance(opts).get key
108
108
  rescue Memcached::NotFound => not_found
@@ -119,7 +119,7 @@ module Itrigga
119
119
  def set_to_cache(key, value, opts = {})
120
120
  raise "Cache not Enabled" unless caching_enabled?(opts)
121
121
 
122
- cache_log "set_to_cache key: #{key}, value: #{value}, opts: #{opts.inspect}"# if opts[:debug]
122
+ cache_log "set_to_cache key: #{key}, value: #{value}, opts: #{opts.inspect}" if opts[:debug]
123
123
  begin
124
124
  Itrigga::Cache.instance(opts).set key, value, opts
125
125
  rescue Exception => e
@@ -182,7 +182,7 @@ module Itrigga
182
182
  @content ||= render_to_string unless performed?
183
183
  end
184
184
 
185
- render(:text=>@content, :content_type=>( @content_type || "text/html")) unless performed?
185
+ render(:text=>@content, :content_type=>( @content_type || "text/html"), :status=>@status) unless performed?
186
186
 
187
187
  @content
188
188
 
@@ -20,6 +20,7 @@ end
20
20
 
21
21
  describe Itrigga::Cache do
22
22
  before do
23
+ Itrigga::Cache.setup!(:backend=>:filecache)
23
24
  @klass = TriggaCacheTestClass.new
24
25
  @klass.stub!(:cache_log) # stub out the logging method in tests
25
26
  end
@@ -61,6 +62,10 @@ describe Itrigga::Cache do
61
62
  end
62
63
 
63
64
  describe "instance" do
65
+ before(:each) do
66
+ Itrigga::Cache.setup!(:backend=>:filecache)
67
+ end
68
+
64
69
  it "should initialize the memcache client" do
65
70
  Itrigga::Cache::Memcache.should_receive(:instance).and_return(@instance = mock("Instance"))
66
71
  Itrigga::Cache.instance(:backend => :memcached).should == @instance
@@ -71,9 +76,15 @@ describe Itrigga::Cache do
71
76
  Itrigga::Cache.instance(:backend => :filecache).should == @instance
72
77
  end
73
78
 
74
-
75
- it "should return the default @@ITRIGGA_CACHE_TYPE" do
76
- Itrigga::Cache::Filecache.should_receive(:instance).and_return(@instance = mock("Instance", :cache => "a cache"))
79
+ context "when no backend given" do
80
+ it "should call instance on the default @@ITRIGGA_CACHE_TYPE" do
81
+ Itrigga::Cache::Filecache.should_receive(:instance).and_return(@instance = mock("Instance", :cache => "a cache"))
82
+ Itrigga::Cache.instance()
83
+ end
84
+ end
85
+
86
+ it "should return the subclass instance" do
87
+ Itrigga::Cache::Filecache.stub!(:instance).and_return(@instance = mock("Instance", :cache => "a cache"))
77
88
  Itrigga::Cache.instance().should == @instance
78
89
  end
79
90
  end
@@ -169,11 +180,17 @@ describe Itrigga::Cache do
169
180
  before do
170
181
  Itrigga::Cache.stub!(:instance).and_return(@cache = mock("Cache"))
171
182
  @cache.stub!(:get).and_return("funky")
183
+ @cache.stub!(:enabled).and_return(true)
172
184
  end
173
185
 
174
- it "should return nil if caching disabled" do
175
- Itrigga::Cache.should_receive(:instance).and_return(nil)
176
- @klass.class.send("get_from_cache","monkeys").should == nil
186
+ context "if caching disabled" do
187
+ before(:each) do
188
+ @cache.stub!(:enabled).and_return(false)
189
+ end
190
+ it "should return nil" do
191
+ @klass.class.send("get_from_cache","monkeys").should == nil
192
+ end
193
+
177
194
  end
178
195
 
179
196
  it "should call get with the key" do
@@ -198,6 +215,7 @@ describe Itrigga::Cache do
198
215
  before do
199
216
  Itrigga::Cache.stub!(:instance).and_return(@cache = mock("Cache"))
200
217
  @cache.stub!(:set)
218
+ @cache.stub!(:enabled).and_return(true)
201
219
  end
202
220
 
203
221
  describe "when no timeout" do
@@ -357,10 +375,19 @@ describe Itrigga::Cache do
357
375
  end
358
376
 
359
377
  it "should call render" do
360
- @controller.should_receive(:render).with(:text => "content", :content_type => "text/html")
378
+ @controller.should_receive(:render).with(hash_including(:text => "content", :content_type => "text/html"))
361
379
  @controller.with_controller_cache(@opts) { "content" }
362
380
  end
363
381
 
382
+
383
+ it "should call render with status set from the instance variable @status" do
384
+ @controller.should_receive(:render).with(hash_including(:status => 418))
385
+ @controller.send(:instance_variable_set, "@status", 418)
386
+ @controller.with_controller_cache(@opts) {
387
+ "content"
388
+ }
389
+ end
390
+
364
391
  it "should return the content" do
365
392
  @controller.with_controller_cache(@opts) { "content" }.should == "content"
366
393
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: itrigga-cache
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 2
9
- - 1
10
- version: 0.2.1
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Al Davidson