public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Add fragment_exist? and exist? methods to cache stores. [#203 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
josevalim (author)
Fri May 16 10:10:30 -0700 2008
lifo (committer)
Mon May 19 02:38:59 -0700 2008
commit  99860b72aebe0348f41e82d4710343498d89a84b
tree    56457862ab6ce523cfb7d6dacd9f38655ab8c715
parent  17d1319c480e58e28641b243da50ae5e5eab89dc
...
98
99
100
 
 
 
 
 
 
 
 
 
 
 
101
102
103
...
124
125
126
127
128
 
...
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
...
135
136
137
 
138
139
0
@@ -98,6 +98,17 @@ module ActionController #:nodoc:
0
         end
0
       end
0
 
0
+      # Check if a cached fragment from the location signified by <tt>key</tt> exists (see <tt>expire_fragment</tt> for acceptable formats)
0
+      def fragment_exist?(key, options = nil)
0
+        return unless cache_configured?
0
+
0
+        key = fragment_cache_key(key)
0
+
0
+        self.class.benchmark "Cached fragment exists?: #{key}" do
0
+          cache_store.exist?(key, options)
0
+        end
0
+      end
0
+
0
       # Name can take one of three forms:
0
       # * String: This would normally take the form of a path like "pages/45/notes"
0
       # * Hash: Is treated as an implicit call to url_for, like { :controller => "pages", :action => "notes", :id => 45 }
0
@@ -124,4 +135,4 @@ module ActionController #:nodoc:
0
       end
0
     end
0
   end
0
-end
0
\ No newline at end of file
0
+end
...
232
233
234
235
 
236
237
238
...
243
244
245
246
 
247
248
249
...
254
255
256
257
 
258
259
260
...
266
267
268
269
 
270
271
272
273
274
275
276
 
277
278
279
...
282
283
284
285
 
286
287
288
289
 
290
291
292
...
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
 
 
410
411
412
...
450
451
452
 
 
 
 
 
 
 
 
 
 
 
 
 
453
454
455
...
494
495
496
497
498
499
500
...
232
233
234
 
235
236
237
238
...
243
244
245
 
246
247
248
249
...
254
255
256
 
257
258
259
260
...
266
267
268
 
269
270
271
272
273
274
275
 
276
277
278
279
...
282
283
284
 
285
286
287
288
 
289
290
291
292
...
395
396
397
 
 
 
 
 
 
 
 
 
 
 
 
398
399
400
401
402
...
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
...
497
498
499
 
500
501
502
0
@@ -232,7 +232,7 @@ class ActionCacheTest < Test::Unit::TestCase
0
     get :index
0
     cached_time = content_to_cache
0
     assert_equal cached_time, @response.body
0
-    assert_cache_exists 'hostname.com/action_caching_test'
0
+    assert fragment_exist?('hostname.com/action_caching_test')
0
     reset!
0
 
0
     get :index
0
@@ -243,7 +243,7 @@ class ActionCacheTest < Test::Unit::TestCase
0
     get :destroy
0
     cached_time = content_to_cache
0
     assert_equal cached_time, @response.body
0
-    assert_cache_does_not_exist 'hostname.com/action_caching_test/destroy'
0
+    assert !fragment_exist?('hostname.com/action_caching_test/destroy')
0
     reset!
0
 
0
     get :destroy
0
@@ -254,7 +254,7 @@ class ActionCacheTest < Test::Unit::TestCase
0
     get :with_layout
0
     cached_time = content_to_cache
0
     assert_not_equal cached_time, @response.body
0
-    assert_cache_exists 'hostname.com/action_caching_test/with_layout'
0
+    assert fragment_exist?('hostname.com/action_caching_test/with_layout')
0
     reset!
0
 
0
     get :with_layout
0
@@ -266,14 +266,14 @@ class ActionCacheTest < Test::Unit::TestCase
0
   def test_action_cache_conditional_options
0
     @request.env['HTTP_ACCEPT'] = 'application/json'
0
     get :index
0
-    assert_cache_does_not_exist 'hostname.com/action_caching_test'
0
+    assert !fragment_exist?('hostname.com/action_caching_test')
0
   end
0
 
0
   def test_action_cache_with_custom_cache_path
0
     get :show
0
     cached_time = content_to_cache
0
     assert_equal cached_time, @response.body
0
-    assert_cache_exists 'test.host/custom/show'
0
+    assert fragment_exist?('test.host/custom/show')
0
     reset!
0
 
0
     get :show
0
@@ -282,11 +282,11 @@ class ActionCacheTest < Test::Unit::TestCase
0
 
0
   def test_action_cache_with_custom_cache_path_in_block
0
     get :edit
0
-    assert_cache_exists 'test.host/edit'
0
+    assert fragment_exist?('test.host/edit')
0
     reset!
0
 
0
     get :edit, :id => 1
0
-    assert_cache_exists 'test.host/1;edit'
0
+    assert fragment_exist?('test.host/1;edit')
0
   end
0
 
0
   def test_cache_expiration
0
@@ -395,18 +395,8 @@ class ActionCacheTest < Test::Unit::TestCase
0
       @request.host = 'hostname.com'
0
     end
0
 
0
-    def assert_cache_exists(path)
0
-      full_path = cache_path(path)
0
-      assert File.exist?(full_path), "#{full_path.inspect} does not exist."
0
-    end
0
-
0
-    def assert_cache_does_not_exist(path)
0
-      full_path = cache_path(path)
0
-      assert !File.exist?(full_path), "#{full_path.inspect} should not exist."
0
-    end
0
-
0
-    def cache_path(path)
0
-      File.join(FILE_STORE_PATH, 'views', path + '.cache')
0
+    def fragment_exist?(path)
0
+      @controller.fragment_exist?(path)
0
     end
0
 
0
     def read_fragment(path)
0
@@ -450,6 +440,19 @@ class FragmentCachingTest < Test::Unit::TestCase
0
     assert_nil @controller.read_fragment('name')
0
   end
0
 
0
+  def test_fragment_exist__with_caching_enabled
0
+    @store.write('views/name', 'value')
0
+    assert @controller.fragment_exist?('name')
0
+    assert !@controller.fragment_exist?('other_name')
0
+  end
0
+
0
+  def test_fragment_exist__with_caching_disabled
0
+    ActionController::Base.perform_caching = false
0
+    @store.write('views/name', 'value')
0
+    assert !@controller.fragment_exist?('name')
0
+    assert !@controller.fragment_exist?('other_name')
0
+  end
0
+
0
   def test_write_fragment__with_caching_enabled
0
     assert_nil @store.read('views/name')
0
     assert_equal 'value', @controller.write_fragment('name', 'value')
0
@@ -494,7 +497,6 @@ class FragmentCachingTest < Test::Unit::TestCase
0
     assert_equal 'generated till now -> ', buffer
0
   end
0
 
0
-
0
   def test_fragment_for
0
     @store.write('views/expensive', 'fragment content')
0
     fragment_computed = false
...
87
88
89
90
91
 
 
 
 
 
 
92
93
94
...
87
88
89
 
 
90
91
92
93
94
95
96
97
98
0
@@ -87,8 +87,12 @@ module ActiveSupport
0
 
0
       def delete_matched(matcher, options = nil)
0
         log("delete matched", matcher.inspect, options)
0
-      end       
0
-      
0
+      end
0
+
0
+      def exist?(key, options = nil)
0
+        log("exist?", key, options)
0
+      end
0
+
0
       def increment(key, amount = 1)
0
         log("incrementing", key, amount)
0
         if num = read(key)
...
40
41
42
 
 
 
 
 
43
44
45
46
47
48
49
 
50
51
52
...
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 
54
55
56
57
0
@@ -40,13 +40,18 @@ module ActiveSupport
0
         end
0
       end
0
 
0
+      def exist?(name, options = nil)
0
+        super
0
+        File.exist?(real_file_path(name))
0
+      end
0
+
0
       private
0
         def real_file_path(name)
0
           '%s/%s.cache' % [@cache_path, name.gsub('?', '.').gsub(':', '.')]
0
         end
0
 
0
         def ensure_cache_path(path)
0
-          FileUtils.makedirs(path) unless File.exists?(path)
0
+          FileUtils.makedirs(path) unless File.exist?(path)
0
         end
0
 
0
         def search_dir(dir, &callback)
...
48
49
50
51
 
 
 
 
 
 
 
52
53
54
...
48
49
50
 
51
52
53
54
55
56
57
58
59
60
0
@@ -48,7 +48,13 @@ module ActiveSupport
0
       rescue MemCache::MemCacheError => e
0
         logger.error("MemCacheError (#{e}): #{e.message}")
0
         false
0
-      end              
0
+      end
0
+
0
+      def exist?(key, options = nil)
0
+        # Doesn't call super, cause exist? in memcache is in fact a read
0
+        # But who cares? Reading is very fast anyway
0
+        !read(key, options).nil?
0
+      end
0
 
0
       def increment(key, amount = 1)       
0
         log("incrementing", key, amount)
...
24
25
26
27
 
 
 
 
 
 
28
29
30
...
24
25
26
 
27
28
29
30
31
32
33
34
35
0
@@ -24,7 +24,12 @@ module ActiveSupport
0
         super
0
         @data.delete_if { |k,v| k =~ matcher }
0
       end
0
-      
0
+
0
+      def exist?(name,options = nil)
0
+        super
0
+        @data.has_key?(name)
0
+      end
0
+
0
       def clear
0
         @data.clear
0
       end

Comments