The
rails dev:cache
command toggles caching in improvement.
By default, Rails allows caching solely in manufacturing surroundings. To allow it within the improvement mode, you’ll be able to run rails dev:cache
command. To disable it, re-run the command.
$ bin/rails dev:cache
Improvement mode is now being cached.
$ bin/rails dev:cache
Improvement mode is not being cached.
Behind the scenes, it is carried out as a Thor command that delegates to the Rails::DevCaching
module.
# railties/lib/rails/instructions/dev/dev_command.rb
require "rails/dev_caching"
module Rails
module Command
class DevCommand < Base
desc "cache", "Toggle improvement mode caching on/off"
def cache
Rails::DevCaching.enable_by_file
finish
finish
finish
finish
The DevCaching::enable_by_file
methodology does the next:
- Create a
tmp
listing if it does not exist. - If the caching file (named
caching-dev.txt
) is current contained in thetmp
listing, it means caching is enabled. Delete that file to disable it. In any other case, create it to allow caching. - Lastly, contact the
restart.txt
file to instruct Rails to restart the app.
# railties/lib/rails/dev_caching.rb
require "fileutils"
module Rails
module DevCaching
class << self
FILE = "tmp/caching-dev.txt"
def enable_by_file
FileUtils.mkdir_p("tmp")
if File.exist?(FILE)
delete_cache_file
places "Improvement mode is not being cached."
else
create_cache_file
places "Improvement mode is now being cached."
finish
FileUtils.contact "tmp/restart.txt"
finish
finish
finish
finish
How Rails Makes use of the Caching File?
When your Rails utility begins, it checks for the presence of the caching file above, and if it exists, it does the next:
- Allow
perform_caching
andenable_fragment_cache_logging
on the controller. - Set the cache retailer to
:memory_store
. - Add the
Cache-Management
header to public with max-age set to 2 days.
In any other case, it disables caching and makes use of the :null_store
.
# config/environments/improvement.rb
if Rails.root.be part of("tmp/caching-dev.txt").exist?
config.action_controller.perform_caching = true
config.action_controller.enable_fragment_cache_logging = true
config.cache_store = :memory_store
config.public_file_server.headers = {
"Cache-Management" => "public, max-age=#{2.days.to_i}"
}
else
config.action_controller.perform_caching = false
config.cache_store = :null_store
finish
perform_caching
configures whether or not the appliance ought to carry out the caching options offered by Motion Controller or not.enable_fragment_cache_logging
determines whether or not to log fragment cache reads and writes in a verbose format.cache_store
configures which cache retailer to make use of for Rails caching.
The above settings instruct Rails to carry out caching. The way it implements it, properly, that is a subject for one more weblog publish. Keep tuned.
💡
That is a wrap. I hope you favored this text and also you discovered one thing new.
As at all times, when you have any questions or suggestions, did not perceive one thing, or discovered a mistake, please go away a remark beneath or ship me an e-mail. I reply to all emails I get from builders, and I sit up for listening to from you.
If you would like to obtain future articles straight in your e-mail, please subscribe to my weblog. For those who’re already a subscriber, thanks.