* When serving Rails applications, web servers are configured to first check the /public directory for a file
matching the request. If the file is not found, they pass the request to Rails.
178 | Chapter 6: Performance
Action caching is triggered by the caches_action method. Here??™s an example that
only allows a page to be viewed on Tuesdays:
class UserController < ApplicationController
before_filter :tuesday?, :only => :happy_tuesday
caches_action :happy_tuesday
def happy_tuesday
render :text => "Happy Tuesday!"
end
protected
def tuesday?
Time.now.wday == 2
end
end
Action-cached pages are expired with the expire_action method, which is again best
called from a cache sweeper. Alternatively, since action caching is implemented on
top of fragment caching, you can use the expire_fragment method to expire one or
many actions at once. You can even use a regular expression to expire all cached
instances of one action (or all actions if you like).
Fragment caching
When the preceding options fail, fragment caching can help. Fragment caching is
the most flexible, but least helpful, option. It is designed to store small fragments of
the page, but it makes no assumptions about your data. You can store HTML fragments,
XML, JSON, or even images in the fragment cache.
Manual access to the fragment cache uses the read_fragment, write_fragment, and
expire_fragment methods.
Pages:
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