Module: ActionController::Caching::Pages::ClassMethods

Defined in:
lib/action_controller/caching/pages.rb

Instance Method Summary collapse

Instance Method Details

#cache_page(content, path, extension = nil, gzip = Zlib::BEST_COMPRESSION) ⇒ Object

Manually cache the content in the key determined by path.

cache_page "I'm the cached content", "/lists/show"


191
192
193
194
195
# File 'lib/action_controller/caching/pages.rb', line 191

def cache_page(content, path, extension = nil, gzip = Zlib::BEST_COMPRESSION)
  if perform_caching
    page_cache.cache(content, path, extension, gzip)
  end
end

#caches_page(*actions) ⇒ Object

Caches the actions using the page-caching approach that’ll store the cache in a path within the page_cache_directory that matches the triggering url.

You can also pass a :gzip option to override the class configuration one.

# cache the index action
caches_page :index

# cache the index action except for JSON requests
caches_page :index, if: Proc.new { !request.format.json? }

# don't gzip images
caches_page :image, gzip: false


211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/action_controller/caching/pages.rb', line 211

def caches_page(*actions)
  if perform_caching
    options = actions.extract_options!

    gzip_level = options.fetch(:gzip, page_cache_compression)
    gzip_level = \
      case gzip_level
      when Symbol
        Zlib.const_get(gzip_level.upcase)
      when Integer
        gzip_level
      when false
        nil
      else
        Zlib::BEST_COMPRESSION
      end

    after_action({ only: actions }.merge(options)) do |c|
      c.cache_page(nil, nil, gzip_level)
    end
  end
end

#expire_page(path) ⇒ Object

Expires the page that was cached with the path as a key.

expire_page "/lists/show"


182
183
184
185
186
# File 'lib/action_controller/caching/pages.rb', line 182

def expire_page(path)
  if perform_caching
    page_cache.expire(path)
  end
end