thorero-cache 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,137 @@
1
+ describe "merb-cache-action" do
2
+
3
+ it "should cache action (action3)" do
4
+ c = get("/cache_controller/action3")
5
+ c.body.strip.should == "test action3"
6
+ c.cached?("/cache_controller/action3").should be_true
7
+ c.cached_action?("action3").should be_true
8
+ c.cache_get("/cache_controller/action3").should == "test action3"
9
+ end
10
+
11
+ it "should expire action (action3)" do
12
+ CACHE.expire_action "action3"
13
+ CACHE.cache_get("/cache_controller/action3").should be_nil
14
+ end
15
+
16
+ it "should cache action (action3) with full path" do
17
+ c = get("/cache_controller/action3/abc/456/ghi")
18
+ c.body.strip.should == "test action3"
19
+ c.cached?(c.request.path).should be_true
20
+ c.cache_get(c.request.path).should == "test action3"
21
+ end
22
+
23
+ it "should expire action (action3) with full path" do
24
+ c = get("/cache_controller/action3/abc/456/ghi")
25
+ c.expire_action(:key => c.request.path)
26
+ c.cache_get(c.request.path).should be_nil
27
+ end
28
+
29
+ it "should expire action (action4) after 3 seconds" do
30
+ c = get("/cache_controller/action4")
31
+ c.body.strip.should == "test action4"
32
+ c.cached?("/cache_controller/action4").should be_true
33
+ c.cache_get("/cache_controller/action4").should == "test action4"
34
+ sleep 4
35
+ c.cache_get("/cache_controller/action4").should be_nil
36
+ c.cached_action?(:action => "action4").should be_false
37
+ end
38
+
39
+ it "should cache action with full path (action4) and expire in 3 seconds" do
40
+ CACHE.expire_action :match => true, :action => "action4"
41
+ CACHE.cached_action?(:action => "action4", :params => %w(path to nowhere)).should be_false
42
+ c = get("/cache_controller/action4/path/to/nowhere/")
43
+ c.cached_action?(:action => "action4", :params => %w(path to nowhere)).should be_true
44
+ sleep 3.5
45
+ c.cache_get("/cache_controller/action4/path/to/nowhere").should be_nil
46
+ c.cached_action?(:action => "action4", :params => %w(path to nowhere)).should be_false
47
+ end
48
+
49
+ it "should expire action in many ways" do
50
+ c = get("/cache_controller/action4")
51
+ CACHE.expire_action("action4")
52
+ CACHE.cached_action?("action4").should be_false
53
+
54
+ c = get("/cache_controller/action4")
55
+ CACHE.expire_action(:match => "/cache_control")
56
+ CACHE.cached_action?(:action => "action4").should be_false
57
+
58
+ c = get("/cache_controller/action4")
59
+ CACHE.expire_action(:action => "action4")
60
+ CACHE.cached_action?(:action => "action4").should be_false
61
+
62
+ c = get("/cache_controller/action4/id1/id2")
63
+ CACHE.expire_action(:action => "action4", :params => %w(id1 id2))
64
+ CACHE.cached_action?(:action => "action4", :params => %w(id1 id2)).should be_false
65
+
66
+ c = get("/cache_controller/action4/id1/id2")
67
+ CACHE.expire_action(:action => "action4", :match => true)
68
+ CACHE.cached_action?(:action => "action4", :params => %w(id1 id2)).should be_false
69
+
70
+ c = get("/cache_controller/action4")
71
+ CACHE.expire_action(:action => "action4", :controller => "cache_controller")
72
+ CACHE.cached_action?(:action => "action4", :controller => "cache_controller").should be_false
73
+
74
+ c = get("/cache_controller/action4/id1/id2")
75
+ CACHE.expire_action(:action => "action4", :params => %w(id1), :match => true)
76
+ CACHE.cached_action?(:action => "action4", :params => %w(id1 id2)).should be_false
77
+
78
+ c = get("/cache_controller/action4/id1/id2")
79
+ CACHE.expire_action(:action => "action4", :controller => "cache_controller", :match => true)
80
+ CACHE.cached_action?(:action => "action4", :params => %w(id1 id2)).should be_false
81
+
82
+ c = get("/cache_controller/action4/id1/id2")
83
+ CACHE.expire_action(:action => "action4", :controller => "cache_controller", :params => %w(id1), :match => true)
84
+ CACHE.cached_action?(:action => "action4", :params => %w(id1 id2)).should be_false
85
+
86
+ c = get("/cache_controller/action4/id1/id2")
87
+ CACHE.expire_action(:action => "action4", :controller => "cache_controller", :params => %w(id1 id2))
88
+ CACHE.cached_action?(:action => "action4", :controller => "cache_controller", :params => %w(id1 id2)).should be_false
89
+
90
+ c = get("/cache_controller/action4")
91
+ CACHE.expire_action(:key => "/cache_controller/action4")
92
+ CACHE.cached_action?(:key => "/cache_controller/action4").should be_false
93
+ c = get("/cache_controller/action4/id1/id2")
94
+ CACHE.expire_action(:key => "/cache_controller/action4", :params => %w(id1 id2))
95
+ CACHE.cached_action?(:key => "/cache_controller/action4", :params => %w(id1 id2)).should be_false
96
+
97
+ c = get("/cache_controller/action4/id1/id2")
98
+ CACHE.expire_action(:key => "/cache_controller/action4/id1", :match => true)
99
+ CACHE.cached_action?(:key => "/cache_controller/action4/id1/id2").should be_false
100
+
101
+ c = get("/cache_controller/action4/id1/id2")
102
+ CACHE.expire_action(:key => "/cache_controller/action4", :params => %w(id1), :match => true)
103
+ CACHE.cached_action?(:key => "/cache_controller/action4/id1/id2").should be_false
104
+ end
105
+
106
+ it "should allow :if conditions with procs" do
107
+ c = get("/cache_controller/action8")
108
+ CACHE.cached_action?(:key => "/cache_controller/action8").should be_false
109
+
110
+ c = get("/cache_controller/action8/cache")
111
+ CACHE.cached_action?(:key => "/cache_controller/action8/cache").should be_true
112
+ end
113
+
114
+ it "should allow :unless conditions with procs" do
115
+ c = get("/cache_controller/action9")
116
+ CACHE.cached_action?(:key => "/cache_controller/action9").should be_false
117
+
118
+ c = get("/cache_controller/action9/cache")
119
+ CACHE.cached_action?(:key => "/cache_controller/action9/cache").should be_true
120
+ end
121
+
122
+ it "should allow :if conditions with symbols" do
123
+ c = get("/cache_controller/action10")
124
+ CACHE.cached_action?(:key => "/cache_controller/action10").should be_false
125
+
126
+ c = get("/cache_controller/action10/cache")
127
+ CACHE.cached_action?(:key => "/cache_controller/action10/cache").should be_true
128
+ end
129
+
130
+ it "should allow :unless conditions with symbols" do
131
+ c = get("/cache_controller/action11")
132
+ CACHE.cached_action?(:key => "/cache_controller/action11").should be_false
133
+
134
+ c = get("/cache_controller/action11/cache")
135
+ CACHE.cached_action?(:key => "/cache_controller/action11/cache").should be_true
136
+ end
137
+ end
@@ -0,0 +1,100 @@
1
+ # TODO
2
+ # more specs for fragment caching:
3
+ # cache_get, cache_set, cached?, cache, expire
4
+
5
+ describe "merb-cache-fragment" do
6
+
7
+ it "should render index" do
8
+ c = dispatch_to(CacheController, :index)
9
+ c.body.should == "test index"
10
+ end
11
+
12
+ it "should cache the fragment (erb capture/concat)" do
13
+ c = dispatch_to(CacheController, :action1)
14
+ NOW = c.body.strip
15
+ c.cache_get("key1").strip.should == NOW
16
+ end
17
+
18
+ it "should cache the fragment (haml capture/concat)" do
19
+ c = dispatch_to(CacheController, :action2)
20
+ now = c.body.strip
21
+ c.cache_get("key11").strip.should == now
22
+ sleep 1
23
+ c = dispatch_to(CacheController, :action2)
24
+ c.cache_get("key11").strip.should == now
25
+ c.expire("key11")
26
+ c.cache_get("key11").should be_nil
27
+ end
28
+
29
+ it "should use the fragment" do
30
+ sleep 1
31
+ c = dispatch_to(CacheController, :action1)
32
+ c.body.strip.should == NOW
33
+ end
34
+
35
+ it "should expire the fragment" do
36
+ CACHE.expire("key1")
37
+ CACHE.cache_get("key1").should be_nil
38
+ end
39
+
40
+ it "should refresh the template" do
41
+ c = dispatch_to(CacheController, :action1)
42
+ c.body.strip.should_not == NOW
43
+ end
44
+
45
+ it "should return nil for unknown keys" do
46
+ CACHE.cache_get("unknown_key").should be_nil
47
+ end
48
+
49
+ it "should expire matching keys" do
50
+ CACHE.cache_set("test1", "test1")
51
+ CACHE.cache_get("test1").should == "test1"
52
+ CACHE.cache_set("test2", "test2")
53
+ CACHE.cache_get("test2").should == "test2"
54
+ CACHE.cache_set("test3", "test3")
55
+ CACHE.cache_get("test3").should == "test3"
56
+ CACHE.expire(:match => "test")
57
+ CACHE.cache_get("test1").should be_nil
58
+ CACHE.cache_get("test2").should be_nil
59
+ CACHE.cache_get("test3").should be_nil
60
+ end
61
+
62
+ it "should expire entry after 3 seconds" do
63
+ CACHE.cache_set("timed_key", "vanish in 3 seconds", 0.05)
64
+ CACHE.cache_get("timed_key").should == "vanish in 3 seconds"
65
+ sleep 3.5
66
+ CACHE.cache_get("timed_key").should be_nil
67
+ end
68
+
69
+ it "should expire in many ways" do
70
+ CACHE.cache_set("test1", "test1")
71
+ CACHE.expire("test1")
72
+ CACHE.cache_get("test1").should be_nil
73
+
74
+ CACHE.cache_set("test2/id1", "test2")
75
+ CACHE.expire(:key => "test2", :params => %w(id1))
76
+ CACHE.cache_get("test2/id1").should be_nil
77
+
78
+ CACHE.cache_set("test3", "test3")
79
+ CACHE.expire(:key => "test3")
80
+ CACHE.cache_get("test3").should be_nil
81
+
82
+ CACHE.cache_set("test4/id1", "test4")
83
+ CACHE.expire(:key => "test4", :params => %w(id1), :match => true)
84
+ CACHE.cache_get("test4/id1/id2").should be_nil
85
+ end
86
+
87
+ it "should expire all keys" do
88
+ CACHE.expire_all
89
+ CACHE.cache_get("key1").should be_nil
90
+ end
91
+
92
+ it "should cache/restore ruby objects" do
93
+ now = Time.now
94
+ data = {:key1 => "key1", :key2 => "key2", :now => Time.now }
95
+ CACHE.cache_set("key1", data)
96
+ _data = CACHE.cache_get("key1")
97
+ data.should == _data
98
+ end
99
+
100
+ end
@@ -0,0 +1,150 @@
1
+ describe "merb-cache-page" do
2
+
3
+ it "should cache page (action5)" do
4
+ c = get("/cache_controller/action5")
5
+ c.body.strip.should == "test action5"
6
+ c.cached_page?("action5").should be_true
7
+ end
8
+
9
+ it "should expire page (action5)" do
10
+ CACHE.expire_page("action5")
11
+ CACHE.cached_page?("action5").should be_false
12
+ end
13
+
14
+ it "should cache page (action5) with full path" do
15
+ c = get("/cache_controller/action5/this/is/a/test")
16
+ c.cached_page?(:action => "action5", :params => %w(this is a test)).should be_true
17
+ end
18
+
19
+ it "should expire page (action5) with full path" do
20
+ CACHE.expire_page(:action => "action5",
21
+ :controller => "cache_controller",
22
+ :params => %w(this is a test))
23
+ CACHE.cached_page?(:key => "/cache_controller/action5/this/is/a/test").should be_false
24
+ end
25
+
26
+ it "should cache page (action6), use it and expire 3 seconds after" do
27
+ CACHE.expire_page :match => true, :action => "action6"
28
+ c = get("/cache_controller/action6")
29
+ now = Time.now.to_s
30
+ c.body.strip.should == now
31
+ c.cached_page?("action6").should be_true
32
+ sleep 1
33
+ c = get("/cache_controller/action6")
34
+ c.body.strip.should == now
35
+ sleep 2
36
+ c = get("/cache_controller/action6")
37
+ c.body.strip.should == Time.now.to_s
38
+ end
39
+
40
+ it "should cache page with full path (action6) and expire in 3 seconds" do
41
+ CACHE.expire_page "action6"
42
+ CACHE.cached_page?(:action => "action6", :params => %w(path to nowhere)).should be_false
43
+ c = get("/cache_controller/action6/path/to/nowhere/")
44
+ now = Time.now.to_s
45
+ c.body.strip.should == now
46
+ c.cached_page?(:action => "action6", :params => %w(path to nowhere)).should be_true
47
+ sleep 1
48
+ c = get("/cache_controller/action6/path/to/nowhere")
49
+ c.body.strip.should == now
50
+ sleep 2
51
+ c = get("/cache_controller/action6/path/to/nowhere")
52
+ c.body.strip.should == Time.now.to_s
53
+ end
54
+
55
+ it "should expire page in many ways" do
56
+ c = get("/cache_controller/action6")
57
+ CACHE.expire_page("action6")
58
+ CACHE.cached_page?("action6").should be_false
59
+
60
+ c = get("/cache_controller/action6")
61
+ CACHE.expire_page(:match => "/cache_control")
62
+ CACHE.cached_page?(:action => "action6").should be_false
63
+
64
+ c = get("/cache_controller/action6")
65
+ CACHE.expire_page(:action => "action6")
66
+ CACHE.cached_page?(:action => "action6").should be_false
67
+
68
+ c = get("/cache_controller/action6/id1/id2")
69
+ CACHE.expire_page(:action => "action6", :params => %w(id1 id2))
70
+ CACHE.cached_page?(:action => "action6", :params => %w(id1 id2)).should be_false
71
+
72
+ c = get("/cache_controller/action6/id1/id2")
73
+ CACHE.expire_page(:action => "action6", :match => true)
74
+ CACHE.cached_page?(:action => "action6", :params => %w(id1 id2)).should be_false
75
+
76
+ c = get("/cache_controller/action6")
77
+ CACHE.expire_page(:action => "action6", :controller => "cache_controller")
78
+ CACHE.cached_page?(:action => "action6", :controller => "cache_controller").should be_false
79
+
80
+ c = get("/cache_controller/action6/id1/id2")
81
+ CACHE.expire_page(:action => "action6", :params => %w(id1), :match => true)
82
+ CACHE.cached_page?(:action => "action6", :params => %w(id1 id2)).should be_false
83
+
84
+ c = get("/cache_controller/action6/id1/id2")
85
+ CACHE.expire_page(:action => "action6", :controller => "cache_controller", :match => true)
86
+ CACHE.cached_page?(:action => "action6", :params => %w(id1 id2)).should be_false
87
+
88
+ c = get("/cache_controller/action6/id1/id2")
89
+ CACHE.expire_page(:action => "action6", :controller => "cache_controller", :params => %w(id1), :match => true)
90
+ CACHE.cached_page?(:action => "action6", :params => %w(id1 id2)).should be_false
91
+
92
+ c = get("/cache_controller/action6/id1/id2")
93
+ CACHE.expire_page(:action => "action6", :controller => "cache_controller", :params => %w(id1 id2))
94
+ CACHE.cached_page?(:action => "action6", :controller => "cache_controller", :params => %w(id1 id2)).should be_false
95
+
96
+ c = get("/cache_controller/action6")
97
+ CACHE.expire_page(:key => "/cache_controller/action6")
98
+ CACHE.cached_page?(:key => "/cache_controller/action6").should be_false
99
+ c = get("/cache_controller/action6/id1/id2")
100
+ CACHE.expire_page(:key => "/cache_controller/action6", :params => %w(id1 id2))
101
+ CACHE.cached_page?(:key => "/cache_controller/action6", :params => %w(id1 id2)).should be_false
102
+
103
+ c = get("/cache_controller/action6/id1/id2")
104
+ CACHE.expire_page(:key => "/cache_controller/action6/id1", :match => true)
105
+ CACHE.cached_page?(:key => "/cache_controller/action6/id1/id2").should be_false
106
+
107
+ c = get("/cache_controller/action6/id1/id2")
108
+ CACHE.expire_page(:key => "/cache_controller/action6", :params => %w(id1), :match => true)
109
+ CACHE.cached_page?(:key => "/cache_controller/action6/id1/id2").should be_false
110
+ end
111
+
112
+ it "should respect original content-type" do
113
+ c = get("/cache_controller/action7.css")
114
+ c.body.should == "CSS"
115
+ c = get("/cache_controller/action7.css")
116
+ c.params[:format].should == "css"
117
+ c.cached_page?(:action => "action7", :extension => 'css').should be_true
118
+
119
+ c = get("/cache_controller/action7.js")
120
+ c.body.should == "JS"
121
+ c = get("/cache_controller/action7.js")
122
+ c.params[:format].should == "js"
123
+ c.cached_page?(:action => "action7", :extension => 'js').should be_true
124
+
125
+ c = get("/cache_controller/action7.xml")
126
+ c.body.should == "XML"
127
+ c = get("/cache_controller/action7.xml")
128
+ c.params[:format].should == "xml"
129
+ c.cached_page?(:action => "action7", :extension => 'xml').should be_true
130
+
131
+ c = get("/cache_controller/action7.jpg")
132
+ c.body.should == "JPG"
133
+ c = get("/cache_controller/action7.jpg")
134
+ c.params[:format].should == "jpg"
135
+ c.cached_page?(:action => "action7", :extension => 'jpg').should be_true
136
+
137
+ c = get("/cache_controller/action7.html")
138
+ c.body.should == "HTML"
139
+ c = get("/cache_controller/action7.html")
140
+ c.params[:format].should == "html"
141
+ c.cached_page?(:action => "action7").should be_true
142
+ end
143
+
144
+ it "should expire all pages" do
145
+ CACHE.expire_all_pages
146
+ CACHE.cached_page?("action6").should be_false
147
+ Dir.glob(Merb::Controller._cache.config[:cache_html_directory] + '/*').should be_empty
148
+ end
149
+
150
+ end
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
3
+ Merb::Router.prepare do |r|
4
+ r.default_routes
5
+ r.match("/").to(:controller => "cache_controller", :action =>"index")
6
+ end
7
+
8
+ CACHE = CacheController.new(Merb::Test::RequestHelper::FakeRequest.new)
9
+ CACHE.expire_all
10
+
11
+ puts "Using #{CACHE._cache.store.cache_store_type.inspect} store"
12
+
13
+ require File.dirname(__FILE__) + "/merb-cache-fragment_spec"
14
+ require File.dirname(__FILE__) + "/merb-cache-action_spec"
15
+ require File.dirname(__FILE__) + "/merb-cache-page_spec"
@@ -0,0 +1,84 @@
1
+ $TESTING=true
2
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
3
+ require "rubygems"
4
+ require "merb-core"
5
+
6
+ require "merb-cache"
7
+ require File.dirname(__FILE__) / "controller"
8
+
9
+ require "merb-haml"
10
+
11
+ def set_database_adapter(adapter)
12
+ config_file = File.dirname(__FILE__) / "config/database.yml"
13
+ config = IO.read(config_file)
14
+ config.gsub!(/:adapter:\s+.*$/, ":adapter: #{adapter}")
15
+ File.open(config_file, "w+") do |c| c.write(config) end
16
+ end
17
+
18
+ def use_cache_store(store, orm = nil)
19
+ Merb::Plugins.config[:merb_cache] = {
20
+ :store => store,
21
+ :cache_directory => File.dirname(__FILE__) / "tmp/cache",
22
+ :cache_html_directory => File.dirname(__FILE__) / "tmp/html",
23
+ :no_tracking => false
24
+ }
25
+ FileUtils.rm_rf(Dir.glob(File.dirname(__FILE__) / "/tmp"))
26
+ case store
27
+ when "dummy"
28
+ when "file"
29
+ when "memory"
30
+ when "memcache"
31
+ require "memcache"
32
+ when "database"
33
+ case orm
34
+ when "datamapper"
35
+ Merb.environment = "test"
36
+ Merb.logger = Merb::Logger.new("log/merb_test.log")
37
+ set_database_adapter("sqlite3")
38
+ require "merb_datamapper"
39
+ when "activerecord"
40
+ Merb.logger = Merb::Logger.new("log/merb_test.log")
41
+ set_database_adapter("sqlite3")
42
+ require "merb_activerecord"
43
+ when "sequel"
44
+ set_database_adapter("sqlite")
45
+ require "merb_sequel"
46
+ else
47
+ raise "Unknown orm: #{orm}"
48
+ end
49
+ else
50
+ raise "Unknown cache store: #{store}"
51
+ end
52
+ end
53
+
54
+ store = "file"
55
+ case ENV["STORE"] || store
56
+ when "file"
57
+ use_cache_store "file"
58
+ when "memory"
59
+ use_cache_store "memory"
60
+ when "memcache"
61
+ use_cache_store "memcache"
62
+ when "datamapper"
63
+ use_cache_store "database", "datamapper"
64
+ when "sequel"
65
+ use_cache_store "database", "sequel"
66
+ when "activerecord"
67
+ use_cache_store "database", "activerecord"
68
+ when "dummy"
69
+ use_cache_store "dummy"
70
+ else
71
+ puts "Invalid cache store: #{ENV["store"]}"
72
+ exit
73
+ end
74
+
75
+ require "fileutils"
76
+ FileUtils.mkdir_p(Merb::Plugins.config[:merb_cache][:cache_html_directory])
77
+ FileUtils.mkdir_p(Merb::Plugins.config[:merb_cache][:cache_directory])
78
+
79
+ Merb.start :environment => "test", :adapter => "runner"
80
+
81
+ require "merb-core/test"
82
+ Spec::Runner.configure do |config|
83
+ config.include Merb::Test::RequestHelper
84
+ end