public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Cache AssetTag timestamps
josh (author)
Sun Jan 04 13:39:16 -0800 2009
commit  ce706b4b9be03a3f2e7d11438e6550d64c5f4461
tree    13605db641ea72a85be74c1c8f93de4bfc10af83
parent  d2a1c2778e76ba30431121d0a9062272e0c90405
...
8
9
10
 
 
11
12
13
...
8
9
10
11
12
13
14
15
0
@@ -8,6 +8,8 @@ module ActionController
0
           # Development mode callbacks
0
           before_dispatch :reload_application
0
           after_dispatch :cleanup_application
0
+
0
+          ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = false
0
         end
0
 
0
         if defined?(ActiveRecord)
...
468
469
470
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
471
472
473
...
526
527
528
 
 
 
529
530
531
532
533
534
535
536
537
538
 
 
539
540
 
 
 
 
 
 
 
 
 
 
541
542
543
...
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
...
542
543
544
545
546
547
548
549
550
551
552
553
 
 
 
 
554
555
556
 
557
558
559
560
561
562
563
564
565
566
567
568
569
0
@@ -468,6 +468,22 @@ module ActionView
0
         tag("img", options)
0
       end
0
 
0
+      def self.cache_asset_timestamps
0
+        @@cache_asset_timestamps
0
+      end
0
+
0
+      # You can enable or disable the asset tag timestamps cache.
0
+      # With the cache enabled, the asset tag helper methods will make fewer
0
+      # expense file system calls. However this prevents you from modifying
0
+      # any asset files while the server is running.
0
+      #
0
+      #   ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = false
0
+      def self.cache_asset_timestamps=(value)
0
+        @@cache_asset_timestamps = value
0
+      end
0
+
0
+      @@cache_asset_timestamps = true
0
+
0
       private
0
         # Add the the extension +ext+ if not present. Return full URLs otherwise untouched.
0
         # Prefix with <tt>/dir/</tt> if lacking a leading +/+. Account for relative URL
0
@@ -526,18 +542,28 @@ module ActionView
0
           end
0
         end
0
 
0
+        @@asset_timestamps_cache = {}
0
+        @@asset_timestamps_cache_guard = Mutex.new
0
+
0
         # Use the RAILS_ASSET_ID environment variable or the source's
0
         # modification time as its cache-busting asset id.
0
         def rails_asset_id(source)
0
           if asset_id = ENV["RAILS_ASSET_ID"]
0
             asset_id
0
           else
0
-            path = File.join(ASSETS_DIR, source)
0
-
0
-            if File.exist?(path)
0
-              File.mtime(path).to_i.to_s
0
+            if @@cache_asset_timestamps && (asset_id = @@asset_timestamps_cache[source])
0
+              asset_id
0
             else
0
-              ''
0
+              path = File.join(ASSETS_DIR, source)
0
+              asset_id = File.exist?(path) ? File.mtime(path).to_i.to_s : ''
0
+
0
+              if @@cache_asset_timestamps
0
+                @@asset_timestamps_cache_guard.synchronize do
0
+                  @@asset_timestamps_cache[source] = asset_id
0
+                end
0
+              end
0
+
0
+              asset_id
0
             end
0
           end
0
         end

Comments