# What Happens When You Run Rails dev:cache Command?

*Published on September 30, 2023*

Tags: Rails Internals

---


> The&nbsp;`rails dev:cache`&nbsp;command toggles caching in development.

By default, Rails enables caching only in production environment. To enable it in the development mode, you can run&nbsp;`rails dev:cache`&nbsp;command. To disable it, re-run the command.

```bash
$ bin/rails dev:cache
Development mode is now being cached.

$ bin/rails dev:cache
Development mode is no longer being cached.
```

Behind the scenes, it's implemented as a&nbsp;[Thor](https://github.com/rails/thor)&nbsp;command that delegates to the&nbsp;`Rails::DevCaching`&nbsp;module.

```ruby
# railties/lib/rails/commands/dev/dev_command.rb

require "rails/dev_caching"

module Rails
  module Command
    class DevCommand < Base
      desc "cache", "Toggle development mode caching on/off"
      def cache
        Rails::DevCaching.enable_by_file
      end
    end
  end
end
```

The&nbsp;`DevCaching::enable_by_file`&nbsp;method does the following:

1. Create a&nbsp;`tmp`&nbsp;directory if it doesn't exist.
2. If the caching file (named&nbsp;`caching-dev.txt`) is present inside the&nbsp;`tmp`&nbsp;directory, it means caching is enabled. Delete that file to disable it. Otherwise, create it to enable caching.
3. Finally, touch the&nbsp;`restart.txt`&nbsp;file to instruct Rails to restart the app.

```ruby
# 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
          puts "Development mode is no longer being cached."
        else
          create_cache_file
          puts "Development mode is now being cached."
        end

        FileUtils.touch "tmp/restart.txt"
      end
    end
  end
end
```

## How Rails Uses the Caching File?

When your Rails application starts, it checks for the presence of the caching file above, and if it exists, it does the following:

1. Enable&nbsp;`perform_caching`&nbsp;and&nbsp;`enable_fragment_cache_logging`&nbsp;on the controller.
2. Set the cache store to&nbsp;`:memory_store`.
3. Add the&nbsp;`Cache-Control`&nbsp;header to public with max-age set to two days.

Otherwise, it disables caching and uses the&nbsp;`:null_store`.

```ruby
# config/environments/development.rb

if Rails.root.join("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-Control" => "public, max-age=#{2.days.to_i}"
  }
else
  config.action_controller.perform_caching = false

  config.cache_store = :null_store
end
```

- **`perform_caching`**&nbsp;configures whether the application should perform the caching features provided by Action Controller or not.
- **`enable_fragment_cache_logging`**&nbsp;determines whether to log fragment cache reads and writes in a verbose format.
- **`cache_store`**&nbsp;configures which cache store to use for Rails caching.

The above settings instruct Rails to perform caching. How it implements it, well, that's a topic for another blog post. Stay tuned.

> 💡 This post is part of the&nbsp;[Rails Internals](/tag/rails-internals)&nbsp;series that explores the internals of Ruby on Rails by reading the source code.

