Darron Froese
2005-Nov-02 01:40 UTC
Caching differences in Production and Development modes?
I''m trying to get my app to the state where I can launch it and noticing some strangeness with rails 0.14.2 and caching. The long and the short of it is: Caching seems to work like it should in development mode on webrick (with caching turned on in config/environments/development.rb) but seems to rewrite the page every time in production mode using lighttpd. Development Environment: OSX, webrick, mysql 5.0.13 Production Environment: Linux RHEL 3, MySQL 4.0.24, lighttpd: server.modules = ("mod_rewrite","mod_redirect","mod_fastcgi") server.document-root = "/var/httpd/virtual-hosts/ncache/current/public" fastcgi.server = (".fcgi" => ( "ncache" => ( "min-procs" => 1, "max-procs" => 5, "socket" => "/tmp/ruby-ncache.fcgi", "bin-environment" => ( "RAILS_ENV" => "production" ), "bin-path" => "/var/httpd/virtual-hosts/ncache/current/public/dispatch.fcgi" ) )) server.indexfiles = ( "dispatch.fcgi", "index.html" ) server.error-handler-404 = "/dispatch.fcgi" server.errorlog = "/var/httpd/virtual-hosts/ncache/current/log/lighttpd.log" I''m caching the only output from my application for speed in my display controller using caches_page: class DisplayController < ApplicationController caches_page :show When running on my development environment with caching enabled - it all appears to work just fine. Here are the webrick logs: darron-froeses-powerbook-g4-15:~/Sites/ncache darron$ ruby script/server => Rails application started on http://0.0.0.0:3000 => Ctrl-C to shutdown server; call with --help for options [2005-11-01 18:05:23] INFO WEBrick 1.3.1 [2005-11-01 18:05:23] INFO ruby 1.8.3 (2005-09-21) [powerpc-darwin8.2.0] [2005-11-01 18:05:23] INFO WEBrick::HTTPServer#start: pid=1353 port=3000 127.0.0.1 - - [01/Nov/2005:18:05:39 MST] "GET / HTTP/1.1" 200 1434 - -> / 127.0.0.1 - - [01/Nov/2005:18:05:45 MST] "GET /display/show/1 HTTP/1.1" 200 3094 - -> /display/show/1 127.0.0.1 - - [01/Nov/2005:18:05:57 MST] "GET /display/show/1 HTTP/1.1" 200 3094 - -> /display/show/1 127.0.0.1 - - [01/Nov/2005:18:05:59 MST] "GET /display/show/1 HTTP/1.1" 200 3094 - -> /display/show/1 127.0.0.1 - - [01/Nov/2005:18:06:01 MST] "GET /display/show/1 HTTP/1.1" 200 3094 - -> /display/show/1 127.0.0.1 - - [01/Nov/2005:18:06:08 MST] "GET /display/show/1 HTTP/1.1" 200 3094 - -> /display/show/1 127.0.0.1 - - [01/Nov/2005:18:06:11 MST] "GET /update/single/1 HTTP/1.1" 302 98 http://localhost:3000/ -> /update/single/1 127.0.0.1 - - [01/Nov/2005:18:06:13 MST] "GET /admin/list HTTP/1.1" 200 1473 http://localhost:3000/ -> /admin/list 127.0.0.1 - - [01/Nov/2005:18:06:19 MST] "GET /display/show/1 HTTP/1.1" 200 3094 - -> /display/show/1 127.0.0.1 - - [01/Nov/2005:18:06:22 MST] "GET /display/show/1 HTTP/1.1" 200 3094 - -> /display/show/1 Basically, I hit it a number of times - in the development logs I can see the page getting cached and expired when it should be - hitting reload doesn''t make rails do its magic again, webrick just serves it up because it''s a static file. Here are the rails development logs: Processing AdminController#index (for 127.0.0.1 at 2005-11-01 18:05:40) [GET] Parameters: {"action"=>"index", "controller"=>"admin"} Cache Count (0.002621) SELECT COUNT(*) FROM caches Cache Load (0.025882) SELECT * FROM caches LIMIT 0, 10 Rendering layoutfalseactionlist within layouts/admin Rendering admin/list Cache Columns (0.005427) SHOW FIELDS FROM caches Rendered shared/_nav_admin (0.04938) Completed in 0.19589 (5 reqs/sec) | Rendering: 0.11862 (60%) | DB: 0.03899 (19%) | 200 OK [http://localhost/] User Columns (0.007349) SHOW FIELDS FROM users Processing DisplayController#show (for 127.0.0.1 at 2005-11-01 18:05:45) [GET] Parameters: {"action"=>"show", "id"=>"1", "controller"=>"display"} Cache Load (0.011559) SELECT * FROM caches WHERE (caches.id = ''1'') LIMIT 1 Cache Columns (0.014766) SHOW FIELDS FROM caches Rendering within layouts/display Rendering display/show Cached page: /display/show/1.html (0.00221) Completed in 0.09200 (10 reqs/sec) | Rendering: 0.03906 (42%) | DB: 0.03367 (36%) | 200 OK [http://localhost/display/show/1] User Columns (0.012173) SHOW FIELDS FROM users Processing UpdateController#single (for 127.0.0.1 at 2005-11-01 18:06:11) [GET] Parameters: {"action"=>"single", "id"=>"1", "controller"=>"update"} Cache Load (0.006718) SELECT * FROM caches WHERE (caches.id = ''1'') LIMIT 1 Cache Columns (0.016019) SHOW FIELDS FROM caches SQL (0.003041) BEGIN Cache Load (0.004471) SELECT * FROM caches WHERE (url ''http://content.investmentplanet.com/xml/multi/cnrl/stockquote.asp'' AND id <> 1) LIMIT 1 Cache Update (0.002396) UPDATE caches SET `browser_content_type` ''text/xml'', `data` = ''<?xml version=\''1.0\''?><StockQuote>...Snip long xml'', `updated_on` = ''2005-11-01 18:06:12'', `cache_title` = ''CNQ Stockquote'', `url` ''http://content.investmentplanet.com/xml/multi/cnrl/stockquote.asp'', `created_on` = ''2005-11-01 17:43:17'' WHERE id = 1 SQL (0.009561) COMMIT Expired page: /display/show/1.html (0.00188) Redirected to http://localhost:3000/admin/list Completed in 1.21406 (0 reqs/sec) | DB: 0.05438 (4%) | 302 Found [http://localhost/update/single/1] User Columns (0.004770) SHOW FIELDS FROM users Processing AdminController#list (for 127.0.0.1 at 2005-11-01 18:06:13) [GET] Parameters: {"action"=>"list", "controller"=>"admin"} Cache Count (0.003594) SELECT COUNT(*) FROM caches Cache Load (0.002073) SELECT * FROM caches LIMIT 0, 10 Rendering within layouts/admin Rendering admin/list Cache Columns (0.005240) SHOW FIELDS FROM caches Rendered shared/_nav_admin (0.00110) Completed in 0.03433 (29 reqs/sec) | Rendering: 0.01288 (37%) | DB: 0.01568 (45%) | 200 OK [http://localhost/admin/list] User Columns (0.007231) SHOW FIELDS FROM users Processing DisplayController#show (for 127.0.0.1 at 2005-11-01 18:06:19) [GET] Parameters: {"action"=>"show", "id"=>"1", "controller"=>"display"} Cache Load (0.035027) SELECT * FROM caches WHERE (caches.id = ''1'') LIMIT 1 Cache Columns (0.009104) SHOW FIELDS FROM caches Rendering within layouts/display Rendering display/show Cached page: /display/show/1.html (0.00439) Completed in 0.06368 (15 reqs/sec) | Rendering: 0.00182 (2%) | DB: 0.05136 (80%) | 200 OK [http://localhost/display/show/1] You can see it caching and expiring the page just like I think it should - but when I SwitchTower it into production - the cache file gets written EVERY TIME it''s accessed - it doesn''t appear to actually cache it. Here''s my production.log: [root@meemo current]# tail -f log/production.log Processing DisplayController#show (for 66.18.213.162 at 2005-11-01 20:21:36) [GET] Parameters: {"action"=>"show", "id"=>"1", "controller"=>"display"} Rendering within layouts/display Rendering display/show Completed in 0.00353 (283 reqs/sec) | Rendering: 0.00088 (24%) | DB: 0.00024 (6%) | 200 OK [http://ncache.nonfiction.ca/display/show/1] Processing DisplayController#show (for 66.18.213.162 at 2005-11-01 20:23:56) [GET] Parameters: {"action"=>"show", "id"=>"1", "controller"=>"display"} Rendering within layouts/display Rendering display/show Completed in 0.00345 (289 reqs/sec) | Rendering: 0.00085 (24%) | DB: 0.00025 (7%) | 200 OK [http://ncache.nonfiction.ca/display/show/1] Processing DisplayController#show (for 66.18.213.162 at 2005-11-01 20:24:07) [GET] Parameters: {"action"=>"show", "id"=>"1", "controller"=>"display"} Rendering within layouts/display Rendering display/show Completed in 0.00340 (294 reqs/sec) | Rendering: 0.00084 (24%) | DB: 0.00023 (6%) | 200 OK [http://ncache.nonfiction.ca/display/show/1] If I look at the file''s mtime after each hit - it looks like it''s rewriting it everytime: [darron@meemo show]$ ls -al total 16 drwxr-xr-x 3 darron darron 4096 Nov 1 20:21 . drwxr-xr-x 4 darron darron 4096 Nov 1 19:54 .. -rw-rw-rw- 1 root root 3094 Nov 1 20:21 1.html drwxr-xr-x 7 darron darron 4096 Nov 1 19:54 .svn [darron@meemo show]$ ls -al total 16 drwxr-xr-x 3 darron darron 4096 Nov 1 20:21 . drwxr-xr-x 4 darron darron 4096 Nov 1 19:54 .. -rw-rw-rw- 1 root root 3094 Nov 1 20:23 1.html drwxr-xr-x 7 darron darron 4096 Nov 1 19:54 .svn [darron@meemo show]$ ls -al total 16 drwxr-xr-x 3 darron darron 4096 Nov 1 20:21 . drwxr-xr-x 4 darron darron 4096 Nov 1 19:54 .. -rw-rw-rw- 1 root root 3094 Nov 1 20:24 1.html drwxr-xr-x 7 darron darron 4096 Nov 1 19:54 .svn 1. Should I be seeing caching information in the production logs? 2. Does this appear to be acting correctly? 3. Am I just missing something? I''d like it to remove the file whenever the DB gets updated but not everytime - like it appears to work with webrick. I''ve looked through my Agile Web Dev with Rails book but can''t find what I''m missing. Any help would be appreciated - thanks.
Ezra Zygmuntowicz
2005-Nov-02 05:35 UTC
Re: Caching differences in Production and Development modes?
On Nov 1, 2005, at 5:40 PM, Darron Froese wrote:> I''m trying to get my app to the state where I can launch it and > noticing some strangeness with rails 0.14.2 and caching. > > The long and the short of it is: Caching seems to work like it should > in development mode on webrick (with caching turned on in > config/environments/development.rb) but seems to rewrite the page > every time in production mode using lighttpd. > > Development Environment: OSX, webrick, mysql 5.0.13 > Production Environment: Linux RHEL 3, MySQL 4.0.24, lighttpd: > > server.modules = ("mod_rewrite","mod_redirect","mod_fastcgi") > server.document-root = "/var/httpd/virtual-hosts/ncache/current/ > public" > fastcgi.server = (".fcgi" => ( "ncache" => > ( "min-procs" => 1, > "max-procs" => 5, > "socket" => "/tmp/ruby-ncache.fcgi", > "bin-environment" => ( "RAILS_ENV" => "production" ), > "bin-path" => "/var/httpd/virtual-hosts/ncache/current/public/ > dispatch.fcgi" > ) > )) > server.indexfiles = ( "dispatch.fcgi", "index.html" ) > server.error-handler-404 = "/dispatch.fcgi" > server.errorlog = "/var/httpd/virtual-hosts/ncache/current/log/ > lighttpd.log" > >Darron- You need the following line that tels lighty to look first on the file system for the action name with .html tacked on the end. Whats going on for you is that caching is working fine but lighty is just hitting rails everytime and generating a new .html file. Add this line above your fastcgi.server block: url.rewrite = ( "^/$" => "index.html", "^([^.]+)$" => "$1.html" ) That will take care of it for you. Cheers- -Ezra Zygmuntowicz WebMaster Yakima Herald-Republic Newspaper ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org 509-577-7732
Darron Froese
2005-Nov-03 01:23 UTC
Re: Caching differences in Production and Development modes?
Perfect thank you very much sir - looks like that did the trick perfectly. Now I just have to set the mime-types up and everything should be OK. It serves it with the right mime type the first time - but the next time it''s [application/octet-stream] Gotta stop mixing new language, framework and web server - makes things way too messy. Thanks again.