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'


80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/action_controller/caching/pages.rb', line 80

def cache_page(content, path, extension = nil, gzip = Zlib::BEST_COMPRESSION)
  return unless perform_caching
  path = page_cache_path(path, extension)

  instrument_page_cache :write_page, path do
    FileUtils.makedirs(File.dirname(path))
    File.open(path, 'wb+') { |f| f.write(content) }
    if gzip
      Zlib::GzipWriter.open(path + '.gz', gzip) { |f| f.write(content) }
    end
  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


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/action_controller/caching/pages.rb', line 107

def caches_page(*actions)
  return unless 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 Fixnum
    gzip_level
  when false
    nil
  else
    Zlib::BEST_COMPRESSION
  end

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

#expire_page(path) ⇒ Object

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

expire_page '/lists/show'


67
68
69
70
71
72
73
74
75
# File 'lib/action_controller/caching/pages.rb', line 67

def expire_page(path)
  return unless perform_caching
  path = page_cache_path(path)

  instrument_page_cache :expire_page, path do
    File.delete(path) if File.exist?(path)
    File.delete(path + '.gz') if File.exist?(path + '.gz')
  end
end