public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Moved the caching stores from ActionController::Caching::Fragments::* to 
ActiveSupport::Cache::*. If you're explicitly referring to a store, like 
ActionController::Caching::Fragments::MemoryStore, you need to update that 
reference with ActiveSupport::Cache::MemoryStore [DHH] Deprecated 
ActionController::Base.fragment_cache_store for 
ActionController::Base.cache_store [DHH] All fragment cache keys are now by 
default prefixed with the 'views/' namespace [DHH] Added 
ActiveRecord::Base.cache_key to make it easier to cache Active Records in 
combination with the new ActiveSupport::Cache::* libraries [DHH] Added 
ActiveSupport::Gzip.decompress/compress(source) as an easy wrapper for Zlib 
[Tobias Luetke] Included MemCache-Client to make the improved 
ActiveSupport::Cache::MemCacheStore work out of the box [Bob Cottrell, Eric 
Hodel] Added config.cache_store to environment options to control the default 
cache store (default is FileStore if tmp/cache is present, otherwise MemoryStore 
is used) [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8546 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
dhh (author)
Thu Jan 03 13:05:12 -0800 2008
commit  2a9ad9ccbc706e546bf02ec95f864944e7d7983b
tree    868624e91f037840bfbf0aca30bb2ea1c9d78701
parent  288553540b5b2f37497cb19357b25ac12e0498fd
...
1
2
 
 
 
 
 
 
3
4
5
...
1
2
3
4
5
6
7
8
9
10
11
0
@@ -1,5 +1,11 @@
0
 *SVN*
0
 
0
+* All fragment cache keys are now by default prefixed with the "views/" namespace [DHH]
0
+
0
+* Moved the caching stores from ActionController::Caching::Fragments::* to ActiveSupport::Cache::*. If you're explicitly referring to a store, like ActionController::Caching::Fragments::MemoryStore, you need to update that reference with ActiveSupport::Cache::MemoryStore [DHH]
0
+
0
+* Deprecated ActionController::Base.fragment_cache_store for ActionController::Base.cache_store [DHH]
0
+
0
 * Made fragment caching in views work for rjs and builder as well #6642 [zsombor]
0
 
0
 * Fixed rendering of partials with layout when done from site layout #9209 [antramm]
...
97
98
99
100
 
101
102
103
...
97
98
99
 
100
101
102
103
0
@@ -97,7 +97,7 @@ A short rundown of the major features:
0
 
0
     class WeblogController < ActionController::Base
0
       before_filter :authenticate, :cache, :audit
0
-      after_filter { |c| c.response.body = GZip::compress(c.response.body) }
0
+      after_filter { |c| c.response.body = Gzip::compress(c.response.body) }
0
       after_filter LocalizeFilter
0
       
0
       def index
...
2
3
4
 
 
 
 
 
 
 
5
6
7
...
9
10
11
 
 
 
 
 
 
 
 
 
 
 
 
 
12
13
14
15
 
 
16
17
18
 
 
 
19
20
 
 
 
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
 
 
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
 
 
 
 
 
223
224
 
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
 
 
 
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
 
710
...
2
3
4
5
6
7
8
9
10
11
12
13
14
...
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 
35
36
37
 
 
38
39
40
41
42
43
44
45
46
47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
 
 
 
 
 
49
50
51
 
 
 
 
 
 
 
 
 
 
 
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
56
57
58
59
60
 
61
62
63
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
67
68
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
72
0
@@ -2,6 +2,13 @@ require 'fileutils'
0
 require 'uri'
0
 require 'set'
0
 
0
+require 'action_controller/caching/pages'
0
+require 'action_controller/caching/actions'
0
+require 'action_controller/caching/sql_cache'
0
+require 'action_controller/caching/sweeping'
0
+require 'action_controller/caching/fragments'
0
+
0
+
0
 module ActionController #:nodoc:
0
   # Caching is a cheap way of speeding up slow applications by keeping the result of calculations, renderings, and database calls
0
   # around for subsequent requests. Action Controller affords you three approaches in varying levels of granularity: Page, Action, Fragment.
0
@@ -9,701 +16,56 @@ module ActionController #:nodoc:
0
   # You can read more about each approach and the sweeping assistance by clicking the modules below.
0
   #
0
   # Note: To turn off all caching and sweeping, set Base.perform_caching = false.
0
+  #
0
+  #
0
+  # == Caching stores
0
+  #
0
+  # All the caching stores from ActiveSupport::Cache is available to be used as backends for Action Controller caching.
0
+  #
0
+  # Configuration examples (MemoryStore is the default):
0
+  #
0
+  #   ActionController::Base.cache_store = :memory_store
0
+  #   ActionController::Base.cache_store = :file_store, "/path/to/cache/directory"
0
+  #   ActionController::Base.cache_store = :drb_store, "druby://localhost:9192"
0
+  #   ActionController::Base.cache_store = :mem_cache_store, "localhost"
0
+  #   ActionController::Base.cache_store = MyOwnStore.new("parameter")
0
   module Caching
0
     def self.included(base) #:nodoc:
0
       base.class_eval do
0
-        include Pages, Actions, Fragments
0
+        @@cache_store = nil
0
+        cattr_reader :cache_store
0
 
0
-        if defined? ActiveRecord
0
-          include Sweeping, SqlCache
0
+        # Defines the storage option for cached fragments
0
+        def self.cache_store=(store_option)
0
+          @@cache_store = ActiveSupport::Cache.lookup_store(store_option)
0
         end
0
 
0
+        include Pages, Actions, Fragments
0
+        include Sweeping, SqlCache if defined?(ActiveRecord)
0
+
0
         @@perform_caching = true
0
         cattr_accessor :perform_caching
0
-      end
0
-    end
0
-
0
-    # Page caching is an approach to caching where the entire action output of is stored as a HTML file that the web server
0
-    # can serve without going through the Action Pack. This can be as much as 100 times faster than going through the process of dynamically
0
-    # generating the content. Unfortunately, this incredible speed-up is only available to stateless pages where all visitors
0
-    # are treated the same. Content management systems -- including weblogs and wikis -- have many pages that are a great fit
0
-    # for this approach, but account-based systems where people log in and manipulate their own data are often less likely candidates.
0
-    #
0
-    # Specifying which actions to cache is done through the <tt>caches</tt> class method:
0
-    #
0
-    #   class WeblogController < ActionController::Base
0
-    #     caches_page :show, :new
0
-    #   end
0
-    #
0
-    # This will generate cache files such as weblog/show/5 and weblog/new, which match the URLs used to trigger the dynamic
0
-    # generation. This is how the web server is able pick up a cache file when it exists and otherwise let the request pass on to
0
-    # the Action Pack to generate it.
0
-    #
0
-    # Expiration of the cache is handled by deleting the cached file, which results in a lazy regeneration approach where the cache
0
-    # is not restored before another hit is made against it. The API for doing so mimics the options from url_for and friends:
0
-    #
0
-    #   class WeblogController < ActionController::Base
0
-    #     def update
0
-    #       List.update(params[:list][:id], params[:list])
0
-    #       expire_page :action => "show", :id => params[:list][:id]
0
-    #       redirect_to :action => "show", :id => params[:list][:id]
0
-    #     end
0
-    #   end
0
-    #
0
-    # Additionally, you can expire caches using Sweepers that act on changes in the model to determine when a cache is supposed to be
0
-    # expired.
0
-    #
0
-    # == Setting the cache directory
0
-    #
0
-    # The cache directory should be the document root for the web server and is set using Base.page_cache_directory = "/document/root".
0
-    # For Rails, this directory has already been set to RAILS_ROOT + "/public".
0
-    #
0
-    # == Setting the cache extension
0
-    #
0
-    # By default, the cache extension is .html, which makes it easy for the cached files to be picked up by the web server. If you want
0
-    # something else, like .php or .shtml, just set Base.page_cache_extension.
0
-    module Pages
0
-      def self.included(base) #:nodoc:
0
-        base.extend(ClassMethods)
0
-        base.class_eval do
0
-          @@page_cache_directory = defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/public" : ""
0
-          cattr_accessor :page_cache_directory
0
-
0
-          @@page_cache_extension = '.html'
0
-          cattr_accessor :page_cache_extension
0
-        end
0
-      end
0
-
0
-      module ClassMethods
0
-        # Expires the page that was cached with the +path+ as a key. Example:
0
-        #   expire_page "/lists/show"
0
-        def expire_page(path)
0
-          return unless perform_caching
0
-
0
-          benchmark "Expired page: #{page_cache_file(path)}" do
0
-            File.delete(page_cache_path(path)) if File.exist?(page_cache_path(path))
0
-          end
0
-        end
0
-
0
-        # Manually cache the +content+ in the key determined by +path+. Example:
0
-        #   cache_page "I'm the cached content", "/lists/show"
0
-        def cache_page(content, path)
0
-          return unless perform_caching
0
-
0
-          benchmark "Cached page: #{page_cache_file(path)}" do
0
-            FileUtils.makedirs(File.dirname(page_cache_path(path)))
0
-            File.open(page_cache_path(path), "wb+") { |f| f.write(content) }
0
-          end
0
-        end
0
 
0
-        # Caches the +actions+ using the page-caching approach that'll store the cache in a path within the page_cache_directory that
0
-        # matches the triggering url.
0
-        def caches_page(*actions)
0
-          return unless perform_caching
0
-          actions = actions.map(&:to_s)
0
-          after_filter { |c| c.cache_page if actions.include?(c.action_name) }
0
+        def self.cache_configured?
0
+          perform_caching && cache_store
0
         end
0
-
0
-        private
0
-          def page_cache_file(path)
0
-            name = (path.empty? || path == "/") ? "/index" : URI.unescape(path.chomp('/'))
0
-            name << page_cache_extension unless (name.split('/').last || name).include? '.'
0
-            return name
0
-          end
0
-
0
-          def page_cache_path(path)
0
-            page_cache_directory + page_cache_file(path)
0
-          end
0
       end
0
-
0
-      # Expires the page that was cached with the +options+ as a key. Example:
0
-      #   expire_page :controller => "lists", :action => "show"
0
-      def expire_page(options = {})
0
-        return unless perform_caching
0
-
0
-        if options.is_a?(Hash)
0
-          if options[:action].is_a?(Array)
0
-            options[:action].dup.each do |action|
0
-              self.class.expire_page(url_for(options.merge(:only_path => true, :skip_relative_url_root => true, :action => action)))
0
-            end
0
-          else
0
-            self.class.expire_page(url_for(options.merge(:only_path => true, :skip_relative_url_root => true)))
0
-          end
0
-        else
0
-          self.class.expire_page(options)
0
-        end
0
-      end
0
-
0
-      # Manually cache the +content+ in the key determined by +options+. If no content is provided, the contents of response.body is used
0
-      # If no options are provided, the requested url is used. Example:
0
-      #   cache_page "I'm the cached content", :controller => "lists", :action => "show"
0
-      def cache_page(content = nil, options = nil)
0
-        return unless perform_caching && caching_allowed
0
-
0
-        path = case options
0
-          when Hash
0
-            url_for(options.merge(:only_path => true, :skip_relative_url_root => true, :format => params[:format]))
0
-          when String
0
-            options
0
-          else
0
-            request.path
0
-        end
0
-
0
-        self.class.cache_page(content || response.body, path)
0
-      end
0
-
0
-      private
0
-        def caching_allowed
0
-          request.get? && response.headers['Status'].to_i == 200
0
-        end
0
     end
0
 
0
-    # Action caching is similar to page caching by the fact that the entire output of the response is cached, but unlike page caching,
0
-    # every request still goes through the Action Pack. The key benefit of this is that filters are run before the cache is served, which
0
-    # allows for authentication and other restrictions on whether someone is allowed to see the cache. Example:
0
-    #
0
-    #   class ListsController < ApplicationController
0
-    #     before_filter :authenticate, :except => :public
0
-    #     caches_page   :public
0
-    #     caches_action :show, :feed
0
-    #   end
0
-    #
0
-    # In this example, the public action doesn't require authentication, so it's possible to use the faster page caching method. But both the
0
-    # show and feed action are to be shielded behind the authenticate filter, so we need to implement those as action caches.
0
-    #
0
-    # Action caching internally uses the fragment caching and an around filter to do the job. The fragment cache is named according to both
0
-    # the current host and the path. So a page that is accessed at http://david.somewhere.com/lists/show/1 will result in a fragment named
0
-    # "david.somewhere.com/lists/show/1". This allows the cacher to differentiate between "david.somewhere.com/lists/" and
0
-    # "jamis.somewhere.com/lists/" -- which is a helpful way of assisting the subdomain-as-account-key pattern.
0
-    #
0
-    # Different representations of the same resource, e.g. <tt>http://david.somewhere.com/lists</tt> and <tt>http://david.somewhere.com/lists.xml</tt>
0
-    # are treated like separate requests and so are cached separately. Keep in mind when expiring an action cache that <tt>:action => 'lists'</tt> is not the same
0
-    # as <tt>:action => 'list', :format => :xml</tt>.
0
-    #
0
-    # You can set modify the default action cache path by passing a :cache_path option.  This will be passed directly to ActionCachePath.path_for.  This is handy
0
-    # for actions with multiple possible routes that should be cached differently.  If a block is given, it is called with the current controller instance.
0
-    #
0
-    #   class ListsController < ApplicationController
0
-    #     before_filter :authenticate, :except => :public
0
-    #     caches_page   :public
0
-    #     caches_action :show, :cache_path => { :project => 1 }
0
-    #     caches_action :show, :cache_path => Proc.new { |controller| 
0
-    #       controller.params[:user_id] ? 
0
-    #         controller.send(:user_list_url, c.params[:user_id], c.params[:id]) :
0
-    #         controller.send(:list_url, c.params[:id]) }
0
-    #   end
0
-    module Actions
0
-      def self.included(base) #:nodoc:
0
-        base.extend(ClassMethods)
0
-          base.class_eval do
0
-            attr_accessor :rendered_action_cache, :action_cache_path
0
-            alias_method_chain :protected_instance_variables, :action_caching
0
-          end
0
-      end
0
-
0
-      module ClassMethods
0
-        # Declares that +actions+ should be cached.
0
-        # See ActionController::Caching::Actions for details.
0
-        def caches_action(*actions)
0
-          return unless perform_caching
0
-          around_filter(ActionCacheFilter.new(*actions))
0
-        end
0
-      end
0
-
0
-      def protected_instance_variables_with_action_caching
0
-        protected_instance_variables_without_action_caching + %w(@action_cache_path)
0
-      end
0
-
0
-      def expire_action(options = {})
0
-        return unless perform_caching
0
-        if options[:action].is_a?(Array)
0
-          options[:action].dup.each do |action|
0
-            expire_fragment(ActionCachePath.path_for(self, options.merge({ :action => action })))
0
-          end
0
+    protected
0
+      # Convenience accessor
0
+      def cache(key, options = nil, &block)
0
+        if cache_configured?
0
+          cache_store.fetch(ActiveSupport::Cache.expand_cache_key(key, :controller), options, &block)
0
         else
0
-          expire_fragment(ActionCachePath.path_for(self, options))
0
+          yield
0
         end
0
       end
0
 
0
-      class ActionCacheFilter #:nodoc:
0
-        def initialize(*actions, &block)
0
-          @options = actions.extract_options!
0
-          @actions = Set.new actions
0
-        end
0
-
0
-        def before(controller)
0
-          return unless @actions.include?(controller.action_name.intern)
0
-          cache_path = ActionCachePath.new(controller, path_options_for(controller, @options))
0
-          if cache = controller.read_fragment(cache_path.path)
0
-            controller.rendered_action_cache = true
0
-            set_content_type!(controller, cache_path.extension)
0
-            controller.send!(:render_for_text, cache)
0
-            false
0
-          else
0
-            controller.action_cache_path = cache_path
0
-          end
0
-        end
0
-
0
-        def after(controller)
0
-          return if !@actions.include?(controller.action_name.intern) || controller.rendered_action_cache || !caching_allowed(controller)
0
-          controller.write_fragment(controller.action_cache_path.path, controller.response.body)
0
-        end
0
 
0
-        private
0
-          def set_content_type!(controller, extension)
0
-            controller.response.content_type = Mime::Type.lookup_by_extension(extension).to_s if extension
0
-          end
0
-
0
-          def path_options_for(controller, options)
0
-            ((path_options = options[:cache_path]).respond_to?(:call) ? path_options.call(controller) : path_options) || {}
0
-          end
0
-
0
-          def caching_allowed(controller)
0
-            controller.request.get? && controller.response.headers['Status'].to_i == 200
0
-          end
0
-      end
0
-      
0
-      class ActionCachePath
0
-        attr_reader :path, :extension
0
-        
0
-        class << self
0
-          def path_for(controller, options)
0
-            new(controller, options).path
0
-          end
0
-        end
0
-        
0
-        def initialize(controller, options = {})
0
-          @extension = extract_extension(controller.request.path)
0
-          path = controller.url_for(options).split('://').last
0
-          normalize!(path)
0
-          add_extension!(path, @extension)
0
-          @path = URI.unescape(path)
0
-        end
0
-        
0
-        private
0
-          def normalize!(path)
0
-            path << 'index' if path[-1] == ?/
0
-          end
0
-        
0
-          def add_extension!(path, extension)
0
-            path << ".#{extension}" if extension
0
-          end
0
-          
0
-          def extract_extension(file_path)
0
-            # Don't want just what comes after the last '.' to accommodate multi part extensions
0
-            # such as tar.gz.
0
-            file_path[/^[^.]+\.(.+)$/, 1]
0
-          end
0
+    private    
0
+      def cache_configured?
0
+        self.class.cache_configured?
0
       end
0
-    end
0
-
0
-    # Fragment caching is used for caching various blocks within templates without caching the entire action as a whole. This is useful when
0
-    # certain elements of an action change frequently or depend on complicated state while other parts rarely change or can be shared amongst multiple
0
-    # parties. The caching is doing using the cache helper available in the Action View. A template with caching might look something like:
0
-    #
0
-    #   <b>Hello <%= @name %></b>
0
-    #   <% cache do %>
0
-    #     All the topics in the system:
0
-    #     <%= render :partial => "topic", :collection => Topic.find(:all) %>
0
-    #   <% end %>
0
-    #
0
-    # This cache will bind to the name of the action that called it, so if this code was part of the view for the topics/list action, you would 
0
-    # be able to invalidate it using <tt>expire_fragment(:controller => "topics", :action => "list")</tt>. 
0
-    # 
0
-    # This default behavior is of limited use if you need to cache multiple fragments per action or if the action itself is cached using 
0
-    # <tt>caches_action</tt>, so we also have the option to qualify the name of the cached fragment with something like:
0
-    #
0
-    #   <% cache(:action => "list", :action_suffix => "all_topics") do %>
0
-    #
0
-    # That would result in a name such as "/topics/list/all_topics", avoiding conflicts with the action cache and with any fragments that use a 
0
-    # different suffix. Note that the URL doesn't have to really exist or be callable - the url_for system is just used to generate unique 
0
-    # cache names that we can refer to when we need to expire the cache. 
0
-    # 
0
-    # The expiration call for this example is:
0
-    # 
0
-    #   expire_fragment(:controller => "topics", :action => "list", :action_suffix => "all_topics")
0
-    #
0
-    # == Fragment stores
0
-    #
0
-    # By default, cached fragments are stored in memory. The available store options are:
0
-    #
0
-    # * FileStore: Keeps the fragments on disk in the +cache_path+, which works well for all types of environments and allows all 
0
-    #   processes running from the same application directory to access the cached content.
0
-    # * MemoryStore: Keeps the fragments in memory, which is fine for WEBrick and for FCGI (if you don't care that each FCGI process holds its
0
-    #   own fragment store). It's not suitable for CGI as the process is thrown away at the end of each request. It can potentially also take
0
-    #   up a lot of memory since each process keeps all the caches in memory.
0
-    # * DRbStore: Keeps the fragments in the memory of a separate, shared DRb process. This works for all environments and only keeps one cache
0
-    #   around for all processes, but requires that you run and manage a separate DRb process.
0
-    # * MemCacheStore: Works like DRbStore, but uses Danga's MemCache instead.
0
-    #   Requires the ruby-memcache library:  gem install ruby-memcache.
0
-    #
0
-    # Configuration examples (MemoryStore is the default):
0
-    #
0
-    #   ActionController::Base.fragment_cache_store = :memory_store
0
-    #   ActionController::Base.fragment_cache_store = :file_store, "/path/to/cache/directory"
0
-    #   ActionController::Base.fragment_cache_store = :drb_store, "druby://localhost:9192"
0
-    #   ActionController::Base.fragment_cache_store = :mem_cache_store, "localhost"
0
-    #   ActionController::Base.fragment_cache_store = MyOwnStore.new("parameter")
0
-    module Fragments
0
-      def self.included(base) #:nodoc:
0
-        base.class_eval do
0
-          @@fragment_cache_store = MemoryStore.new
0
-          cattr_reader :fragment_cache_store
0
-
0
-          # Defines the storage option for cached fragments
0
-          def self.fragment_cache_store=(store_option)
0
-            store, *parameters = *([ store_option ].flatten)
0
-            @@fragment_cache_store = if store.is_a?(Symbol)
0
-              store_class_name = (store == :drb_store ? "DRbStore" : store.to_s.camelize)
0
-              store_class = ActionController::Caching::Fragments.const_get(store_class_name)
0
-              store_class.new(*parameters)
0
-            else
0
-              store
0
-            end
0
-          end
0
-        end
0
-      end
0
-
0
-      # Given a name (as described in <tt>expire_fragment</tt>), returns a key suitable for use in reading, 
0
-      # writing, or expiring a cached fragment. If the name is a hash, the generated name is the return
0
-      # value of url_for on that hash (without the protocol).
0
-      def fragment_cache_key(name)
0
-        name.is_a?(Hash) ? url_for(name).split("://").last : name
0
-      end
0
-
0
-      def fragment_for(block, name = {}, options = nil) #:nodoc:
0
-        unless perform_caching then block.call; return end
0
-
0
-        buffer = yield
0
-
0
-        if cache = read_fragment(name, options)
0
-          buffer.concat(cache)
0
-        else
0
-          pos = buffer.length
0
-          block.call
0
-          write_fragment(name, buffer[pos..-1], options)
0
-        end
0
-      end
0
-
0
-      # Called by CacheHelper#cache
0
-      def cache_rxml_fragment(block, name = {}, options = nil) #:nodoc:
0
-        fragment_for(block, name, options) do
0
-          eval('xml.target!', block.binding)
0
-        end
0
-      end
0
-      
0
-      # Called by CacheHelper#cache
0
-      def cache_rjs_fragment(block, name = {}, options = nil) #:nodoc:
0
-        fragment_for(block, name, options) do
0
-          begin
0
-            debug_mode, ActionView::Base.debug_rjs = ActionView::Base.debug_rjs, false
0
-            eval('page.to_s', block.binding)
0
-          ensure
0
-            ActionView::Base.debug_rjs = debug_mode
0
-          end
0
-        end
0
-      end
0
-      
0
-      # Called by CacheHelper#cache
0
-      def cache_erb_fragment(block, name = {}, options = nil) #:nodoc:
0
-        fragment_for(block, name, options) do
0
-          eval(ActionView::Base.erb_variable, block.binding)
0
-        end
0
-      end
0
-
0
-
0
-      # Writes <tt>content</tt> to the location signified by <tt>name</tt> (see <tt>expire_fragment</tt> for acceptable formats)
0
-      def write_fragment(name, content, options = nil)
0
-        return unless perform_caching
0
-
0
-        key = fragment_cache_key(name)
0
-        self.class.benchmark "Cached fragment: #{key}" do
0
-          fragment_cache_store.write(key, content, options)
0
-        end
0
-        content
0
-      end
0
-
0
-      # Reads a cached fragment from the location signified by <tt>name</tt> (see <tt>expire_fragment</tt> for acceptable formats)
0
-      def read_fragment(name, options = nil)
0
-        return unless perform_caching
0
-
0
-        key = fragment_cache_key(name)
0
-        self.class.benchmark "Fragment read: #{key}" do
0
-          fragment_cache_store.read(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
-      # * Regexp: Will destroy all the matched fragments, example:
0
-      #     %r{pages/\d*/notes}
0
-      #   Ensure you do not specify start and finish in the regex (^$) because
0
-      #   the actual filename matched looks like ./cache/filename/path.cache
0
-      #   Regexp expiration is only supported on caches that can iterate over
0
-      #   all keys (unlike memcached).
0
-      def expire_fragment(name, options = nil)
0
-        return unless perform_caching
0
-
0
-        key = fragment_cache_key(name)
0
-
0
-        if key.is_a?(Regexp)
0
-          self.class.benchmark "Expired fragments matching: #{key.source}" do
0
-            fragment_cache_store.delete_matched(key, options)
0
-          end
0
-        else
0
-          self.class.benchmark "Expired fragment: #{key}" do
0
-            fragment_cache_store.delete(key, options)
0
-          end
0
-        end
0
-      end
0
-
0
-
0
-      class UnthreadedMemoryStore #:nodoc:
0
-        def initialize #:nodoc:
0
-          @data = {}
0
-        end
0
-
0
-        def read(name, options=nil) #:nodoc:
0
-          @data[name]
0
-        end
0
-
0
-        def write(name, value, options=nil) #:nodoc:
0
-          @data[name] = value
0
-        end
0
-
0
-        def delete(name, options=nil) #:nodoc:
0
-          @data.delete(name)
0
-        end
0
-
0
-        def delete_matched(matcher, options=nil) #:nodoc:
0
-          @data.delete_if { |k,v| k =~ matcher }
0
-        end
0
-      end
0
-
0
-      module ThreadSafety #:nodoc:
0
-        def read(name, options=nil) #:nodoc:
0
-          @mutex.synchronize { super }
0
-        end
0
-
0
-        def write(name, value, options=nil) #:nodoc:
0
-          @mutex.synchronize { super }
0
-        end
0
-
0
-        def delete(name, options=nil) #:nodoc:
0
-          @mutex.synchronize { super }
0
-        end
0
-
0
-        def delete_matched(matcher, options=nil) #:nodoc:
0
-          @mutex.synchronize { super }
0
-        end
0
-      end
0
-
0
-      class MemoryStore < UnthreadedMemoryStore #:nodoc:
0
-        def initialize #:nodoc:
0
-          super
0
-          if ActionController::Base.allow_concurrency
0
-            @mutex = Mutex.new
0
-            MemoryStore.module_eval { include ThreadSafety }
0
-          end
0
-        end
0
-      end
0
-
0
-      class DRbStore < MemoryStore #:nodoc:
0
-        attr_reader :address
0
-
0
-        def initialize(address = 'druby://localhost:9192')
0
-          super()
0
-          @address = address
0
-          @data = DRbObject.new(nil, address)
0
-        end
0
-      end
0
-
0
-    begin
0
-      require_library_or_gem 'memcache'
0
-      class MemCacheStore < MemoryStore #:nodoc:
0
-        attr_reader :addresses
0
-
0
-        def initialize(*addresses)
0
-          super()
0
-          addresses = addresses.flatten
0
-          addresses = ["localhost"] if addresses.empty?
0
-          @addresses = addresses
0
-          @data = MemCache.new(*addresses)
0
-        end
0
-      end
0
-    rescue LoadError
0
-      # MemCache wasn't available so neither can the store be
0
-    end
0
-
0
-      class UnthreadedFileStore #:nodoc:
0
-        attr_reader :cache_path
0
-
0
-        def initialize(cache_path)
0
-          @cache_path = cache_path
0
-        end
0
-
0
-        def write(name, value, options = nil) #:nodoc:
0
-          ensure_cache_path(File.dirname(real_file_path(name)))
0
-          File.open(real_file_path(name), "wb+") { |f| f.write(value) }
0
-        rescue => e
0
-          Base.logger.error "Couldn't create cache directory: #{name} (#{e.message})" if Base.logger
0
-        end
0
-
0
-        def read(name, options = nil) #:nodoc:
0
-          File.open(real_file_path(name), 'rb') { |f| f.read } rescue nil
0
-        end
0
-
0
-        def delete(name, options) #:nodoc:
0
-          File.delete(real_file_path(name))
0
-        rescue SystemCallError => e
0
-          # If there's no cache, then there's nothing to complain about
0
-        end
0
-
0
-        def delete_matched(matcher, options) #:nodoc:
0
-          search_dir(@cache_path) do |f|
0
-            if f =~ matcher
0
-              begin
0
-                File.delete(f)
0
-              rescue SystemCallError => e
0
-                # If there's no cache, then there's nothing to complain about
0
-              end
0
-            end
0
-          end
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.exist?(path)
0
-          end
0
-
0
-          def search_dir(dir, &callback)
0
-            Dir.foreach(dir) do |d|
0
-              next if d == "." || d == ".."
0
-              name = File.join(dir, d)
0
-              if File.directory?(name)
0
-                search_dir(name, &callback)
0
-              else
0
-                callback.call name
0
-              end
0
-            end
0
-          end
0
-        end
0
-
0
-        class FileStore < UnthreadedFileStore #:nodoc:
0
-          def initialize(cache_path)
0
-            super(cache_path)
0
-            if ActionController::Base.allow_concurrency
0
-              @mutex = Mutex.new
0
-              FileStore.module_eval { include ThreadSafety }
0
-            end
0
-          end
0
-        end
0
-    end
0
-
0
-    # Sweepers are the terminators of the caching world and responsible for expiring caches when model objects change.
0
-    # They do this by being half-observers, half-filters and implementing callbacks for both roles. A Sweeper example:
0
-    #
0
-    #   class ListSweeper < ActionController::Caching::Sweeper
0
-    #     observe List, Item
0
-    #
0
-    #     def after_save(record)
0
-    #       list = record.is_a?(List) ? record : record.list
0
-    #       expire_page(:controller => "lists", :action => %w( show public feed ), :id => list.id)
0
-    #       expire_action(:controller => "lists", :action => "all")
0
-    #       list.shares.each { |share| expire_page(:controller => "lists", :action => "show", :id => share.url_key) }
0
-    #     end
0
-    #   end
0
-    #
0
-    # The sweeper is assigned in the controllers that wish to have its job performed using the <tt>cache_sweeper</tt> class method:
0
-    #
0
-    #   class ListsController < ApplicationController
0
-    #     caches_action :index, :show, :public, :feed
0
-    #     cache_sweeper :list_sweeper, :only => [ :edit, :destroy, :share ]
0
-    #   end
0
-    #
0
-    # In the example above, four actions are cached and three actions are responsible for expiring those caches.
0
-    module Sweeping
0
-      def self.included(base) #:nodoc:
0
-        base.extend(ClassMethods)
0
-      end
0
-
0
-      module ClassMethods #:nodoc:
0
-        def cache_sweeper(*sweepers)
0
-          return unless perform_caching
0
-          configuration = sweepers.extract_options!
0
-          sweepers.each do |sweeper|
0
-            ActiveRecord::Base.observers << sweeper if defined?(ActiveRecord) and defined?(ActiveRecord::Base)
0
-            sweeper_instance = Object.const_get(Inflector.classify(sweeper)).instance
0
-
0
-            if sweeper_instance.is_a?(Sweeper)
0
-              around_filter(sweeper_instance, :only => configuration[:only])
0
-            else
0
-              after_filter(sweeper_instance, :only => configuration[:only])
0
-            end
0
-          end
0
-        end
0
-      end
0
-    end
0
-
0
-    if defined?(ActiveRecord) and defined?(ActiveRecord::Observer)
0
-      class Sweeper < ActiveRecord::Observer #:nodoc:
0
-        attr_accessor :controller
0
-
0
-        def before(controller)
0
-          self.controller = controller
0
-          callback(:before)
0
-        end
0
-
0
-        def after(controller)
0
-          callback(:after)
0
-          # Clean up, so that the controller can be collected after this request
0
-          self.controller = nil
0
-        end
0
-
0
-        protected
0
-          # gets the action cache path for the given options.
0
-          def action_path_for(options)
0
-            ActionController::Caching::Actions::ActionCachePath.path_for(controller, options)
0
-          end
0
-
0
-          # Retrieve instance variables set in the controller.
0
-          def assigns(key)
0
-            controller.instance_variable_get("@#{key}")
0
-          end
0
-
0
-        private
0
-          def callback(timing)
0
-            controller_callback_method_name = "#{timing}_#{controller.controller_name.underscore}"
0
-            action_callback_method_name     = "#{controller_callback_method_name}_#{controller.action_name}"
0
-
0
-            send!(controller_callback_method_name) if respond_to?(controller_callback_method_name, true)
0
-            send!(action_callback_method_name)     if respond_to?(action_callback_method_name, true)
0
-          end
0
-
0
-          def method_missing(method, *arguments)
0
-            return if @controller.nil?
0
-            @controller.send!(method, *arguments)
0
-          end
0
-      end
0
-    end
0
-
0
-    module SqlCache
0
-      def self.included(base) #:nodoc:
0
-        if defined?(ActiveRecord) && ActiveRecord::Base.respond_to?(:cache)
0
-          base.alias_method_chain :perform_action, :caching
0
-        end
0
-      end
0
-
0
-      def perform_action_with_caching
0
-        ActiveRecord::Base.cache do
0
-          perform_action_without_caching
0
-        end
0
-      end
0
-    end
0
   end
0
-end
0
+end
0
\ No newline at end of file
...
31
32
33
34
 
35
 
36
37
38
 
39
40
 
41
42
 
43
44
45
46
47
48
 
 
 
 
 
49
50
51
...
31
32
33
 
34
35
36
37
38
 
39
40
 
41
42
 
43
44
45
46
47
 
 
48
49
50
51
52
53
54
55
0
@@ -31,21 +31,25 @@ module ActionView
0
       #      <%= render :partial => "topics", :collection => @topic_list %>
0
       #      <i>Topics listed alphabetically</i>
0
       #    <% end %>
0
-      def cache(name = {}, &block)
0
+      def cache(name = {}, options = nil, &block)
0
         template_extension = first_render[/\.(\w+)$/, 1].to_sym
0
+
0
         case template_extension
0
         when :erb, :rhtml
0
-          @controller.cache_erb_fragment(block, name)
0
+          @controller.cache_erb_fragment(block, name, options)
0
         when :rjs
0
-          @controller.cache_rjs_fragment(block, name)
0
+          @controller.cache_rjs_fragment(block, name, options)
0
         when :builder, :rxml
0
-          @controller.cache_rxml_fragment(block, name)
0
+          @controller.cache_rxml_fragment(block, name, options)
0
         else
0
           # do a last ditch effort for those brave souls using
0
           # different template engines. This should give plugin
0
           # writters a simple hook.
0
-          raise "fragment caching not supported for #{template_extension} files." unless @controller.respond_to?("cache_#{template_extension}_fragment")
0
-          @controller.send "cache_#{template_extension}_fragment", block, name
0
+          unless @controller.respond_to?("cache_#{template_extension}_fragment")
0
+            raise "fragment caching not supported for #{template_extension} files."
0
+          end
0
+
0
+          @controller.send!("cache_#{template_extension}_fragment", block, name, options)
0
         end
0
       end
0
     end
...
5
6
7
8
 
9
10
11
...
343
344
345
346
 
347
348
349
...
355
356
357
358
359
 
 
360
361
362
...
368
369
370
371
372
 
 
373
374
375
376
377
 
378
379
380
381
382
383
 
384
385
386
387
388
 
389
390
 
391
392
393
394
 
395
396
397
 
398
399
400
401
 
402
403
 
404
405
406
407
408
409
 
 
 
410
411
412
413
414
415
 
 
 
416
417
418
419
420
421
 
422
423
424
...
430
431
432
433
 
434
435
436
...
441
442
443
444
 
445
446
447
...
449
450
451
452
 
453
454
455
...
458
459
460
461
 
462
463
464
...
466
467
468
469
 
470
471
472
...
5
6
7
 
8
9
10
11
...
343
344
345
 
346
347
348
349
...
355
356
357
 
 
358
359
360
361
362
...
368
369
370
 
 
371
372
373
374
375
376
 
377
378
379
380
381
382
 
383
384
385
386
387
 
388
389
 
390
391
392
393
 
394
395
396
 
397
398
399
400
 
401
402
 
403
404
405
406
 
 
 
407
408
409
410
411
412
 
 
 
413
414
415
416
417
418
419
420
 
421
422
423
424
...
430
431
432
 
433
434
435
436
...
441
442
443
 
444
445
446
447
...
449
450
451
 
452
453
454
455
...
458
459
460
 
461
462
463
464
...
466
467
468
 
469
470
471
472
0
@@ -5,7 +5,7 @@ CACHE_DIR = 'test_cache'
0
 # Don't change '/../temp/' cavalierly or you might hose something you don't want hosed
0
 FILE_STORE_PATH = File.join(File.dirname(__FILE__), '/../temp/', CACHE_DIR)
0
 ActionController::Base.page_cache_directory = FILE_STORE_PATH
0
-ActionController::Base.fragment_cache_store = :file_store, FILE_STORE_PATH
0
+ActionController::Base.cache_store = :file_store, FILE_STORE_PATH
0
 
0
 class PageCachingTestController < ActionController::Base
0
   caches_page :ok, :no_content, :found, :not_found
0
@@ -343,7 +343,7 @@ class ActionCacheTest < Test::Unit::TestCase
0
     end
0
 
0
     def assert_cache_exists(path)
0
-      full_path = File.join(FILE_STORE_PATH, path + '.cache')
0
+      full_path = File.join(FILE_STORE_PATH, "views", path + '.cache')
0
       assert File.exist?(full_path), "#{full_path.inspect} does not exist."
0
     end
0
 end
0
@@ -355,8 +355,8 @@ end
0
 class FragmentCachingTest < Test::Unit::TestCase
0
   def setup
0
     ActionController::Base.perform_caching = true
0
-    @store = ActionController::Caching::Fragments::UnthreadedMemoryStore.new
0
-    ActionController::Base.fragment_cache_store = @store
0
+    @store = ActiveSupport::Cache::MemoryStore.new
0
+    ActionController::Base.cache_store = @store
0
     @controller = FragmentCachingTestController.new
0
     @params = {:controller => 'posts', :action => 'index'}
0
     @request = ActionController::TestRequest.new
0
@@ -368,57 +368,57 @@ class FragmentCachingTest < Test::Unit::TestCase
0
   end
0
 
0
   def test_fragement_cache_key
0
-    assert_equal 'what a key', @controller.fragment_cache_key('what a key')
0
-    assert_equal( "test.host/fragment_caching_test/some_action",
0
+    assert_equal 'views/what a key', @controller.fragment_cache_key('what a key')
0
+    assert_equal( "views/test.host/fragment_caching_test/some_action",
0
                   @controller.fragment_cache_key(:controller => 'fragment_caching_test',:action => 'some_action'))
0
   end
0
 
0
   def test_read_fragment__with_caching_enabled
0
-    @store.write('name', 'value')
0
+    @store.write('views/name', 'value')
0
     assert_equal 'value', @controller.read_fragment('name')
0
   end
0
 
0
   def test_read_fragment__with_caching_disabled
0
     ActionController::Base.perform_caching = false
0
-    @store.write('name', 'value')
0
+    @store.write('views/name', 'value')
0
     assert_nil @controller.read_fragment('name')
0
   end
0
 
0
   def test_write_fragment__with_caching_enabled
0
-    assert_nil @store.read('name')
0
+    assert_nil @store.read('views/name')
0
     assert_equal 'value', @controller.write_fragment('name', 'value')
0
-    assert_equal 'value', @store.read('name')
0
+    assert_equal 'value', @store.read('views/name')
0
   end
0
 
0
   def test_write_fragment__with_caching_disabled
0
-    assert_nil @store.read('name')
0
+    assert_nil @store.read('views/name')
0
     ActionController::Base.perform_caching = false
0
     assert_equal nil, @controller.write_fragment('name', 'value')
0
-    assert_nil @store.read('name')
0
+    assert_nil @store.read('views/name')
0
   end
0
 
0
   def test_expire_fragment__with_simple_key
0
-    @store.write('name', 'value')
0
+    @store.write('views/name', 'value')
0
     @controller.expire_fragment 'name'
0
-    assert_nil @store.read('name')
0
+    assert_nil @store.read('views/name')
0
   end
0
 
0
   def test_expire_fragment__with__regexp
0
-    @store.write('name', 'value')
0
-    @store.write('another_name', 'another_value')
0
-    @store.write('primalgrasp', 'will not expire ;-)')
0
+    @store.write('views/name', 'value')
0
+    @store.write('views/another_name', 'another_value')
0
+    @store.write('views/primalgrasp', 'will not expire ;-)')
0
 
0
     @controller.expire_fragment /name/
0
 
0
-    assert_nil @store.read('name')
0
-    assert_nil @store.read('another_name')
0
-    assert_equal 'will not expire ;-)', @store.read('primalgrasp')
0
+    assert_nil @store.read('views/name')
0
+    assert_nil @store.read('views/another_name')
0
+    assert_equal 'will not expire ;-)', @store.read('views/primalgrasp')
0
   end
0
 
0
   def test_fragment_for__with_disabled_caching
0
     ActionController::Base.perform_caching = false
0
 
0
-    @store.write('expensive', 'fragment content')
0
+    @store.write('views/expensive', 'fragment content')
0
     fragment_computed = false
0
 
0
     buffer = 'generated till now -> '
0
@@ -430,7 +430,7 @@ class FragmentCachingTest < Test::Unit::TestCase
0
 
0
 
0
   def test_fragment_for
0
-    @store.write('expensive', 'fragment content')
0
+    @store.write('views/expensive', 'fragment content')
0
     fragment_computed = false
0
 
0
     buffer = 'generated till now -> '
0
@@ -441,7 +441,7 @@ class FragmentCachingTest < Test::Unit::TestCase
0
   end
0
 
0
   def test_cache_erb_fragment
0
-    @store.write('expensive', 'fragment content')
0
+    @store.write('views/expensive', 'fragment content')
0
     _erbout = 'generated till now -> '
0
 
0
     assert_equal( 'generated till now -> fragment content',
0
@@ -449,7 +449,7 @@ class FragmentCachingTest < Test::Unit::TestCase
0
   end
0
 
0
   def test_cache_rxml_fragment
0
-    @store.write('expensive', 'fragment content')
0
+    @store.write('views/expensive', 'fragment content')
0
     xml = 'generated till now -> '
0
     class << xml; def target!; to_s; end; end
0
 
0
@@ -458,7 +458,7 @@ class FragmentCachingTest < Test::Unit::TestCase
0
   end
0
 
0
   def test_cache_rjs_fragment
0
-    @store.write('expensive', 'fragment content')
0
+    @store.write('views/expensive', 'fragment content')
0
     page = 'generated till now -> '
0
 
0
     assert_equal( 'generated till now -> fragment content',
0
@@ -466,7 +466,7 @@ class FragmentCachingTest < Test::Unit::TestCase
0
   end
0
 
0
   def test_cache_rjs_fragment_debug_mode_does_not_interfere
0
-    @store.write('expensive', 'fragment content')
0
+    @store.write('views/expensive', 'fragment content')
0
     page = 'generated till now -> '
0
 
0
     begin
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
@@ -1,47 +0,0 @@
0
-require File.dirname(__FILE__) + '/../abstract_unit'
0
-
0
-MemCache = Struct.new(:MemCache, :address) unless Object.const_defined?(:MemCache)
0
-
0
-class FragmentCacheStoreSettingTest < Test::Unit::TestCase
0
-  def teardown
0
-    ActionController::Base.fragment_cache_store = ActionController::Caching::Fragments::MemoryStore.new
0
-  end
0
-  
0
-  def test_file_fragment_cache_store
0
-    ActionController::Base.fragment_cache_store = :file_store, "/path/to/cache/directory"
0
-    assert_kind_of(
0
-      ActionController::Caching::Fragments::FileStore,
0
-      ActionController::Base.fragment_cache_store
0
-    )
0
-    assert_equal "/path/to/cache/directory", ActionController::Base.fragment_cache_store.cache_path
0
-  end
0
-  
0
-  def test_drb_fragment_cache_store
0
-    ActionController::Base.fragment_cache_store = :drb_store, "druby://localhost:9192"
0
-    assert_kind_of(
0
-      ActionController::Caching::Fragments::DRbStore,
0
-      ActionController::Base.fragment_cache_store
0
-    )
0
-    assert_equal "druby://localhost:9192", ActionController::Base.fragment_cache_store.address
0
-  end
0
-
0
-  if defined? CGI::Session::MemCacheStore
0
-    def test_mem_cache_fragment_cache_store
0
-      ActionController::Base.fragment_cache_store = :mem_cache_store, "localhost"
0
-      assert_kind_of(
0
-        ActionController::Caching::Fragments::MemCacheStore,
0
-        ActionController::Base.fragment_cache_store
0
-      )
0
-      assert_equal %w(localhost), ActionController::Base.fragment_cache_store.addresses
0
-    end
0
-  end
0
-
0
-  def test_object_assigned_fragment_cache_store
0
-    ActionController::Base.fragment_cache_store = ActionController::Caching::Fragments::FileStore.new("/path/to/cache/directory")
0
-    assert_kind_of(
0
-      ActionController::Caching::Fragments::FileStore,
0
-      ActionController::Base.fragment_cache_store
0
-    )
0
-    assert_equal "/path/to/cache/directory", ActionController::Base.fragment_cache_store.cache_path
0
-  end
0
-end
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* Added ActiveRecord::Base.cache_key to make it easier to cache Active Records in combination with the new ActiveSupport::Cache::* libraries [DHH]
0
+
0
 * Make sure CSV fixtures are compatible with ruby 1.9's new csv implementation. [JEG2]
0
 
0
 * Added by parameter to increment, decrement, and their bang varieties so you can do player1.increment!(:points, 5) #10542 [Sam]
...
1959
1960
1961
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1962
1963
1964
...
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
0
@@ -1959,6 +1959,22 @@ module ActiveRecord #:nodoc:
0
         # We can't use alias_method here, because method 'id' optimizes itself on the fly.
0
         (id = self.id) ? id.to_s : nil # Be sure to stringify the id for routes
0
       end
0
+      
0
+      # Returns a cache key that can be used to identify this record. Examples:
0
+      #
0
+      #   Product.new.cache_key     # => "products/new"
0
+      #   Product.find(5).cache_key # => "products/5" (updated_at not available)
0
+      #   Person.find(5).cache_key  # => "people/5-20071224150000" (updated_at available)
0
+      def cache_key
0
+        case 
0
+        when new_record?
0
+          "#{self.class.name.tableize}/new"
0
+        when self[:updated_at]
0
+          "#{self.class.name.tableize}/#{id}-#{updated_at.to_s(:number)}"
0
+        else
0
+          "#{self.class.name.tableize}/#{id}"
0
+        end
0
+      end
0
 
0
       def id_before_type_cast #:nodoc:
0
         read_attribute_before_type_cast(self.class.primary_key)
...
1
2
 
 
 
 
 
 
3
4
5
...
1
2
3
4
5
6
7
8
9
10
11
0
@@ -1,5 +1,11 @@
0
 *SVN*
0
 
0
+* Added ActiveSupport::Gzip.decompress/compress(source) as an easy wrapper for Zlib [Tobias Luetke]
0
+
0
+* Included MemCache-Client to make the improved ActiveSupport::Cache::MemCacheStore work out of the box [Bob Cottrell, Eric Hodel]
0
+
0
+* Added ActiveSupport::Cache::* framework as an extraction from ActionController::Caching::Fragments::* [DHH]
0
+
0
 * Fixed String#titleize to work for strings with 's too #10571 [trek]
0
 
0
 * Changed the implementation of Enumerable#group_by to use a double array approach instead of a hash such that the insert order is honored [DHH/Marcel]
...
32
33
34
 
 
 
35
36
37
...
32
33
34
35
36
37
38
39
40
0
@@ -32,6 +32,9 @@ require 'active_support/core_ext'
0
 require 'active_support/clean_logger'
0
 require 'active_support/buffered_logger'
0
 
0
+require 'active_support/gzip'
0
+require 'active_support/cache'
0
+
0
 require 'active_support/dependencies'
0
 require 'active_support/deprecation'
0
 
...
7
8
9
 
10
11
12
...
7
8
9
10
11
12
13
0
@@ -7,6 +7,7 @@ module ActiveSupport #:nodoc:
0
           :short        => "%e %b",
0
           :long         => "%B %e, %Y",
0
           :db           => "%Y-%m-%d",
0
+          :number       => "%Y%m%d",
0
           :long_ordinal => lambda { |date| date.strftime("%B #{date.day.ordinalize}, %Y") }, # => "April 25th, 2007"
0
           :rfc822       => "%e %b %Y"
0
         }
...
94
95
96
 
 
97
98
99
...
94
95
96
97
98
99
100
101
0
@@ -94,6 +94,8 @@ module ActiveSupport #:nodoc:
0
             value.to_query(namespace ? "#{namespace}[#{key}]" : key)
0
           end.sort * '&'
0
         end
0
+        
0
+        alias_method :to_param, :to_query
0
 
0
         def to_xml(options = {})
0
           options[:indent] ||= 2
...
5
6
7
 
8
9
10
...
5
6
7
8
9
10
11
0
@@ -5,6 +5,7 @@ module ActiveSupport #:nodoc:
0
       module Conversions
0
         DATE_FORMATS = {
0
           :db           => "%Y-%m-%d %H:%M:%S",
0
+          :number       => "%Y%m%d%H%M%S",
0
           :time         => "%H:%M",
0
           :short        => "%d %b %H:%M",
0
           :long         => "%B %d, %Y %H:%M",
...
12
13
14
 
 
 
 
 
 
15
...
12
13
14
15
16
17
18
19
20
21
0
@@ -12,3 +12,9 @@ begin
0
 rescue Gem::LoadError
0
   $:.unshift "#{File.dirname(__FILE__)}/vendor/xml-simple-1.0.11"
0
 end
0
+
0
+begin
0
+  gem 'memcache-client', '~> 1.5.0'
0
+rescue Gem::LoadError
0
+  $:.unshift "#{File.dirname(__FILE__)}/vendor/memcache-client-1.5.0"
0
+end
0
\ No newline at end of file
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* Added config.cache_store to environment options to control the default cache store (default is FileStore if tmp/cache is present, otherwise MemoryStore is used) [DHH]
0
+
0
 * Added that rails:update is run when you do rails:freeze:edge to ensure you also get the latest JS and config files #10565 [jeff]
0
 
0
 * SQLite: db:drop:all doesn't fail silently if the database is already open.  #10577 [Cheah Chu Yeow, mrichman]
...
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
 
84
85
86
...
92
93
94
 
 
 
 
95
96
 
97
98
99
100
 
101
102
103
...
239
240
241
 
 
 
 
 
 
 
 
 
 
 
 
242
243
244
...
277
278
279
 
 
280
281
282
...
309
310
311
312
 
313
314
315
316
317
318
319
320
321
322
323
...
418
419
420
 
 
 
421
422
423
...
647
648
649
 
 
 
 
 
 
 
 
650
651
652
...
58
59
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
62
63
64
...
70
71
72
73
74
75
76
77
78
79
80
81
82
 
83
84
85
86
...
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
...
272
273
274
275
276
277
278
279
...
306
307
308
 
309
310
311
312
 
 
 
 
 
313
314
315
...
410
411
412
413
414
415
416
417
418
...
642
643
644
645
646
647
648
649
650
651
652
653
654
655
0
@@ -58,29 +58,7 @@ module Rails
0
     end
0
 
0
     # Sequentially step through all of the available initialization routines,
0
-    # in order:
0
-    #
0
-    # * #check_ruby_version
0
-    # * #set_load_path
0
-    # * #require_frameworks
0
-    # * #set_autoload_paths
0
-    # * add_plugin_load_paths
0
-    # * #load_environment
0
-    # * #initialize_encoding
0
-    # * #initialize_database
0
-    # * #initialize_logger
0
-    # * #initialize_framework_logging
0
-    # * #initialize_framework_views
0
-    # * #initialize_dependency_mechanism
0
-    # * #initialize_whiny_nils
0
-    # * #initialize_temporary_directories
0
-    # * #initialize_framework_settings
0
-    # * #add_support_load_paths
0
-    # * #load_plugins
0
-    # * #load_observers
0
-    # * #initialize_routing
0
-    # * #after_initialize
0
-    # * #load_application_initializers
0
+    # in order (view execution order in source).
0
     def process
0
       check_ruby_version
0
       set_load_path
0
@@ -92,12 +70,17 @@ module Rails
0
 
0
       initialize_encoding
0
       initialize_database
0
+
0
+      initialize_cache
0
+      initialize_framework_caches
0
+
0
       initialize_logger
0
       initialize_framework_logging
0
+
0
       initialize_framework_views
0
       initialize_dependency_mechanism
0
       initialize_whiny_nils
0
-      initialize_temporary_directories
0
+      initialize_temporary_session_directory
0
       initialize_framework_settings
0
 
0
       add_support_load_paths
0
@@ -239,6 +222,18 @@ module Rails
0
       end
0
     end
0
 
0
+    def initialize_cache
0
+      unless defined?(RAILS_CACHE)
0
+        silence_warnings { Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(configuration.cache_store) }
0
+      end
0
+    end
0
+
0
+    def initialize_framework_caches
0
+      if configuration.frameworks.include?(:action_controller)
0
+        ActionController::Base.cache_store ||= RAILS_CACHE
0
+      end
0
+    end
0
+
0
     # If the +RAILS_DEFAULT_LOGGER+ constant is already set, this initialization
0
     # routine does nothing. If the constant is not set, and Configuration#logger
0
     # is not +nil+, this also does nothing. Otherwise, a new logger instance
0
@@ -277,6 +272,8 @@ module Rails
0
       for framework in ([ :active_record, :action_controller, :action_mailer ] & configuration.frameworks)
0
         framework.to_s.camelize.constantize.const_get("Base").logger ||= RAILS_DEFAULT_LOGGER
0
       end
0
+      
0
+      RAILS_CACHE.logger ||= RAILS_DEFAULT_LOGGER
0
     end
0
 
0
     # Sets +ActionController::Base#view_paths+ and +ActionMailer::Base#template_root+
0
@@ -309,15 +306,10 @@ module Rails
0
       require('active_support/whiny_nil') if configuration.whiny_nils
0
     end
0
 
0
-    def initialize_temporary_directories
0
+    def initialize_temporary_session_directory
0
       if configuration.frameworks.include?(:action_controller)
0
         session_path = "#{configuration.root_path}/tmp/sessions/"
0
         ActionController::Base.session_options[:tmpdir] = File.exist?(session_path) ? session_path : Dir::tmpdir
0
-
0
-        cache_path = "#{configuration.root_path}/tmp/cache/"
0
-        if File.exist?(cache_path)
0
-          ActionController::Base.fragment_cache_store = :file_store, cache_path
0
-        end
0
       end
0
     end
0
 
0
@@ -418,6 +410,9 @@ module Rails
0
     # used directly.
0
     attr_accessor :logger
0
 
0
+    # The specific cache store to use. By default, the ActiveSupport::Cache::Store will be used.
0
+    attr_accessor :cache_store
0
+
0
     # The root of the application's views. (Defaults to <tt>app/views</tt>.)
0
     attr_accessor :view_path
0
 
0
@@ -647,6 +642,14 @@ module Rails
0
       def default_plugin_loader
0
         Plugin::Loader
0
       end
0
+      
0
+      def default_cache_store
0
+        if File.exist?("#{root_path}/tmp/cache/")
0
+          [ :file_store, "#{root_path}/tmp/cache/" ]
0
+        else
0
+          :memory_store
0
+        end
0
+      end
0
   end
0
 end
0
 

Comments