padrino-cache 0.12.0 → 0.12.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a0f9b6295baf4a34d62feb4b56eb4d29a164eee4
4
- data.tar.gz: 7f7bec0848f097f87953dca324d9e044be02860a
3
+ metadata.gz: 2b7050f5d6d2ab9a219ac49f53070953c9b36fce
4
+ data.tar.gz: 1d9a1dd4afb6129cc79d587cdcff69a08492a062
5
5
  SHA512:
6
- metadata.gz: add4db22861cbadbd691e234f56d143fddd10420eaf8b8c53d2c506967bb47860082bb8b9166e0805506a0ee666431febca78666c3742b3e0e2736917f62ac16
7
- data.tar.gz: e1fe58db16647084e285adf8a5d2013114ada60be75268289c2a49876853d726e4e650b9da6911629ba927f1a42285622c31bd8858918e1d94cf2103caee0cae
6
+ metadata.gz: 83da0914d495a370167a331d774818e0bee19f705cc6bf20dae375d50c8e04693dccedff3e85dfba18384a2b837f09597ac7746d1723b5bee156fcba784ead30
7
+ data.tar.gz: e8e1f995cd4256f0c29e319f77f3497b367fbbcc74fd2652be35544f9d566171719d68e40820bea5089d3f8b9db65dfd8d5168ce21ab91a10958938d192edce2
data/README.rdoc CHANGED
@@ -257,7 +257,7 @@ You can set a global caching option or a per app caching options.
257
257
 
258
258
  Padrino.cache = Padrino::Cache.new(:LRUHash) # Keeps cached values in memory
259
259
  Padrino.cache = Padrino::Cache.new(:Memcached) # Uses default server at localhost
260
- Padrino.cache = Padrino::Cache.new(:Memcached, '127.0.0.1:11211', :exception_retry_limit => 1)
260
+ Padrino.cache = Padrino::Cache.new(:Memcached, :server => '127.0.0.1:11211', :exception_retry_limit => 1)
261
261
  Padrino.cache = Padrino::Cache.new(:Memcached, :backend => memcached_or_dalli_instance)
262
262
  Padrino.cache = Padrino::Cache.new(:Redis) # Uses default server at localhost
263
263
  Padrino.cache = Padrino::Cache.new(:Redis, :host => '127.0.0.1', :port => 6379, :db => 0)
@@ -0,0 +1,23 @@
1
+ module Padrino
2
+ module Cache
3
+ module Helpers
4
+ module ObjectCache
5
+ def cache_object(key, opts = {})
6
+ if settings.caching?
7
+ began_at = Time.now
8
+ if value = settings.cache[key.to_s]
9
+ logger.debug "GET Object", began_at, key.to_s if defined?(logger)
10
+ else
11
+ value = yield
12
+ settings.cache.store(key.to_s, value, opts)
13
+ logger.debug "SET Object", began_at, key.to_s if defined?(logger)
14
+ end
15
+ value
16
+ else
17
+ yield
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
data/lib/padrino-cache.rb CHANGED
@@ -31,7 +31,7 @@ module Padrino
31
31
  # Padrino.cache = Padrino::Cache.new(:File, :dir => Padrino.root('tmp', app_name.to_s, 'cache')) # default choice
32
32
  # Padrino.cache = Padrino::Cache.new(:LRUHash) # Keeps cached values in memory
33
33
  # Padrino.cache = Padrino::Cache.new(:Memcached) # Uses default server at localhost
34
- # Padrino.cache = Padrino::Cache.new(:Memcached, '127.0.0.1:11211', :exception_retry_limit => 1)
34
+ # Padrino.cache = Padrino::Cache.new(:Memcached, :server => '127.0.0.1:11211', :exception_retry_limit => 1)
35
35
  # Padrino.cache = Padrino::Cache.new(:Memcached, :backend => memcached_or_dalli_instance)
36
36
  # Padrino.cache = Padrino::Cache.new(:Redis) # Uses default server at localhost
37
37
  # Padrino.cache = Padrino::Cache.new(:Redis, :host => '127.0.0.1', :port => 6379, :db => 0)
@@ -93,6 +93,7 @@ module Padrino
93
93
  # MyApp.cache.clear
94
94
  #
95
95
  def registered(app)
96
+ app.helpers Padrino::Cache::Helpers::ObjectCache
96
97
  app.helpers Padrino::Cache::Helpers::CacheStore
97
98
  app.helpers Padrino::Cache::Helpers::Fragment
98
99
  app.helpers Padrino::Cache::Helpers::Page
@@ -1,16 +1,12 @@
1
1
  require File.expand_path('../helper', __FILE__)
2
2
 
3
3
  describe "PadrinoCache" do
4
-
5
- before do
6
- end
7
-
8
4
  after do
9
5
  tmp = File.expand_path("../tmp", __FILE__)
10
6
  %x[rm -rf #{tmp}]
11
7
  end
12
8
 
13
- should 'cache a fragment' do
9
+ it 'should cache a fragment' do
14
10
  called = false
15
11
  mock_app do
16
12
  register Padrino::Cache
@@ -23,10 +19,10 @@ describe "PadrinoCache" do
23
19
  get "/foo"
24
20
  assert_equal 200, status
25
21
  assert_equal 'test fragment', body
26
- assert_not_equal called, false
22
+ refute_equal called, false
27
23
  end
28
24
 
29
- should 'cache a page' do
25
+ it 'should cache a page' do
30
26
  called = false
31
27
  mock_app do
32
28
  register Padrino::Cache
@@ -39,10 +35,10 @@ describe "PadrinoCache" do
39
35
  get "/foo"
40
36
  assert_equal 200, status
41
37
  assert_equal 'test page', body
42
- assert_not_equal false, called
38
+ refute_equal false, called
43
39
  end
44
40
 
45
- should 'delete from the cache' do
41
+ it 'should delete from the cache' do
46
42
  called = false
47
43
  mock_app do
48
44
  register Padrino::Cache
@@ -57,10 +53,10 @@ describe "PadrinoCache" do
57
53
  get "/foo"
58
54
  assert_equal 200, status
59
55
  assert_equal 'test page again', body
60
- assert_not_equal false, called
56
+ refute_equal false, called
61
57
  end
62
58
 
63
- should 'accept custom cache keys' do
59
+ it 'should accept custom cache keys' do
64
60
  called = false
65
61
  mock_app do
66
62
  register Padrino::Cache
@@ -102,7 +98,7 @@ describe "PadrinoCache" do
102
98
  assert_equal 'bar', body
103
99
  end
104
100
 
105
- should 'delete based on urls' do
101
+ it 'should delete based on urls' do
106
102
  called = false
107
103
  mock_app do
108
104
  register Padrino::Cache
@@ -119,7 +115,7 @@ describe "PadrinoCache" do
119
115
  assert_equal 'test page again', body
120
116
  end
121
117
 
122
- should 'accept allow controller-wide caching' do
118
+ it 'should accept allow controller-wide caching' do
123
119
  called = false
124
120
  mock_app do
125
121
  controller :cache => true do
@@ -136,7 +132,7 @@ describe "PadrinoCache" do
136
132
  assert_equal 'test', body
137
133
  end
138
134
 
139
- should 'allow cache disabling on a per route basis' do
135
+ it 'should allow cache disabling on a per route basis' do
140
136
  called = false
141
137
  mock_app do
142
138
  register Padrino::Cache
@@ -153,7 +149,7 @@ describe "PadrinoCache" do
153
149
  assert_equal 'test again', body
154
150
  end
155
151
 
156
- should 'allow expiring for pages' do
152
+ it 'should allow expiring for pages' do
157
153
  called = false
158
154
  mock_app do
159
155
  register Padrino::Cache
@@ -171,13 +167,12 @@ describe "PadrinoCache" do
171
167
  get "/foo"
172
168
  assert_equal 200, status
173
169
  assert_equal 'test', body
174
- sleep 2
175
- get "/foo"
170
+ Time.stub(:now, Time.now + 3) { get "/foo" }
176
171
  assert_equal 200, status
177
172
  assert_equal 'test again', body
178
173
  end
179
174
 
180
- should 'allow expiring for fragments' do
175
+ it 'should allow expiring for fragments' do
181
176
  called = false
182
177
  mock_app do
183
178
  register Padrino::Cache
@@ -197,13 +192,12 @@ describe "PadrinoCache" do
197
192
  get "/foo"
198
193
  assert_equal 200, status
199
194
  assert_equal 'test', body
200
- sleep 3
201
- get "/foo"
195
+ Time.stub(:now, Time.now + 3) { get "/foo" }
202
196
  assert_equal 200, status
203
197
  assert_equal 'test again', body
204
198
  end
205
199
 
206
- should 'allow disabling of the cache' do
200
+ it 'should allow disabling of the cache' do
207
201
  called = false
208
202
  mock_app do
209
203
  register Padrino::Cache
@@ -219,7 +213,7 @@ describe "PadrinoCache" do
219
213
  assert_equal 500, status
220
214
  end
221
215
 
222
- should 'not cache integer statuses' do
216
+ it 'should not cache integer statuses' do
223
217
  mock_app do
224
218
  register Padrino::Cache
225
219
  enable :caching
@@ -244,7 +238,7 @@ describe "PadrinoCache" do
244
238
  assert_equal 503, status
245
239
  end
246
240
 
247
- should 'cache should not hit with unique params' do
241
+ it 'should cache should not hit with unique params' do
248
242
  call_count = 0
249
243
  mock_app do
250
244
  register Padrino::Cache
@@ -271,7 +265,7 @@ describe "PadrinoCache" do
271
265
  assert_equal 2, call_count
272
266
  end
273
267
 
274
- should 'resolve block cache keys' do
268
+ it 'should resolve block cache keys' do
275
269
  call_count = 0
276
270
  mock_app do
277
271
  register Padrino::Cache
@@ -293,7 +287,7 @@ describe "PadrinoCache" do
293
287
  assert_equal 3, call_count
294
288
  end
295
289
 
296
- should 'raise an error if providing both a cache_key and block' do
290
+ it 'should raise an error if providing both a cache_key and block' do
297
291
  mock_app do
298
292
  register Padrino::Cache
299
293
  enable :caching
@@ -306,7 +300,7 @@ describe "PadrinoCache" do
306
300
  assert_raises(RuntimeError) { get '/foo' }
307
301
  end
308
302
 
309
- should 'cache content_type' do
303
+ it 'should cache content_type' do
310
304
  called = false
311
305
  mock_app do
312
306
  register Padrino::Cache
@@ -331,4 +325,25 @@ describe "PadrinoCache" do
331
325
  assert_equal '{"foo":"bar"}', body
332
326
  assert_match /json/, last_response.content_type
333
327
  end
328
+
329
+ it 'should cache an object' do
330
+ counter = 0
331
+ mock_app do
332
+ register Padrino::Cache
333
+ enable :caching
334
+ get '/' do
335
+ result = ''
336
+ 2.times do
337
+ result = cache_object 'object1' do
338
+ counter += 1
339
+ { :foo => 'bar' }
340
+ end
341
+ end
342
+ result[:foo].to_s
343
+ end
344
+ end
345
+ get '/'
346
+ assert_equal 'bar', body
347
+ assert_equal 1, counter
348
+ end
334
349
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: padrino-cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ version: 0.12.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Padrino Team
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2014-02-09 00:00:00.000000000 Z
14
+ date: 2014-04-04 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: padrino-core
@@ -19,28 +19,28 @@ dependencies:
19
19
  requirements:
20
20
  - - '='
21
21
  - !ruby/object:Gem::Version
22
- version: 0.12.0
22
+ version: 0.12.1
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - '='
28
28
  - !ruby/object:Gem::Version
29
- version: 0.12.0
29
+ version: 0.12.1
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: padrino-helpers
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  requirements:
34
34
  - - '='
35
35
  - !ruby/object:Gem::Version
36
- version: 0.12.0
36
+ version: 0.12.1
37
37
  type: :runtime
38
38
  prerelease: false
39
39
  version_requirements: !ruby/object:Gem::Requirement
40
40
  requirements:
41
41
  - - '='
42
42
  - !ruby/object:Gem::Version
43
- version: 0.12.0
43
+ version: 0.12.1
44
44
  - !ruby/object:Gem::Dependency
45
45
  name: moneta
46
46
  requirement: !ruby/object:Gem::Requirement
@@ -69,6 +69,7 @@ files:
69
69
  - README.rdoc
70
70
  - Rakefile
71
71
  - lib/padrino-cache.rb
72
+ - lib/padrino-cache/helpers/cache_object.rb
72
73
  - lib/padrino-cache/helpers/cache_store.rb
73
74
  - lib/padrino-cache/helpers/fragment.rb
74
75
  - lib/padrino-cache/helpers/page.rb