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'


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/action_controller/caching/pages.rb', line 92

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
    tmpfile = Tempfile.new([File.basename(path), File.extname(path)])
    tmpfile.write(content)
    tmpfile.close

    dirname = File.dirname(path)
    FileUtils.makedirs(dirname) unless File.directory?(dirname)
    FileUtils.mv(tmpfile.path, path)

    if gzip
      Zlib::GzipWriter.open(tmpfile.path + '.gz', gzip) { |gz| gz.write(content) }
      FileUtils.mv(tmpfile.path + '.gz', path + '.gz')
    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


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/action_controller/caching/pages.rb', line 126

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'


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

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

#page_cache_compressionObject

The compression used for gzip. If false (default), the page is not compressed. If can be a symbol showing the ZLib compression method, for example, :best_compression or :best_speed or an integer configuring the compression level.



60
61
62
# File 'lib/action_controller/caching/pages.rb', line 60

def page_cache_compression
  thread_storage[:page_cache_compression]
end

#page_cache_compression=(value) ⇒ Object



68
69
70
# File 'lib/action_controller/caching/pages.rb', line 68

def page_cache_compression=(value)
  thread_storage[:page_cache_compression] = value
end

#page_cache_directoryObject

The cache directory should be the document root for the web server and is set using Base.page_cache_directory = "/document/root". For Rails, this directory has already been set to Rails.public_path (which is usually set to Rails.root + "/public"). Changing this setting can be useful to avoid naming conflicts with files in public/, but doing so will likely require configuring your web server to look in the new location for cached files.



53
54
55
# File 'lib/action_controller/caching/pages.rb', line 53

def page_cache_directory
  thread_storage[:page_cache_directory]
end

#page_cache_directory=(value) ⇒ Object



64
65
66
# File 'lib/action_controller/caching/pages.rb', line 64

def page_cache_directory=(value)
  thread_storage[:page_cache_directory] = value
end

#thread_storageObject



72
73
74
# File 'lib/action_controller/caching/pages.rb', line 72

def thread_storage
  Thread.current[:multiple_page_caching] ||= {}
end