{"id":3121,"date":"2018-06-23T06:33:30","date_gmt":"2018-06-23T12:33:30","guid":{"rendered":"http:\/\/www.mooreds.com\/wordpress\/?p=3121"},"modified":"2018-06-23T06:33:30","modified_gmt":"2018-06-23T12:33:30","slug":"rails-views-cached-in-production-environment","status":"publish","type":"post","link":"https:\/\/www.mooreds.com\/wordpress\/archives\/3121","title":{"rendered":"Rails Views Cached In Production Environment"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignright size-medium wp-image-3122\" src=\"http:\/\/www.mooreds.com\/wordpress\/wp-content\/uploads\/2018\/06\/seemed-3309912_640-300x200.jpg\" alt=\"Railroad tracks\" width=\"300\" height=\"200\" srcset=\"http:\/\/edit.mooreds.com\/wordpress\/wp-content\/uploads\/2018\/06\/seemed-3309912_640-300x200.jpg 300w, http:\/\/edit.mooreds.com\/wordpress\/wp-content\/uploads\/2018\/06\/seemed-3309912_640.jpg 640w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/>I was troubleshooting a data issue in a production environment. It wasn&#8217;t heroku, rather a rails environment hosted on AWS. It was Rails 4.2, ruby 2.2.3.<\/p>\n<p>First off, it&#8217;s worth noting that there were two or three bugs that were commingled and causing issues for our client. A number of folks had spent a long time trying to troubleshoot the issue. At this point, I was tasked with taking a look and had access to all the environments. The problem only seemed to appear on production, and appeared to be a data issue. I was editing views directly on production to track down where the data issue appeared, as well as running queries on the production database and using the rails console to see what rails thought was happening. In other words, it was a hot mess. However, this debugging story isn&#8217;t the point of this post. Rather, I ran into the most peculiar situation and wanted to document it so that if I ever ran into it in the future, I would remember it.<\/p>\n<p>Basically, I had a view that looked something like this:<\/p>\n<pre>text\r\n&lt;% cache('[key]') %&gt;\r\nother text\r\n&lt;% end %&gt;\r\n<\/pre>\n<p>I changed <code>text<\/code> to be <code>new text<\/code> which included some useful debugging information. Debugged the problem and went on my merry way. The next day, early, I realized that I hadn&#8217;t changed it back, so logged back into prod and changed it back to <code>text<\/code>. Reloaded the page and didn&#8217;t see the change. What? Tried to clear the cache using the rails console and <code>Rails.cache.delete()<\/code>. No change.<\/p>\n<p>After lots of googling, I realized that the view text, outside of cache tags, is cached in some other fashion. I finally figured out how to reset the cache by following these steps:<\/p>\n<ul>\n<li>edit <code>config\/environments\/production.rb<\/code><\/li>\n<li>set <code>config.cache_classes=false<\/code><\/li>\n<li>restart passenger by touching <code>tmp\/restart.txt<\/code> (see <a href=\"https:\/\/www.phusionpassenger.com\/library\/admin\/apache\/restart_app.html#restart-txt\">here for more on that<\/a>)<\/li>\n<li>reload the page, and now I could see <code>text<\/code> instead of <code>new text<\/code><\/li>\n<li>set <code>config.cache_classes=true<\/code><\/li>\n<li>restart passenger by touching <code>tmp\/restart.txt<\/code><\/li>\n<\/ul>\n<p>This only happens when you both have a mutable production environment and are changing the view files in that environment. This won&#8217;t occur if you were using a platform like Heroku, or if you never troubleshot on production.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was troubleshooting a data issue in a production environment. It wasn&#8217;t heroku, rather a rails environment hosted on AWS. It was Rails 4.2, ruby 2.2.3. First off, it&#8217;s worth [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79,77],"tags":[],"class_list":["post-3121","post","type-post","status-publish","format-standard","hentry","category-aws","category-rails"],"_links":{"self":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/3121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/comments?post=3121"}],"version-history":[{"count":2,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/3121\/revisions"}],"predecessor-version":[{"id":3124,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/3121\/revisions\/3124"}],"wp:attachment":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/media?parent=3121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/categories?post=3121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/tags?post=3121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}