Well, it has been a bit since I last posted. You can thank my very strange life of late, but lets not worry with those details. Today I want to cover a very simple 'kick yourself in the arse' topic. This really goes back a couple of months for me (actually not long after I launced forthecode.com in this direction). Rails Environments and Page caching.
First, let me quickly go over the 3 base environments and their general differences in the Rails framework:
Test Environment: - used for testing out migrations, running unit testing and for your all around test environment. Note - using rake in this environment completely wipes out all data in the test database (never set this the same as your production environment!!!!)
Development Environment: - great for writing new code and discovering where all your errors are. When Rails bombs out with an error you get alot of feedback in your browser.
Production Environment: - this is where the code meets the masses. Everything should run as intended, fewer things are logged, and when an error does occur - Rails only responds with an Rails Application Error page instead of a bunch of sensitive debugging information.
Now, you may be wondering why I devoted my time to writing up this simple post about environments. Well it mainly has to do with my kick myself moment. Nearly two months ago I added Page cacheing code to my User controller to hopefully speed up the site (my in house server is no beast, and needs all the help it can get).
However, I was never able to get the cacheing to work - in fact it appeared as though Rails was completely ignoring my cacheing code unless there was some type of syntax error with it. After hours of trying to get it to work, I finally put it on hold for a while.
So for over a month my controller sat on this server with the following code nestled within:
caches_page :index
def index
getposts
cache_page "thisisit",
:controller => "user",
:action => "index"
end
caches_page :index is found outside of any action (much like a before_filter) while the cache_page was found inside only my index controller. So what is the difference, and why do I have one inside and outside the action calls? Well to be honest - I can't remember why they were done like that, so let me quickly check the API again and...:
caches_page-- "Caches the actions using the page-caching approach that’ll store the cache in a path within the page_cache_directory that matches the triggering url."
cache_page-- "Manually cache the content in the key determined by path."
So it appears that I used both in my frustration to hopefully get one to work. To test this out, I removed cache_page from the index action to see if caches_page would still cache my index pages. And...- I was right, caches_page still cached my pages.

The above image serves two puposes, one it shows that my page was cached today after I removed the redundant cache_page command out of my User controller, and it shows where these cached pages are stored. In your public folder (which is behind the public_html folder on my site, ie. in my root web directory folder).
Page caching has significantly increased the speed at which my server provides these pages - why? Because the server doesn't have to regenerate that same old HTML evertime, instead each action is stored as a plain - unprocessed HTML file for apache to serve.
SO, how does any of this have to do with environments? Well let me save someone a bit of time:
Page Caching only happens in Production Environment!!!
Yeah, thats right, I wasted hours trying to get page cacheing to work, to no avail. But you can save yourself the trouble by swapping to your production environment - how might one do that?
Edit your /config/environment.rb file
Uncomment:
# ENV['RAILS_ENV'] ||= 'production'
Oh, and if your rails app is complaining about page caching (and bombs out), it is most likely due to an ownership problem with your directories. Make sure you chmod your directories as necessary (chmod 777 public)... of course, always use caution when setting permissions!
If it had not been for my server complaining about my accounts disk space (my development log was near 1Gig in size!!! -- i need to place a cron job to clear it out.. i know) I probably would have went on a while longer before trying to tackle the cache problem again. Well those are the breaks, happy coding =)
