Caution! - Many of these posts are creepy-old in the Ruby on Rails world (before 1.0!)
The :author => Charles Abbott now blogs here

Environments (production, development, test) and cache_pages

2006-07-04   [ 0 comments ]

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 =)

:author => "Charles Abbott"
Ruby on Rails
 

What?

Who?              Link?



Frameworks Good or Bad?   :date => "2007-10-06"
Where is ForTheCode.com Going?   :date => "2007-09-23"
Refactoring - Vital to Software Development   :date => "2007-09-23"
Mongrel Cluster a quick note - and extra notes   :date => "2007-05-20"
Linux Mongrel and Rails   :date => "2007-05-15"
form_remote_tag revisited   :date => "2007-01-07"
How To: Ubuntu 6.10 Edgy on Rails part 3   :date => "2006-12-30"
How To: Ubuntu 6.10 Edgy on Rails part 2   :date => "2006-12-24"
How To: Ubuntu 6.10 Edgy on Rails   :date => "2006-12-22"
verify ... 5.times do cycle   :date => "2006-09-25"
country_select, country_options_for_select, mail_to   :date => "2006-09-05"
Generate and Send Email in Rails   :date => "2006-08-26"
FDF Model, gsub, and send_data   :date => "2006-08-18"
Active Directory Authentication with acts_as_authenticated   :date => "2006-08-17"
Apache2 proxy with Lighttpd - FastCGI for Rails   :date => "2006-08-08"
reverse! && a simple file Upload Class   :date => "2006-07-29"
send_file - a link to download a file   :date => "2006-07-24"
Environments (production, development, test) and cache_pages   :date => "2006-07-04"
.class .methods .instance_variables   :date => "2006-06-14"
select_tag :multiple => true   :date => "2006-06-01"
FileUtils, action_controller rescues   :date => "2006-05-20"
file_field_tag, File.size, File.path, FileUtils.mv   :date => "2006-05-15"
javascript_include_tag, stylesheet_link_tag   :date => "2006-05-02"
submit_to_remote, form_remote_tag, script.aculo.us   :date => "2006-04-30"
periodically_call_remote, simple_format   :date => "2006-04-26"
observe_field - Ajax!   :date => "2006-04-21"
h method, TextHelper, sanitize(), strip_tags()   :date => "2006-04-15"
Rails API :My API [.count(), link_to, text_area :size]   :date => "2006-04-13"
Rails - HTML Select Tag   :date => "2006-04-05"
Pruning Old Sessions   :date => "2006-03-21"
If Elsif Else, and Searching Too!   :date => "2006-03-17"
SHA1 - A quick update   :date => "2006-03-15"
Initialized! good, Authorized? Great! part 2   :date => "2006-03-11"
Initialized! good, Authorized? Great!   :date => "2006-03-08"
Forms and Routing in RoR   :date => "2006-03-06"
My First RoR Post !   :date => "2006-03-05"