sinatra-cache 0.3.4 → 0.3.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +11 -0
- data/VERSION +1 -1
- data/lib/sinatra/cache.rb +1 -1
- data/lib/sinatra/templates.rb +15 -6
- data/sinatra-cache.gemspec +7 -4
- data/spec/fixtures/apps/partials/views/home/index.erb +4 -0
- data/spec/fixtures/apps/partials/views/layout.erb +11 -0
- data/spec/fixtures/apps/partials/views/shared/_header.erb +3 -0
- data/spec/sinatra/cache_spec.rb +178 -1
- metadata +20 -4
data/README.rdoc
CHANGED
@@ -374,6 +374,17 @@ Report it here: http://github.com/kematzy/sinatra-cache/issues
|
|
374
374
|
|
375
375
|
* Work out and include instructions on how to use a '../public/custom/cache/dir' with Passenger.
|
376
376
|
|
377
|
+
* Enable time-based / date-based cache expiry and regeneration of the cached-pages. [ht oakleafs]
|
378
|
+
|
379
|
+
* Enable .gz version of the cached file, further reducing the processing on the server. [ht oakleafs]
|
380
|
+
It would be killer to have <b>an extra .gz file next to the cached file</b>. That way, in Apache, you set it up like that:
|
381
|
+
|
382
|
+
RewriteCond %{HTTP:Accept-Encoding} gzip
|
383
|
+
RewriteCond %{REQUEST_FILENAME}.gz$ -f
|
384
|
+
RewriteRule ^(.*)$ $1.gz [L,QSA]
|
385
|
+
|
386
|
+
And it should serve the compressed file if available.
|
387
|
+
|
377
388
|
* Write more tests to ensure everything is very solid.
|
378
389
|
|
379
390
|
* Any other improvements you or I can think of.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.5
|
data/lib/sinatra/cache.rb
CHANGED
data/lib/sinatra/templates.rb
CHANGED
@@ -15,12 +15,15 @@ module Sinatra
|
|
15
15
|
def render(engine, data, options={}, locals={}, &block)
|
16
16
|
# merge app-level options
|
17
17
|
options = settings.send(engine).merge(options) if settings.respond_to?(engine)
|
18
|
+
options[:outvar] ||= '@_out_buf'
|
18
19
|
|
19
20
|
# extract generic options
|
20
|
-
locals
|
21
|
-
views
|
22
|
-
|
23
|
-
layout
|
21
|
+
locals = options.delete(:locals) || locals || {}
|
22
|
+
views = options.delete(:views) || settings.views || "./views"
|
23
|
+
@default_layout = :layout if @default_layout.nil?
|
24
|
+
layout = options.delete(:layout)
|
25
|
+
layout = @default_layout if layout.nil? or layout == true
|
26
|
+
content_type = options.delete(:content_type) || options.delete(:default_content_type)
|
24
27
|
|
25
28
|
# set the cache related options
|
26
29
|
cache_enabled = settings.respond_to?(:cache_enabled) ? settings.send(:cache_enabled) : false
|
@@ -30,8 +33,11 @@ module Sinatra
|
|
30
33
|
cache_option = true if cache_option.nil?
|
31
34
|
|
32
35
|
# compile and render template
|
33
|
-
|
34
|
-
|
36
|
+
layout_was = @default_layout
|
37
|
+
@default_layout = false
|
38
|
+
template = compile_template(engine, data, options, views)
|
39
|
+
output = template.render(self, locals, &block)
|
40
|
+
@default_layout = layout_was
|
35
41
|
|
36
42
|
# render layout
|
37
43
|
if layout
|
@@ -48,6 +54,9 @@ module Sinatra
|
|
48
54
|
# rendering without a layout
|
49
55
|
(cache_enabled && cache_option && settings.send(:environment) == settings.cache_environment) ?
|
50
56
|
cache_write_file(cache_file_path, output.gsub(/\n\r?$/,"") ) : output
|
57
|
+
|
58
|
+
output.extend(ContentTyped).content_type = content_type if content_type
|
59
|
+
output
|
51
60
|
end
|
52
61
|
|
53
62
|
end #/module Templates
|
data/sinatra-cache.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{sinatra-cache}
|
8
|
-
s.version = "0.3.
|
8
|
+
s.version = "0.3.5"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["kematzy"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-11-15}
|
13
13
|
s.description = %q{A Sinatra Extension that makes Page and Fragment Caching easy.}
|
14
14
|
s.email = %q{kematzy@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -32,13 +32,16 @@ Gem::Specification.new do |s|
|
|
32
32
|
"spec/fixtures/apps/base/views/index.erb",
|
33
33
|
"spec/fixtures/apps/base/views/layout.erb",
|
34
34
|
"spec/fixtures/apps/base/views/params.erb",
|
35
|
+
"spec/fixtures/apps/partials/views/home/index.erb",
|
36
|
+
"spec/fixtures/apps/partials/views/layout.erb",
|
37
|
+
"spec/fixtures/apps/partials/views/shared/_header.erb",
|
35
38
|
"spec/sinatra/cache_spec.rb",
|
36
39
|
"spec/spec_helper.rb"
|
37
40
|
]
|
38
41
|
s.homepage = %q{http://github.com/kematzy/sinatra-cache}
|
39
42
|
s.rdoc_options = ["--charset=UTF-8"]
|
40
43
|
s.require_paths = ["lib"]
|
41
|
-
s.rubygems_version = %q{1.3.
|
44
|
+
s.rubygems_version = %q{1.3.7}
|
42
45
|
s.summary = %q{A Sinatra Extension that makes Page and Fragment Caching easy.}
|
43
46
|
s.test_files = [
|
44
47
|
"spec/sinatra/cache_spec.rb",
|
@@ -49,7 +52,7 @@ Gem::Specification.new do |s|
|
|
49
52
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
50
53
|
s.specification_version = 3
|
51
54
|
|
52
|
-
if Gem::Version.new(Gem::
|
55
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
53
56
|
s.add_runtime_dependency(%q<sinatra>, [">= 1.0"])
|
54
57
|
s.add_runtime_dependency(%q<sinatra-outputbuffer>, [">= 0.1.0"])
|
55
58
|
s.add_development_dependency(%q<sinatra-tests>, [">= 0.1.6"])
|
data/spec/sinatra/cache_spec.rb
CHANGED
@@ -426,7 +426,7 @@ describe "Sinatra" do
|
|
426
426
|
end
|
427
427
|
|
428
428
|
it "should output the correct CSS as expected" do
|
429
|
-
body.should == "body { color: red; }"
|
429
|
+
body.should == "body { color: red; }\n"
|
430
430
|
end
|
431
431
|
|
432
432
|
it "should automatically create the cache /css output directory" do
|
@@ -656,6 +656,183 @@ describe "Sinatra" do
|
|
656
656
|
|
657
657
|
end #/ with caching enabled
|
658
658
|
|
659
|
+
describe "EDGE cases" do
|
660
|
+
|
661
|
+
describe "Using nested buffers" do
|
662
|
+
|
663
|
+
it "should use Sinatra v1.1" do
|
664
|
+
Sinatra::VERSION.should == '1.1.0'
|
665
|
+
end
|
666
|
+
|
667
|
+
require 'sinatra/outputbuffer'
|
668
|
+
|
669
|
+
class MyPartialsTestApp < Sinatra::Base
|
670
|
+
register(Sinatra::Tests)
|
671
|
+
|
672
|
+
enable :raise_errors
|
673
|
+
|
674
|
+
register(Sinatra::OutputBuffer)
|
675
|
+
register(Sinatra::Cache)
|
676
|
+
|
677
|
+
# need to set the root of the app for the default :cache_fragments_output_dir to work
|
678
|
+
set :root, ::APP_ROOT
|
679
|
+
set :app_dir, "#{APP_ROOT}/apps/partials"
|
680
|
+
set :public, "#{fixtures_path}/public"
|
681
|
+
set :views, "#{APP_ROOT}/apps/partials/views"
|
682
|
+
|
683
|
+
set :cache_enabled, true
|
684
|
+
set :cache_environment, :test
|
685
|
+
set :cache_output_dir, "#{test_cache_path('system/cache')}"
|
686
|
+
set :cache_fragments_output_dir, "#{test_cache_path('system/cache')}_fragments"
|
687
|
+
|
688
|
+
# NB! Although without tests, the positioning of the custom method in relation to other
|
689
|
+
# Cache related declaration has no effect.
|
690
|
+
helpers do
|
691
|
+
def uncached_erb(template, options={})
|
692
|
+
erb(template, options.merge(:cache => false ))
|
693
|
+
end
|
694
|
+
|
695
|
+
##
|
696
|
+
# Renders ERB partials
|
697
|
+
#
|
698
|
+
# ==== Examples
|
699
|
+
#
|
700
|
+
# <%= partial('shared/header') %> => renders app/views/shared/_header.erb
|
701
|
+
#
|
702
|
+
# <%= partial('snippets/snippet') %> => renders app/views/snippets/_snippet.erb
|
703
|
+
#
|
704
|
+
#
|
705
|
+
# @api public
|
706
|
+
def partial(template, *args)
|
707
|
+
template_array = template.to_s.split('/')
|
708
|
+
template = template_array[0..-2].join('/') + "/_#{template_array[-1]}"
|
709
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
710
|
+
options.merge!(:layout => false, :cache => false) # add support for Sinatra::Cache
|
711
|
+
# options[:outvar] = '@_out_buf'
|
712
|
+
if collection = options.delete(:collection) then
|
713
|
+
collection.inject([]) do |buffer, member|
|
714
|
+
buffer << erb(:"#{template}", options.merge(:layout => false, :locals => { template_array[-1].to_sym => member } ) )
|
715
|
+
end.join("\n")
|
716
|
+
else
|
717
|
+
erb(:"#{template}", options)
|
718
|
+
end
|
719
|
+
rescue Errno::ENOENT => e
|
720
|
+
out = "ERROR: The partial [views/#{template.to_s}] is missing."
|
721
|
+
out << " Please create it to remove this error. [ Exception: #{h e.inspect}]" if self.class.development? || self.class.test?
|
722
|
+
out
|
723
|
+
rescue Exception => e
|
724
|
+
if self.class.production?
|
725
|
+
# TODO:: must store the error in the log somehow.
|
726
|
+
"<!-- ERROR: rendering the partial [#{template.to_s}] -->"
|
727
|
+
else
|
728
|
+
throw e
|
729
|
+
end
|
730
|
+
end
|
731
|
+
|
732
|
+
end
|
733
|
+
|
734
|
+
get('/') { erb(:"home/index") }
|
735
|
+
# get('/erb/?') { erb(:index, :layout => false) }
|
736
|
+
|
737
|
+
# complex regex URL, matches
|
738
|
+
get %r{^/params/?([\s\w-]+)?/?([\w-]+)?/?([\w-]+)?/?([\w-]+)?/?([\w-]+)?/?([\w-]+)?} do
|
739
|
+
@captures = params[:captures].nil? ? [] : params[:captures].compact
|
740
|
+
erb(:params)
|
741
|
+
end
|
742
|
+
get('/file-extensions.*') do
|
743
|
+
@vars = { :framework => "Sinatra", :url => "www.sinatrarb.com" }
|
744
|
+
case params[:splat].first.to_sym
|
745
|
+
when :json
|
746
|
+
erb(@vars.to_json, :layout => false )
|
747
|
+
when :yaml
|
748
|
+
erb(@vars.to_yaml,:layout => false)
|
749
|
+
else
|
750
|
+
# direct output, should NOT be Cached
|
751
|
+
@vars.inspect
|
752
|
+
# @vars.inspect
|
753
|
+
end
|
754
|
+
end
|
755
|
+
|
756
|
+
## NO CACHING
|
757
|
+
get('/uncached/erb'){ erb(:index, :cache => false) }
|
758
|
+
get('/uncached/erb/no/layout'){ erb(:index, :cache => false, :layout => false ) }
|
759
|
+
get('/uncached/uncached_erb'){ uncached_erb(:index) }
|
760
|
+
|
761
|
+
get '/css/screen.css' do
|
762
|
+
content_type 'text/css'
|
763
|
+
sass(:css, :style => :compact)
|
764
|
+
end
|
765
|
+
get '/css/no/cache.css' do
|
766
|
+
content_type 'text/css'
|
767
|
+
sass(:css, :style => :compact, :cache => false)
|
768
|
+
end
|
769
|
+
|
770
|
+
# enable :method_override
|
771
|
+
post('/post/?') { erb("POST", :layout => false) }
|
772
|
+
put('/put/?') { erb('PUT', :layout => false) }
|
773
|
+
delete('/delete/?') { erb('DELETE', :layout => false) }
|
774
|
+
|
775
|
+
get %r{^/fragments/?([\s\w-]+)?/?([\w-]+)?/?([\w-]+)?/?([\w-]+)?/?([\w-]+)?/?([\w-]+)?} do
|
776
|
+
erb(:fragments, :layout => false, :cache => false)
|
777
|
+
end
|
778
|
+
get %r{^/sharedfragments/?([\s\w-]+)?/?([\w-]+)?/?([\w-]+)?/?([\w-]+)?/?([\w-]+)?/?([\w-]+)?} do
|
779
|
+
erb(:fragments_shared, :layout => false, :cache => false)
|
780
|
+
end
|
781
|
+
|
782
|
+
end
|
783
|
+
|
784
|
+
def app; ::MyPartialsTestApp.new ; end
|
785
|
+
|
786
|
+
describe "basic Page caching" do
|
787
|
+
|
788
|
+
describe "GET requests for" do
|
789
|
+
|
790
|
+
describe "the Home page - ['/' => /index.html] (with layout)" do
|
791
|
+
|
792
|
+
before(:each) do
|
793
|
+
@cache_file = "#{test_cache_path('system/cache')}/index.html"
|
794
|
+
get('/')
|
795
|
+
end
|
796
|
+
|
797
|
+
after(:each) do
|
798
|
+
FileUtils.rm(@cache_file) if @delete_cached_test_files
|
799
|
+
end
|
800
|
+
|
801
|
+
it "should render the expected output" do
|
802
|
+
# <html>
|
803
|
+
# <head>
|
804
|
+
# <title>Sinatra::Cache</title>
|
805
|
+
# <!-- page cached: 2010-11-15 18:21:06 -->
|
806
|
+
# </head>
|
807
|
+
# <body>
|
808
|
+
# <div id="header">
|
809
|
+
# <h1>HELLO from [content_for(:section_header)]</h1>
|
810
|
+
# </div>
|
811
|
+
# <h2>HELLO FROM [FILE ../sinatra-cache/spec/fixtures/apps/partials/views/home/index.erb]</h2>
|
812
|
+
# </body>
|
813
|
+
# </html>
|
814
|
+
body.should have_tag('html > head > title', 'Sinatra::Cache')
|
815
|
+
body.should match(/<!-- page cached: \d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d -->/)
|
816
|
+
body.should have_tag('html > body > div#header > h1', /HELLO from \[content_for\(:section_header\)\]/)
|
817
|
+
body.should have_tag('html > body > h2', /HELLO FROM \[/)
|
818
|
+
end
|
819
|
+
|
820
|
+
it "should create a cached file in the cache output directory" do
|
821
|
+
test(?f, @cache_file).should == true
|
822
|
+
end
|
823
|
+
|
824
|
+
end #/ GET ['/' => /index.html]
|
825
|
+
|
826
|
+
end #/ GET requests
|
827
|
+
|
828
|
+
end #/ basic Page caching
|
829
|
+
|
830
|
+
|
831
|
+
end #/ Using nested buffers
|
832
|
+
|
833
|
+
end #/ EDGE cases
|
834
|
+
|
835
|
+
|
659
836
|
end #/ Cache
|
660
837
|
|
661
838
|
end #/ Sinatra
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 25
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
8
|
- 3
|
8
|
-
-
|
9
|
-
version: 0.3.
|
9
|
+
- 5
|
10
|
+
version: 0.3.5
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- kematzy
|
@@ -14,16 +15,18 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2010-
|
18
|
+
date: 2010-11-15 00:00:00 +08:00
|
18
19
|
default_executable:
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
21
22
|
name: sinatra
|
22
23
|
prerelease: false
|
23
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
24
26
|
requirements:
|
25
27
|
- - ">="
|
26
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 15
|
27
30
|
segments:
|
28
31
|
- 1
|
29
32
|
- 0
|
@@ -34,9 +37,11 @@ dependencies:
|
|
34
37
|
name: sinatra-outputbuffer
|
35
38
|
prerelease: false
|
36
39
|
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
37
41
|
requirements:
|
38
42
|
- - ">="
|
39
43
|
- !ruby/object:Gem::Version
|
44
|
+
hash: 27
|
40
45
|
segments:
|
41
46
|
- 0
|
42
47
|
- 1
|
@@ -48,9 +53,11 @@ dependencies:
|
|
48
53
|
name: sinatra-tests
|
49
54
|
prerelease: false
|
50
55
|
requirement: &id003 !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
51
57
|
requirements:
|
52
58
|
- - ">="
|
53
59
|
- !ruby/object:Gem::Version
|
60
|
+
hash: 23
|
54
61
|
segments:
|
55
62
|
- 0
|
56
63
|
- 1
|
@@ -62,9 +69,11 @@ dependencies:
|
|
62
69
|
name: rspec
|
63
70
|
prerelease: false
|
64
71
|
requirement: &id004 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
65
73
|
requirements:
|
66
74
|
- - ">="
|
67
75
|
- !ruby/object:Gem::Version
|
76
|
+
hash: 27
|
68
77
|
segments:
|
69
78
|
- 1
|
70
79
|
- 3
|
@@ -97,6 +106,9 @@ files:
|
|
97
106
|
- spec/fixtures/apps/base/views/index.erb
|
98
107
|
- spec/fixtures/apps/base/views/layout.erb
|
99
108
|
- spec/fixtures/apps/base/views/params.erb
|
109
|
+
- spec/fixtures/apps/partials/views/home/index.erb
|
110
|
+
- spec/fixtures/apps/partials/views/layout.erb
|
111
|
+
- spec/fixtures/apps/partials/views/shared/_header.erb
|
100
112
|
- spec/sinatra/cache_spec.rb
|
101
113
|
- spec/spec_helper.rb
|
102
114
|
has_rdoc: true
|
@@ -109,23 +121,27 @@ rdoc_options:
|
|
109
121
|
require_paths:
|
110
122
|
- lib
|
111
123
|
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
+
none: false
|
112
125
|
requirements:
|
113
126
|
- - ">="
|
114
127
|
- !ruby/object:Gem::Version
|
128
|
+
hash: 3
|
115
129
|
segments:
|
116
130
|
- 0
|
117
131
|
version: "0"
|
118
132
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
133
|
+
none: false
|
119
134
|
requirements:
|
120
135
|
- - ">="
|
121
136
|
- !ruby/object:Gem::Version
|
137
|
+
hash: 3
|
122
138
|
segments:
|
123
139
|
- 0
|
124
140
|
version: "0"
|
125
141
|
requirements: []
|
126
142
|
|
127
143
|
rubyforge_project:
|
128
|
-
rubygems_version: 1.3.
|
144
|
+
rubygems_version: 1.3.7
|
129
145
|
signing_key:
|
130
146
|
specification_version: 3
|
131
147
|
summary: A Sinatra Extension that makes Page and Fragment Caching easy.
|