Last Updated: February 25, 2016
·
855
· jelder

Keep an eye on your cache keys

"There are only two hard problems in Computer Science:
cache invalidation and naming things."
-- Phil Karlton

Most Rails apps rely heavily on key-based cache invalidation. Unfortunately, Rails won't log cache keys if caching is disabled. At the same time, enabling caching in your development environment is usually a bad idea. Constantly flushing the cache by hand is a frustrating distraction from actual work. This makes it easy to accidentally design an inappropriate cache key.

You can work around this by simultaneously enabling caching in your config/development.rb and disabling caching by not starting memcached.

Enable caching only for yourself (if your collaborators prefer not to be bothered by cache info in their logs).

config.cache_store = :dalli_store
config.action_controller.perform_caching = ( %w[ jacob ].include? ENV["USER"] )

Stop memcached (assuming Mac here).

$ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist

And prevent memcached from starting at next boot.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>homebrew.mxcl.memcached</string>
  <key>KeepAlive</key>
  <true/>
  <key>ProgramArguments</key>
  <array>
    <string>/usr/local/bin/memcached</string>
    <string>-l</string>
    <string>localhost</string>
  </array>
  <key>RunAtLoad</key>
  <false/>
  <key>WorkingDirectory</key>
  <string>/usr/local</string>
</dict>
</plist>

Next time you render a view, you'll see every cache key listed out explicitly in log/development.log.

1 Response
Add your response

For clarification of that last bit, you're editing line 16 of ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist, changing it from <true/> to <false/>.

over 1 year ago ·