public
Description: All the extra stuff you could want for the Mack Framework.
Homepage: http://www.mackframework.com
Clone URL: git://github.com/markbates/mack-more.git
Feature: Page Caching [#18 state:resolved]
markbates (author)
Fri Jul 18 09:38:30 -0700 2008
commit  e5c2e8ade1ef1dcef5344ec8a9f0662d4ee2717c
tree    a2ec3c2c4a39afc59ff242b51cb21dd19ae00f3d
parent  d16c138c3927367c222babacf543cfa125c8d42d
...
12
13
14
 
15
16
17
18
19
20
 
21
22
23
...
27
28
29
30
31
32
 
33
34
35
...
37
38
39
40
41
42
43
...
12
13
14
15
16
17
18
19
20
 
21
22
23
24
...
28
29
30
 
31
 
32
33
34
35
...
37
38
39
 
40
41
42
0
@@ -12,12 +12,13 @@ module Mack
0
           page = Cachetastic::Caches::PageCache.get(request.fullpath)
0
           if page
0
             response = Mack::Response.new
0
+            response["Content-Type"] = page.content_type
0
             response.write(page.body)
0
             return response.finish
0
           end
0
           ret = @app.call(env)
0
           res = ret[2]
0
-          Cachetastic::Caches::PageCache.set(request.fullpath, Mack::Caching::PageCaching::Page.new(res.body))
0
+          Cachetastic::Caches::PageCache.set(request.fullpath, Mack::Caching::PageCaching::Page.new(res.body, res["Content-Type"])) if res.successful?
0
           return ret
0
         end
0
         return @app.call(env)
0
@@ -27,9 +28,8 @@ module Mack
0
         
0
         attr_reader :body
0
         attr_reader :content_type
0
-        attr_reader :status
0
         
0
-        def initialize(body, content_type = "text/html", status = 200)
0
+        def initialize(body, content_type = "text/html")
0
           if body.is_a?(Array)
0
             raise Mack::Caching::PageCaching::UncacheableError.new("Multipart pages can not be cached!") if body.size > 1
0
             @body = body.first
0
@@ -37,7 +37,6 @@ module Mack
0
             @body = body
0
           end
0
           @content_type = content_type
0
-          @status = status
0
         end
0
         
0
         def to_s
...
4
5
6
 
 
 
 
 
 
 
 
 
 
 
 
 
7
8
...
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
0
@@ -4,5 +4,18 @@ class DefaultController
0
   # /
0
   def index
0
   end
0
+  
0
+  def hello_world
0
+    wants(:xml) do
0
+      render(:text, "<name>#{params[:name]}</name><rand>#{rand}</rand>")
0
+    end
0
+    wants(:html) do
0
+      render(:text, "<p>#{params[:name]}</p><p>#{rand}</p>")
0
+    end
0
+  end
0
+  
0
+  def always_500
0
+    render(:text, "i should be 500: #{rand}", :status => 500)
0
+  end
0
 
0
 end
...
4
5
6
 
 
7
...
4
5
6
7
8
9
0
@@ -4,3 +4,5 @@ mack::session_id: _fake_application_session_id
0
 
0
 orm: data_mapper
0
 mack::testing_framework: rspec
0
+
0
+use_page_caching: true
0
\ No newline at end of file
...
9
10
11
12
13
14
15
16
 
 
 
 
17
18
19
20
21
22
23
24
 
 
 
 
 
25
26
27
 
 
 
 
 
 
 
 
 
28
29
30
31
 
 
 
 
 
32
33
34
35
36
37
38
39
40
41
 
 
 
 
 
 
 
 
42
43
44
 
 
 
 
 
 
 
45
46
47
...
9
10
11
 
 
 
 
 
12
13
14
15
16
17
18
 
 
 
 
 
19
20
21
22
23
24
25
 
26
27
28
29
30
31
32
33
34
35
 
36
 
37
38
39
40
41
42
 
43
44
45
46
47
48
 
 
49
50
51
52
53
54
55
56
57
58
 
59
60
61
62
63
64
65
66
67
68
0
@@ -9,39 +9,60 @@ describe "Page Caching" do
0
   describe "use_page_caching is turned on" do
0
   
0
     it "should serve a cached page" do
0
-      temp_app_config(:use_page_caching => true) do
0
-        Cachetastic::Caches::PageCache.set("/", Mack::Caching::PageCaching::Page.new("hello"))
0
-        get "/"
0
-        response.body.should == "hello"
0
-      end
0
+      Cachetastic::Caches::PageCache.set("/", Mack::Caching::PageCaching::Page.new("hello"))
0
+      get "/"
0
+      response.body.should == "hello"
0
+      response.content_type.should == "text/html"
0
     end
0
     
0
     it "should cache a page designated to be cached" do
0
-      temp_app_config(:use_page_caching => true) do
0
-        get "/"
0
-        response.body.should match(/Welcome to your Mack application!/)
0
-        Cachetastic::Caches::PageCache.get("/").to_s.should == response.body
0
-      end
0
+      get "/default/hello_world?name=mark"
0
+      response.body.should match(/<p>mark<\/p>/)
0
+      page = Cachetastic::Caches::PageCache.get("/default/hello_world?name=mark")
0
+      page.to_s.should == response.body
0
+      page.content_type.should == "text/html"
0
     end
0
     
0
-    it "should store the content type correctly"
0
+    it "should store and deliver the content type correctly" do
0
+      get "/default/hello_world.xml?name=mark"
0
+      response.body.should match(/<name>mark<\/name>/)
0
+      response.content_type.should == "application/xml; text/xml"
0
+      old_body = response.body
0
+      get "/default/hello_world.xml?name=mark"
0
+      response.body.should == old_body
0
+      response.content_type.should == "application/xml; text/xml"
0
+    end
0
     
0
-    it "should deliver the content type correctly"
0
     
0
-    it "should store the status correctly"
0
+    it "should never store non-successful pages" do
0
+      get "/default/always_500"
0
+      response.status.should == 500
0
+      Cachetastic::Caches::PageCache.get("/default/always_500").should be_nil
0
+    end
0
     
0
-    it "should deliver the status correctly"
0
     
0
   end
0
   
0
   describe "use_page_caching is turned off" do
0
   
0
     it "should serve the uncached page" do
0
-      get "/"
0
-      response.body.should match(/Welcome to your Mack application!/)
0
+      temp_app_config("use_page_caching" => false) do
0
+        get "/default/hello_world?name=mark"
0
+        response.body.should match(/<p>mark<\/p>/)
0
+        old_body = response.body
0
+        get "/default/hello_world?name=mark"
0
+        response.body.should match(/<p>mark<\/p>/)
0
+        response.body.should_not == old_body
0
+      end
0
     end
0
     
0
-    it "should not store the cached page"
0
+    it "should not store the cached page" do
0
+      temp_app_config("use_page_caching" => false) do
0
+        get "/default/hello_world?name=mark"
0
+        response.body.should match(/<p>mark<\/p>/)
0
+        Cachetastic::Caches::PageCache.get("/default/hello_world?name=mark").should be_nil
0
+      end
0
+    end
0
     
0
   end
0
   

Comments