kematzy-sinatra-cache 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +48 -26
- data/VERSION.yml +1 -1
- data/lib/sinatra/cache.rb +6 -6
- data/test/cache_test.rb +13 -29
- metadata +1 -1
data/README.textile
CHANGED
@@ -2,26 +2,43 @@ h1. Sinatra::Cache
|
|
2
2
|
|
3
3
|
Adds a very *simple* Page Caching functionality to "Sinatra":http://www.sinatrarb.com/
|
4
4
|
|
5
|
-
<
|
6
|
-
|
7
|
-
|
5
|
+
<hr>
|
6
|
+
|
7
|
+
h3. *PLEASE KEEP THIS IN MIND*
|
8
|
+
|
9
|
+
This is *unfinished work*, so use with care.
|
8
10
|
|
9
11
|
If you find it lacking or badly coded, please feel free to fork and improve. I'll be very happy :)
|
10
|
-
|
12
|
+
|
13
|
+
<hr>
|
11
14
|
|
12
15
|
|
13
|
-
h3.
|
16
|
+
h3. TODO's
|
14
17
|
|
15
|
-
* Improve the logging output to the standard Sinatra output (if we are having :logging enabled )
|
16
|
-
* Add fragment and action (?) caching...
|
17
|
-
* Enable the options functionality for the <tt>:cache</tt> & <tt>:cache_expire</tt> methods, so that they can take custom extensions and more
|
18
18
|
|
19
|
+
= Make caching an option of the normal <tt>erb()/haml()/sass()</tt> methods, sort of like this:
|
20
|
+
<pre><code>
|
21
|
+
erb(:index, :cache => true/false)
|
22
|
+
|
23
|
+
# alternatively enable caching by setting a global config like this
|
24
|
+
set :cache_enabled_for :all || :only/except => ['/','/contact',...]
|
25
|
+
|
26
|
+
</code></pre>
|
19
27
|
|
20
|
-
h3. Configuration
|
21
28
|
|
22
|
-
|
29
|
+
= Improve the logging output to the standard Sinatra output (if we are having :logging enabled )
|
23
30
|
|
24
|
-
|
31
|
+
= Add fragment caching...
|
32
|
+
|
33
|
+
= Enable the options functionality for the <tt>:cache</tt> & <tt>:cache_expire</tt> methods, so that they can take custom extensions and more
|
34
|
+
|
35
|
+
|
36
|
+
<hr>
|
37
|
+
|
38
|
+
|
39
|
+
h3. CONFIGURATION
|
40
|
+
|
41
|
+
*Example 1: Sinatra 'classic' app.*
|
25
42
|
|
26
43
|
<pre><code>
|
27
44
|
<snip....>
|
@@ -32,41 +49,43 @@ Sinatra 'classic' app.
|
|
32
49
|
# sets the default extension for cached files
|
33
50
|
set :cache_page_extension, '.html'
|
34
51
|
# sets the Cache dir to the root of the /public directory.
|
35
|
-
set :
|
52
|
+
set :cache_output_dir, '' # was :cache_dir
|
36
53
|
...
|
37
54
|
</code></pre>
|
38
55
|
|
39
|
-
*Example 2
|
40
|
-
|
41
|
-
Sinatra 'sub-classed' app.
|
56
|
+
*Example 2: Sinatra 'sub-classed' app.*
|
42
57
|
|
43
58
|
<pre><code>
|
44
59
|
<snip....>
|
45
60
|
require 'sinatra/cache'
|
46
|
-
|
47
|
-
class MyApp < Sinatra::Base
|
48
61
|
|
62
|
+
class MyApp < Sinatra::Base
|
63
|
+
|
49
64
|
register Sinatra::Cache
|
50
|
-
|
65
|
+
|
51
66
|
# toggle for the cache functionality.
|
52
67
|
set :cache_enabled, true
|
53
68
|
# sets the default extension for cached files
|
54
69
|
set :cache_page_extension, '.html'
|
55
|
-
# sets the Cache dir to the root of the /public directory.
|
56
|
-
set :
|
57
|
-
|
70
|
+
# sets the Cache output dir to the root of the /public directory.
|
71
|
+
set :cache_output_dir, '' # was :cache_dir
|
72
|
+
|
58
73
|
<snip....>
|
59
74
|
</code></pre>
|
60
|
-
|
61
75
|
|
62
|
-
|
76
|
+
|
77
|
+
<hr>
|
78
|
+
|
79
|
+
|
80
|
+
h3. USAGE
|
81
|
+
|
63
82
|
|
64
83
|
Basic Page Caching into static HTML files
|
65
84
|
|
66
85
|
<pre><code>
|
67
86
|
get '/contact' do
|
68
87
|
# NB! options is currently not working
|
69
|
-
cache( erb( :contact ), :options => 'go here' )
|
88
|
+
cache( erb( :contact ), :options => 'should go here' )
|
70
89
|
end
|
71
90
|
</code></pre>
|
72
91
|
|
@@ -82,11 +101,14 @@ Expiring old pages (ie: after POST or PUT events)
|
|
82
101
|
See the "test":test/ and "test/fixtures":test/fixtures/ directories for more examples.
|
83
102
|
|
84
103
|
|
85
|
-
|
104
|
+
<hr>
|
105
|
+
|
106
|
+
|
107
|
+
h3. DEFAULT OPTIONS
|
86
108
|
|
87
109
|
* <tt>:cache_enabled</tt> => toggle for the cache functionality. Default is: <tt>true</tt>
|
88
110
|
* <tt>:cache_page_extension</tt> => sets the default extension for cached files. Default is: <tt>.html</tt>
|
89
|
-
* <tt>:
|
111
|
+
* <tt>:cache_output_dir</tt> => sets cache directory where cached files are stored. Default is: ''(empty) == root of /public.<br>
|
90
112
|
set to empty, since the ideal 'system/cache/' does not currently work with Passenger & mod_rewrite :(
|
91
113
|
* <tt>:cache_logging</tt> => toggle for logging the cache calls. Default is: <tt>true</tt>
|
92
114
|
* <tt>:cache_logging_level</tt> => sets the level of the cache logger. Default is: <tt>:info</tt>.<br>
|
data/VERSION.yml
CHANGED
data/lib/sinatra/cache.rb
CHANGED
@@ -9,7 +9,7 @@ module Sinatra
|
|
9
9
|
#
|
10
10
|
module Cache
|
11
11
|
|
12
|
-
VERSION = 'Sinatra::Cache v0.2.
|
12
|
+
VERSION = 'Sinatra::Cache v0.2.2'
|
13
13
|
def self.version; VERSION; end
|
14
14
|
|
15
15
|
|
@@ -30,7 +30,7 @@ module Sinatra
|
|
30
30
|
return content unless options.cache_enabled
|
31
31
|
|
32
32
|
unless content.nil?
|
33
|
-
content = "#{
|
33
|
+
content = "#{content}\n#{page_cached_timestamp}"
|
34
34
|
path = cache_page_path(request.path_info,opts)
|
35
35
|
FileUtils.makedirs(File.dirname(path))
|
36
36
|
open(path, 'wb+') { |f| f << content }
|
@@ -94,9 +94,9 @@ module Sinatra
|
|
94
94
|
def cache_page_path(path,opts={})
|
95
95
|
# test if given a full path rather than relative path, otherwise join the public path to cache_dir
|
96
96
|
# and ensure it is a full path
|
97
|
-
cache_dir = (options.
|
98
|
-
options.
|
99
|
-
cache_dir =
|
97
|
+
cache_dir = (options.cache_output_dir == File.expand_path(options.cache_output_dir)) ?
|
98
|
+
options.cache_output_dir : File.expand_path("#{options.public}/#{options.cache_output_dir}")
|
99
|
+
cache_dir = cache_output_dir[0..-2] if cache_dir[-1,1] == '/'
|
100
100
|
"#{cache_dir}/#{cache_file_name(path,opts)}"
|
101
101
|
end
|
102
102
|
|
@@ -127,7 +127,7 @@ module Sinatra
|
|
127
127
|
app.helpers(Cache::Helpers)
|
128
128
|
app.set :cache_enabled, true
|
129
129
|
app.set :cache_page_extension, '.html'
|
130
|
-
app.set :
|
130
|
+
app.set :cache_output_dir, ''
|
131
131
|
app.set :cache_logging, true
|
132
132
|
app.set :cache_logging_level, :info
|
133
133
|
end
|
data/test/cache_test.rb
CHANGED
@@ -40,7 +40,7 @@ describe "Sinatra::Cache" do
|
|
40
40
|
|
41
41
|
set :cache_enabled, false
|
42
42
|
set :cache_page_extension, '.cache.html'
|
43
|
-
set :
|
43
|
+
set :cache_output_dir, 'system/cache'
|
44
44
|
set :cache_logging, false
|
45
45
|
set :cache_logging_level, :info
|
46
46
|
|
@@ -228,8 +228,8 @@ describe "Sinatra::Cache" do
|
|
228
228
|
assert_equal('.html', @default_app.options.cache_page_extension)
|
229
229
|
end
|
230
230
|
|
231
|
-
it "the :
|
232
|
-
assert_equal('', @default_app.options.
|
231
|
+
it "the :cache_output_dir option should be correct ('') " do
|
232
|
+
assert_equal('', @default_app.options.cache_output_dir)
|
233
233
|
end
|
234
234
|
|
235
235
|
it "the :cache_logging option should be correct (true)" do
|
@@ -252,8 +252,8 @@ describe "Sinatra::Cache" do
|
|
252
252
|
assert_equal('.cache.html', @custom_app.options.cache_page_extension)
|
253
253
|
end
|
254
254
|
|
255
|
-
it "the :
|
256
|
-
assert_equal('system/cache', @custom_app.options.
|
255
|
+
it "the :cache_output_dir option should be correct ('system/cache') " do
|
256
|
+
assert_equal('system/cache', @custom_app.options.cache_output_dir)
|
257
257
|
end
|
258
258
|
|
259
259
|
it "the :cache_logging option should be correct (true)" do
|
@@ -288,9 +288,9 @@ describe "Sinatra::Cache" do
|
|
288
288
|
request = Rack::MockRequest.new(@default_app)
|
289
289
|
response = request.get('/cache')
|
290
290
|
assert response
|
291
|
-
assert_match(
|
291
|
+
assert_match(/^Hello World from Sinatra Version=\[\d\.\d\.\d(\.\d)?\]\n/, response.body)
|
292
292
|
assert(test(?f, "#{public_fixtures_path}/cache.html"))
|
293
|
-
assert_match(
|
293
|
+
assert_match(/^Hello World from Sinatra Version=\[\d\.\d\.\d(\.\d)?\]\n/, File.read("#{public_fixtures_path}/cache.html"))
|
294
294
|
end
|
295
295
|
|
296
296
|
it "should expire the /cache page" do
|
@@ -334,7 +334,7 @@ describe "Sinatra::Cache" do
|
|
334
334
|
|
335
335
|
set :cache_enabled, true
|
336
336
|
set :cache_page_extension, '.cache.html'
|
337
|
-
set :
|
337
|
+
set :cache_output_dir, 'system/cache'
|
338
338
|
set :cache_logging, true
|
339
339
|
set :cache_logging_level, :info
|
340
340
|
|
@@ -360,9 +360,9 @@ describe "Sinatra::Cache" do
|
|
360
360
|
request = Rack::MockRequest.new(@custom_enabled_app)
|
361
361
|
response = request.get('/cache')
|
362
362
|
assert response
|
363
|
-
assert_match(
|
363
|
+
assert_match(/^Hello World from Sinatra Version=\[\d\.\d\.\d(\.\d)?\]\n<!-- page cached: \d+-\d+-\d+ \d+:\d+:\d+ -->\n$/, response.body)
|
364
364
|
assert(test(?f, "#{public_fixtures_path}/system/cache/cache.cache.html"))
|
365
|
-
assert_match(
|
365
|
+
assert_match(/^Hello World from Sinatra Version=\[\d\.\d\.\d(\.\d)?\]\n<!-- page cached: \d+-\d+-\d+ \d+:\d+:\d+ -->\n$/, File.read("#{public_fixtures_path}/system/cache/cache.cache.html"))
|
366
366
|
end
|
367
367
|
|
368
368
|
it "should expire the /cache page" do
|
@@ -392,7 +392,7 @@ describe "Sinatra::Cache" do
|
|
392
392
|
describe "should respond to" do
|
393
393
|
|
394
394
|
[
|
395
|
-
"
|
395
|
+
"cache_output_dir", "cache_output_dir=","cache_output_dir?", "cache_enabled","cache_enabled=", "cache_enabled?",
|
396
396
|
"cache_logging","cache_logging=","cache_logging?","cache_logging_level","cache_logging_level=","cache_logging_level?",
|
397
397
|
"cache_page_extension","cache_page_extension=","cache_page_extension?"
|
398
398
|
].each do |m|
|
@@ -412,11 +412,11 @@ describe "Sinatra::Cache" do
|
|
412
412
|
before do
|
413
413
|
Sinatra::Base.register(Sinatra::Cache)
|
414
414
|
end
|
415
|
-
|
415
|
+
|
416
416
|
describe "should respond to" do
|
417
417
|
|
418
418
|
[
|
419
|
-
"
|
419
|
+
"cache_output_dir", "cache_output_dir=","cache_output_dir?", "cache_enabled","cache_enabled=", "cache_enabled?",
|
420
420
|
"cache_logging","cache_logging=","cache_logging?","cache_logging_level","cache_logging_level=","cache_logging_level?",
|
421
421
|
"cache_page_extension","cache_page_extension=","cache_page_extension?"
|
422
422
|
].each do |m|
|
@@ -431,20 +431,4 @@ describe "Sinatra::Cache" do
|
|
431
431
|
|
432
432
|
end #/Sinatra::Default
|
433
433
|
|
434
|
-
# it "should description" do
|
435
|
-
# assert_equal('Sinatra::Application', Sinatra::Application.methods.sort)
|
436
|
-
# end
|
437
|
-
# it "should description1" do
|
438
|
-
# assert_equal('Sinatra::Base', Sinatra::Base.methods.sort)
|
439
|
-
# end
|
440
|
-
# it "should description2" do
|
441
|
-
# assert_equal('@default_app', @default_app.methods.sort)
|
442
|
-
# end
|
443
|
-
#
|
444
|
-
# it "should description3" do
|
445
|
-
# assert_equal('Sinatra::Default', Sinatra::Default.methods.sort)
|
446
|
-
# end
|
447
|
-
|
448
|
-
|
449
|
-
|
450
434
|
end #/Sinatra::Cache
|