merb-cache 0.9.2 → 0.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +8 -5
- data/lib/merb-cache/cache-action.rb +1 -1
- data/lib/merb-cache/cache-page.rb +14 -4
- data/lib/merb-cache/merb-cache.rb +1 -0
- data/spec/config/database.yml +14 -0
- data/spec/controller.rb +58 -0
- data/spec/merb-cache-action_spec.rb +106 -0
- data/spec/merb-cache-fragment_spec.rb +100 -0
- data/spec/merb-cache-page_spec.rb +150 -0
- data/spec/merb-cache_spec.rb +15 -0
- data/spec/spec_helper.rb +83 -0
- data/spec/views/cache_controller/action1.html.erb +4 -0
- data/spec/views/cache_controller/action2.html.haml +4 -0
- metadata +15 -3
data/Rakefile
CHANGED
@@ -5,7 +5,8 @@ require "spec/rake/spectask"
|
|
5
5
|
|
6
6
|
PLUGIN = "merb-cache"
|
7
7
|
NAME = "merb-cache"
|
8
|
-
MERB_CACHE_VERSION = Merb::MORE_VERSION rescue "0.9.
|
8
|
+
MERB_CACHE_VERSION = Merb::MORE_VERSION rescue "0.9.3"
|
9
|
+
VERSION = "0.9.3"
|
9
10
|
AUTHOR = "Alex Boussinet"
|
10
11
|
EMAIL = "alex.boussinet@gmail.com"
|
11
12
|
HOMEPAGE = "http://www.merbivore.com"
|
@@ -22,10 +23,10 @@ spec = Gem::Specification.new do |s|
|
|
22
23
|
s.author = AUTHOR
|
23
24
|
s.email = EMAIL
|
24
25
|
s.homepage = HOMEPAGE
|
25
|
-
s.add_dependency('merb-core', '>= 0.9.
|
26
|
+
s.add_dependency('merb-core', '>= 0.9.3')
|
26
27
|
s.require_path = 'lib'
|
27
28
|
s.autorequire = PLUGIN
|
28
|
-
s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,
|
29
|
+
s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,spec}/**/*")
|
29
30
|
|
30
31
|
# rdoc
|
31
32
|
s.has_rdoc = true
|
@@ -63,15 +64,17 @@ Rake::GemPackageTask.new(spec) do |pkg|
|
|
63
64
|
pkg.gem_spec = spec
|
64
65
|
end
|
65
66
|
|
67
|
+
install_home = ENV['GEM_HOME'] ? "-i #{ENV['GEM_HOME']}" : ""
|
68
|
+
|
66
69
|
task :install => [:package] do
|
67
|
-
sh %{sudo gem install pkg/#{NAME}-#{MERB_CACHE_VERSION} --no-update-sources}
|
70
|
+
sh %{sudo gem install #{install_home} pkg/#{NAME}-#{MERB_CACHE_VERSION} --no-update-sources}
|
68
71
|
end
|
69
72
|
|
70
73
|
namespace :jruby do
|
71
74
|
|
72
75
|
desc "Run :package and install the resulting .gem with jruby"
|
73
76
|
task :install => :package do
|
74
|
-
sh %{#{SUDO} jruby -S gem install pkg/#{NAME}-#{Merb::VERSION}.gem --no-rdoc --no-ri}
|
77
|
+
sh %{#{SUDO} jruby -S gem install #{install_home} pkg/#{NAME}-#{Merb::VERSION}.gem --no-rdoc --no-ri}
|
75
78
|
end
|
76
79
|
|
77
80
|
end
|
@@ -28,7 +28,7 @@ module Merb::Cache::ControllerClassMethods
|
|
28
28
|
# ==== Example
|
29
29
|
# cache_actions :mostly_static, [:barely_dynamic, 10]
|
30
30
|
def cache_actions(*actions)
|
31
|
-
if actions.any? && Merb::Cache.cached_actions.
|
31
|
+
if actions.any? && !Merb::Cache.cached_actions.key?(controller_name)
|
32
32
|
before(:cache_action_before)
|
33
33
|
after(:cache_action_after)
|
34
34
|
end
|
@@ -29,7 +29,7 @@ module Merb::Cache::ControllerClassMethods
|
|
29
29
|
# ==== Example
|
30
30
|
# cache_pages :mostly_static, [:barely_dynamic, 10]
|
31
31
|
def cache_pages(*pages)
|
32
|
-
if pages.any? && Merb::Cache.cached_pages.
|
32
|
+
if pages.any? && !Merb::Cache.cached_pages.key?(controller_name)
|
33
33
|
before(:cache_page_before)
|
34
34
|
after(:cache_page_after)
|
35
35
|
end
|
@@ -44,6 +44,8 @@ end
|
|
44
44
|
module Merb::Cache::ControllerInstanceMethods
|
45
45
|
# Mixed in Merb::Controller. Provides methods related to page caching
|
46
46
|
|
47
|
+
DEFAULT_PAGE_EXTENSION = 'html'
|
48
|
+
|
47
49
|
# Checks whether a cache entry exists
|
48
50
|
#
|
49
51
|
# ==== Parameter
|
@@ -54,9 +56,11 @@ module Merb::Cache::ControllerInstanceMethods
|
|
54
56
|
#
|
55
57
|
# ==== Example
|
56
58
|
# cached_page?(:action => 'show', :params => [params[:page]])
|
59
|
+
# cached_page?(:action => 'show', :extension => 'js')
|
57
60
|
def cached_page?(options)
|
58
61
|
key = Merb::Controller._cache.key_for(options, controller_name, true)
|
59
|
-
|
62
|
+
extension = options[:extension] || DEFAULT_PAGE_EXTENSION
|
63
|
+
File.file?(Merb::Controller._cache.config[:cache_html_directory] / "#{key}.#{extension}")
|
60
64
|
end
|
61
65
|
|
62
66
|
# Expires the page identified by the key computed after the parameters
|
@@ -67,13 +71,15 @@ module Merb::Cache::ControllerInstanceMethods
|
|
67
71
|
# ==== Examples
|
68
72
|
# expire_page(:action => 'show', :controller => 'news')
|
69
73
|
# expire_page(:action => 'show', :match => true)
|
74
|
+
# expire_page(:action => 'show', :extension => 'js')
|
70
75
|
def expire_page(options)
|
71
76
|
config_dir = Merb::Controller._cache.config[:cache_html_directory]
|
72
77
|
Merb::Controller._cache.expire_key_for(options, controller_name, true) do |key, match|
|
73
78
|
if match
|
74
79
|
files = Dir.glob(config_dir / "#{key}*")
|
75
80
|
else
|
76
|
-
|
81
|
+
extension = options[:extension] || DEFAULT_PAGE_EXTENSION
|
82
|
+
files = config_dir / "#{key}.#{extension}"
|
77
83
|
end
|
78
84
|
FileUtils.rm_rf(files)
|
79
85
|
end
|
@@ -106,14 +112,18 @@ module Merb::Cache::ControllerInstanceMethods
|
|
106
112
|
# If request.path is "/", the name will be "/index.html"
|
107
113
|
# If request.path is "/news/show/1", the name will be "/news/show/1.html"
|
108
114
|
# If request.path is "/news/show/", the name will be "/news/show.html"
|
115
|
+
# If request.path is "/news/styles.css", the name will be "/news/styles.css"
|
109
116
|
def _cache_page(data = nil)
|
117
|
+
return if Merb::Controller._cache.config[:disable_page_caching]
|
110
118
|
controller = controller_name
|
111
119
|
action = action_name.to_sym
|
112
120
|
pages = Merb::Controller._cache.cached_pages[controller]
|
113
121
|
return unless pages && pages.key?(action)
|
114
122
|
path = request.path.chomp("/")
|
115
123
|
path = "index" if path.empty?
|
116
|
-
|
124
|
+
ext = "." + (params[:format].empty? ? DEFAULT_PAGE_EXTENSION : params[:format])
|
125
|
+
ext = nil if File.extname(path) == ext
|
126
|
+
cache_file = Merb::Controller._cache.config[:cache_html_directory] / "#{path}#{ext}"
|
117
127
|
if data
|
118
128
|
cache_directory = File.dirname(cache_file)
|
119
129
|
FileUtils.mkdir_p(cache_directory)
|
@@ -40,6 +40,7 @@ class Merb::Cache
|
|
40
40
|
def start
|
41
41
|
@config = DEFAULT_CONFIG.merge(Merb::Plugins.config[:merb_cache] || {})
|
42
42
|
if @config[:disable] == true || Merb.environment == @config[:disable]
|
43
|
+
config[:disable_page_caching] = true
|
43
44
|
config[:store] = "dummy"
|
44
45
|
end
|
45
46
|
@config[:cache_html_directory] ||= Merb.dir_for(:public) / "cache"
|
@@ -0,0 +1,14 @@
|
|
1
|
+
---
|
2
|
+
:development: &defaults
|
3
|
+
:adapter: sqlite3
|
4
|
+
:database: tmp/merb-cache.sqlite3
|
5
|
+
:log_stream: log/sql.log
|
6
|
+
:log_level: 0
|
7
|
+
|
8
|
+
:test:
|
9
|
+
<<: *defaults
|
10
|
+
:database: tmp/merb-cache.sqlite3
|
11
|
+
|
12
|
+
:production:
|
13
|
+
<<: *defaults
|
14
|
+
:database: tmp/merb-cache.sqlite3
|
data/spec/controller.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
class CacheController < Merb::Controller
|
2
|
+
self._template_root = File.dirname(__FILE__) / "views"
|
3
|
+
|
4
|
+
cache_action :action3
|
5
|
+
cache_action :action4, 0.05
|
6
|
+
# or cache_actions :action3, [:action4, 0.05]
|
7
|
+
|
8
|
+
cache_page :action5
|
9
|
+
cache_page :action6, 0.05
|
10
|
+
# or cache_pages :action5, [:action6, 0.05]
|
11
|
+
cache_page :action7
|
12
|
+
|
13
|
+
def action1
|
14
|
+
render
|
15
|
+
end
|
16
|
+
|
17
|
+
def action2
|
18
|
+
render
|
19
|
+
end
|
20
|
+
|
21
|
+
def action3
|
22
|
+
"test action3"
|
23
|
+
end
|
24
|
+
|
25
|
+
def action4
|
26
|
+
"test action4"
|
27
|
+
end
|
28
|
+
|
29
|
+
def action5
|
30
|
+
"test action5"
|
31
|
+
end
|
32
|
+
|
33
|
+
def action6
|
34
|
+
Time.now.to_s
|
35
|
+
end
|
36
|
+
|
37
|
+
def action7
|
38
|
+
provides :js, :css, :html, :xml, :jpg
|
39
|
+
case params[:format]
|
40
|
+
when "css"
|
41
|
+
"CSS"
|
42
|
+
when "js"
|
43
|
+
"JS"
|
44
|
+
when "html"
|
45
|
+
"HTML"
|
46
|
+
when "xml"
|
47
|
+
"XML"
|
48
|
+
when "jpg"
|
49
|
+
"JPG"
|
50
|
+
else
|
51
|
+
raise "BAD FORMAT: #{params[:format].inspect}"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def index
|
56
|
+
"test index"
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,106 @@
|
|
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
|
+
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"
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,83 @@
|
|
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
|
+
}
|
24
|
+
FileUtils.rm_rf(Dir.glob(File.dirname(__FILE__) / "/tmp"))
|
25
|
+
case store
|
26
|
+
when "dummy"
|
27
|
+
when "file"
|
28
|
+
when "memory"
|
29
|
+
when "memcache"
|
30
|
+
require "memcache"
|
31
|
+
when "database"
|
32
|
+
case orm
|
33
|
+
when "datamapper"
|
34
|
+
Merb.environment = "test"
|
35
|
+
Merb.logger = Merb::Logger.new("log/merb_test.log")
|
36
|
+
set_database_adapter("sqlite3")
|
37
|
+
require "merb_datamapper"
|
38
|
+
when "activerecord"
|
39
|
+
Merb.logger = Merb::Logger.new("log/merb_test.log")
|
40
|
+
set_database_adapter("sqlite3")
|
41
|
+
require "merb_activerecord"
|
42
|
+
when "sequel"
|
43
|
+
set_database_adapter("sqlite")
|
44
|
+
require "merb_sequel"
|
45
|
+
else
|
46
|
+
raise "Unknown orm: #{orm}"
|
47
|
+
end
|
48
|
+
else
|
49
|
+
raise "Unknown cache store: #{store}"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
store = "file"
|
54
|
+
case ENV["STORE"] || store
|
55
|
+
when "file"
|
56
|
+
use_cache_store "file"
|
57
|
+
when "memory"
|
58
|
+
use_cache_store "memory"
|
59
|
+
when "memcache"
|
60
|
+
use_cache_store "memcache"
|
61
|
+
when "datamapper"
|
62
|
+
use_cache_store "database", "datamapper"
|
63
|
+
when "sequel"
|
64
|
+
use_cache_store "database", "sequel"
|
65
|
+
when "activerecord"
|
66
|
+
use_cache_store "database", "activerecord"
|
67
|
+
when "dummy"
|
68
|
+
use_cache_store "dummy"
|
69
|
+
else
|
70
|
+
puts "Invalid cache store: #{ENV["store"]}"
|
71
|
+
exit
|
72
|
+
end
|
73
|
+
|
74
|
+
require "fileutils"
|
75
|
+
FileUtils.mkdir_p(Merb::Plugins.config[:merb_cache][:cache_html_directory])
|
76
|
+
FileUtils.mkdir_p(Merb::Plugins.config[:merb_cache][:cache_directory])
|
77
|
+
|
78
|
+
Merb.start :environment => "test", :adapter => "runner"
|
79
|
+
|
80
|
+
require "merb-core/test"
|
81
|
+
Spec::Runner.configure do |config|
|
82
|
+
config.include Merb::Test::RequestHelper
|
83
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: merb-cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Boussinet
|
@@ -9,7 +9,7 @@ autorequire: merb-cache
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-05-04 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
requirements:
|
20
20
|
- - ">="
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: 0.9.
|
22
|
+
version: 0.9.3
|
23
23
|
version:
|
24
24
|
description: Merb plugin that provides caching (page, action, fragment, object)
|
25
25
|
email: alex.boussinet@gmail.com
|
@@ -52,6 +52,18 @@ files:
|
|
52
52
|
- lib/merb-cache/merb-cache.rb
|
53
53
|
- lib/merb-cache/merbtasks.rb
|
54
54
|
- lib/merb-cache.rb
|
55
|
+
- spec/config
|
56
|
+
- spec/config/database.yml
|
57
|
+
- spec/controller.rb
|
58
|
+
- spec/merb-cache-action_spec.rb
|
59
|
+
- spec/merb-cache-fragment_spec.rb
|
60
|
+
- spec/merb-cache-page_spec.rb
|
61
|
+
- spec/merb-cache_spec.rb
|
62
|
+
- spec/spec_helper.rb
|
63
|
+
- spec/views
|
64
|
+
- spec/views/cache_controller
|
65
|
+
- spec/views/cache_controller/action1.html.erb
|
66
|
+
- spec/views/cache_controller/action2.html.haml
|
55
67
|
has_rdoc: true
|
56
68
|
homepage: http://www.merbivore.com
|
57
69
|
post_install_message:
|