In Rails 8.1 development, what does bin/rails dev:cache actually switch, and why does Rails.cache.fetch keep caching when it is off?
answer
- a marker file under tmp/
- tmp/caching-dev.txt plus tmp/restart.txt
- perform_caching true or false
- the store line never changes
- server -C does the same at boot
basics
~10 sbin/rails dev:cache creates or deletes tmp/caching-dev.txt, and development.rb sets config.action_controller.perform_caching from that file, which gates Action Controller caching such as view fragments; config.cache_store stays :memory_store, so Rails.cache.fetch caches either way.
solid answer
~40 s`bin/rails dev:cache` is a toggle: if `tmp/caching-dev.txt` exists it deletes it, otherwise it creates it, prints whether Action Controller caching is now enabled or disabled, and touches `tmp/restart.txt` so Puma's `tmp_restart` plugin reloads the app. The generated `development.rb` checks that file: when present it sets `config.action_controller.perform_caching = true`, turns on fragment cache logging and adds a two-day `cache-control` header for public files; when absent it sets `perform_caching` to `false`. That flag only gates caching provided by Action Controller, such as the view `cache` helper. `Rails.cache` is still the `:memory_store` that `config.cache_store` names, so low-level `Rails.cache.fetch` calls keep caching. To stop every kind of caching, set `config.cache_store = :null_store`, as the template's own comment says.
code
bash · 5 lines$ bin/rails dev:cache
Action Controller caching enabled for development mode.
$ bin/rails dev:cache
Action Controller caching disabled for development mode.
$ bin/rails server -C # create the marker file, then bootgo deeper
Remember that bin/rails dev:cache is a toggle stored in tmp/caching-dev.txt and that it controls view and controller caching in development, not the store itself.
Explain the two layers: config.cache_store builds Rails.cache, while perform_caching gates Action Controller caching. Low-level Rails.cache calls ignore the flag, and :null_store is the way to stop them.
Use this to explain confusing development behaviour quickly: per-process memory stores, a console that cannot see the server's cache, and fragment cache logging to prove whether keys hit before chasing a production cache bug.
Set a team convention for how caching is exercised before production, such as running development with caching on for cache-heavy features, so key mistakes surface on a laptop rather than after deploy.
## Two switches, not one Rails separates **where** cached data lives from **whether Action Controller uses caching at all**: - **`config.cache_store`** builds `Rails.cache`, the store object. In a generated development environment it is `:memory_store`. - **`config.action_controller.perform_caching`** is a flag on controllers. Action Controller's caching code checks `perform_caching && cache_store` before using the store, and the view `cache` helper checks `controller.perform_caching` before it reads or writes a fragment. `bin/rails dev:cache` only drives the second one. That is why a developer can turn it off and still see `Rails.cache.fetch` return a cached value: low-level calls go straight to the store object and never consult the controller flag. The Rails caching guide says it plainly: changing `perform_caching` only affects caching provided by Action Controller. ## What the command does, step by step The command is `Rails::Command::DevCommand#cache`, which calls `Rails::DevCaching.enable_by_file`: 1. It makes sure `tmp/` exists. 2. If `tmp/caching-dev.txt` exists, it deletes the file and prints `Action Controller caching disabled for development mode.` 3. Otherwise it creates the file and prints `Action Controller caching enabled for development mode.` 4. Either way it touches `tmp/restart.txt`. The generated `config/puma.rb` loads `plugin :tmp_restart`, so a running Puma restarts and re-reads `development.rb`. The state lives in that marker file, so it survives server restarts and is per checkout, which is why it belongs in `tmp/` and out of version control. ## What `development.rb` does with the file The generated development file contains this branch: | `tmp/caching-dev.txt` | Settings applied | |---|---| | present | `perform_caching = true`, `enable_fragment_cache_logging = true`, public files served with `cache-control: public, max-age=172800` | | absent | `perform_caching = false` | | either | `config.cache_store = :memory_store` | In an app generated with `--api`, the `true` branch leaves out the two Action Controller lines because there are no view fragments to cache, but the file still controls the public-file header. Fragment cache logging is the part developers notice: with it on, the log prints a `Read fragment` or `Write fragment` line for each cached block, which is the quickest way to see whether your fragment keys hit. ## Starting the server with caching set `bin/rails server` accepts **`--dev-caching`** (alias **`-C`**) and **`--no-dev-caching`**. These call `Rails::DevCaching.enable_by_argument`, which creates the marker file for `true` and deletes it for an explicit `false`, before the server boots; the flag is only honoured when the server starts in the development environment. Leaving the flag off leaves the current state as it is. ## Turning off every kind of caching Because the store is independent of the flag, the reliable way to make development behave as if nothing were cached is to change the store: - set `config.cache_store = :null_store`, which the generated comment suggests, so reads always miss and writes are discarded; - or clear what is there with `Rails.cache.clear` from a console when you only want a fresh start. Note the trap in the other direction too. A `:memory_store` lives inside each server process, so the console you open with `bin/rails console` has its own empty copy. Clearing the cache from the console does not clear the one inside the running server; restarting the server, for example with `bin/rails dev:cache` twice, does. ## Why interviewers ask this The question checks whether a candidate knows the two layers exist. Weak answers say that `dev:cache` "turns caching on" in general, or that it changes the store to something persistent. Strong answers name the marker file, the `perform_caching` flag it drives, the store that stays put, and the `:null_store` escape hatch.
- You cleared `Rails.cache` from `bin/rails console`, yet the development server still serves the old value. Why?With `:memory_store` each Ruby process owns its own hash. The console process cleared its copy, not the one inside the running Puma process. Restart the server, or clear from code that runs inside it, or use a store both processes reach, such as the file store, while you debug.
- How would you make development skip caching entirely, including `Rails.cache.fetch` calls in models?Set `config.cache_store = :null_store` in `development.rb`. `perform_caching = false` only stops Action Controller caching; the null store makes every read a miss and discards every write, so low-level calls recompute their blocks every time.
- bin/rails dev:cache switches config.cache_store between :null_store and :memory_store.
- With dev:cache off, Rails.cache.fetch always runs its block.
- dev:cache writes a setting into config/environments/development.rb.
- perform_caching = false disables low-level Rails.cache calls as well.
- Clearing Rails.cache in the console clears the running server's memory store.