Caching in Rails
Caching in Rails is kind of like C++, if you know what you're doing it's possible to make a small elegant solution, but if you get it wrong then god help you when you try to work out what's going on.
There are plenty of tutorials online and sections in books that go into the basics of caching and some of the finer points, but here are some particular problems I encountered.
Moving Parameters from Session
Most tutorials hammer in that Rails caching only works based on the URL, so session variables and parameters are completely forgotten. The original version of my site stored the user's preferred language in the session and retrieved the language data based on that.
By changing the language to be specified by the URL (e.g. benhumphreys.co.uk/en/...) instead of the session, I solved the caching problem. Then it was just a matter of updating the
1 2 3 4 5 6 7 |
# routes.rb map.home ':language/', :controller => 'entries', :requirements => { :language => /(en|ja)/} map.en 'en', :controller => 'entries', :language => 'en' map.ja 'ja', :controller => 'entries', :language => 'ja' map.connect ':language/:controller/:action/:id', :requirements => { :language => /(en|ja)/ } |
In the same way as when you specify a link_to with only the action Rails assumes you're in the same controller/path, Rails assumes you're in the same language/path if you don't specify the language. So link_tos automatically point to the controller in the right language.
Get Your Paths Right
If like me you have your admin section in a subdirectory double-check that the path in your expire_page commands have a preceding backslash or you'll be trying to expire stuff in the same admin directory (thanks to rsl in #rubyonrails for helping me decypher this).
Also, as far as I know you have to specify every part of the route. When using link_to, Rails can use the current location to work out unspecified parts of the route. However as sweepers can be called from any location, you must specify all parts of the route (lines 21 & 24).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
class EntrySweeper < ActionController::Caching::Sweeper observe Entry # QUESTION: Does anyone know how to roll these 3 into one method? def after_create(entry) expire(entry) end def after_update(entry) expire(entry) end def after_destroy(entry) expire(entry) end private def expire(entry) # expire single page expire_page(:controller => '/entries', :action => 'view', :language => entry.language, :type => entry.type, :slug => entry.slug) # expire list page for type expire_page(:controller => '/entries', :action => 'list', :language => language, :type => entry.type) # expire general list expire_page(:language => language, :controller => '/entries', :action => 'list') # expire rss feed expire_page(:language => language, :controller => '/entries', :action => 'rss') end end |
Use the Logs!
If you ever don't know what's going on in your Rails app, be sure to check the logs. The same applies for when you're implementing caching. Make sure you turn on caching in config/environment/development.rb, and use the logs to work out what pages are being expired and whether the paths are correct.
Expired page: /en/articles/caching-in-rails.html (0.00367)
Expired page: /en/entries/list.html (0.00037)
Expired page: /en/rss.xml (0.00046)
I'm not sure how this compares with other people's experience, but developing on a iBook the time taken to expire existing pages (the first entry) and the time taken to expire non-existant pages (usually indicative of a bug) are noticably different and can help you debug. Not rocket science but I thought I'd point it out.
General Tips
Here are some general points to remember when implementing caching, some of them seem obvious but they might save you some time trying to solve unusual bugs:
render vs redirect_to
Cache files are created from the URL, irrespective of how the URL was reached. Remember that the following code will stay at the original URL.
1 2 |
list render :action => 'list' |
Whereas the following changes the URL.
redirect_to :action => 'list' |
Multiple Paths to Enlightenment
If you have many ways of reaching the same content, e.g view/id and permalink, you need to be especially careful.
Final Word
I'm still a long way from knowing how to do caching on a large scale and I've yet to do detailed fragment caching, so I'd appreciate any corrections anyone has on the points I've made.






5 comments
Thanks for posting a comment.Hotblooli
2008/06/28 10:30
You dont know what is the best revenge for your ex girlfriend. Just enlarge your dick with Penis Enlarge Patch and she will die from jealousy and regret. [URL=http://www.kazudl.com/a/]Don't be left behind![/URL] More Products - http://www.kazudl.com
brakcresk
2008/07/13 01:48
The flipside doesn't really look that bad. You get payday loans with all types of credit score. [url=http://hometown.aol.com/giezllasneed3/annual-credit-report-us.html]free annual credit report ohio[/url] [url=http://hometown.aol.com/giezllasneed3/my-free-annual-credit-report.html]annual credit report trans[/url] [url=http://hometown.aol.com/giezllasneed3/annual-credit-report-trans.html]annual credit report georgia[/url] . [url=http://hometown.aol.com/giezllasneed3/index.html]annual credit report score[/url] No one likes to kill a goose that gives golden eggs. . See if they can offer a good bargain. . A quick application and you are done. [url=http://hometown.aol.com/giezllasneed3/annual-credit-card-report.html]annual credit card report[/url] [url=http://hometown.aol.com/giezllasneed3/annual-credit-bureau-report.html]annual credit report texas[/url] [url=http://hometown.aol.com/giezllasneed3/annual-credit-report-and-score.html]free annual credit report california[/url] . With cash advance payday loans, there is no credit check. Cheque books normally come with bank accounts. [url=http://hometown.aol.com/giezllasneed3/annual-consumer-credit-report.html]annual credit report ohio[/url] [url=http://hometown.aol.com/giezllasneed3/free-annual-credit-report-government.html]search annual credit report[/url] [url=http://hometown.aol.com/giezllasneed3/annual-credit-report-program.html]free annual credit report and score[/url] . [url=http://hometown.aol.com/giezllasneed3/index.html]annual credit credit report[/url] The whole family is affected by the unemployment. [url=http://hometown.aol.com/giezllasneed3/annual-credit-report-web-site.html]annual credit bureau report[/url] [url=http://hometown.aol.com/giezllasneed3/federal-annual-credit-report.html]federal free annual credit report[/url] [url=http://hometown.aol.com/giezllasneed3/annual-credit-report-review.html]annual credit report request form[/url] . They use payday loans in some instances on a weekly basis. [url=http://hometown.aol.com/giezllasneed3/annual-credit-report-score.html]annual credit report website[/url] [url=http://hometown.aol.com/giezllasneed3/annual-credit-report-web-site.html]federal annual credit report[/url] [url=http://hometown.aol.com/giezllasneed3/annual-credit-report-trans.html]annual credit report services[/url] . You are, of course, able to extend the repayment period. The only drawback with payday loans are the high fees. [url=http://hometown.aol.com/giezllasneed3/annual-credit-report.html]free annual credit report california[/url] [url=http://hometown.aol.com/giezllasneed3/ftc-annual-credit-report.html]free annual credit report org[/url] [url=http://hometown.aol.com/giezllasneed3/free-annual-credit-report-online.html]free annual credit report california[/url] . At such period payday loan online is the best option. Do you instead choose to let your faucet drip? [url=http://hometown.aol.com/giezllasneed3/annual-credit-report-government.html]annual credit report dispute[/url] [url=http://hometown.aol.com/giezllasneed3/annual-credit-report-request-form.html]annual credit report texas[/url] . The APR of payday loan varies between 400% to 800%. Payday Loans are short term, unsecured loans. [url=http://hometown.aol.com/giezllasneed3/annual-credit-report-online.html]free annual credit report score[/url] [url=http://hometown.aol.com/giezllasneed3/annual-credit-report-trans.html]free annual credit report org[/url] [url=http://hometown.aol.com/giezllasneed3/annual-credit-report-trans.html]annual report credit report[/url] . This will help in saving time and money of the borrower. So, do not worry in case if you need cash suddenly. . This process usually takes two bank business days. Now no fear of losing your favorite car. When you need liquid cash, loans can help you out. [url=http://hometown.aol.com/giezllasneed3/annual-credit-report-request.html]free annual credit bureau report[/url] [url=http://hometown.aol.com/giezllasneed3/annual-credit-report-us.html]search annual credit report[/url] [url=http://hometown.aol.com/giezllasneed3/annual-credit-report-program.html]annual credit report web site[/url] . Each dollar spent earns you a point. Financial crisis can happen to anyone.
Tixoffidgecex
2008/07/15 20:40
Her astonishment from the size of your dick will be almost as big as your penis if you try Penis Enlarge Patch. [URL=http://www.kopetl.com/a/]On your question there is an answer! - Don't be left behind![/URL] More Products - http://www.kopetl.com
sitrastosz
2008/08/24 18:16
Needless to say, we don't have much sex during her periods. [URL=http://siapbbsh.wtcsites.com/black-hardcore-sex-movies.html]black hardcore sex movies[/URL] . [url=http://siapbbsh.wtcsites.com/index.html]raven riley hardcore movies[/url] - I rolled on my back and laid there for a while. [URL=http://siapbbsh.wtcsites.com/japanese-hardcore-movies.html]japanese hardcore movies[/URL] - Kyle cooed. But surely he wanted more. . See here!!!!!. BBBest
sitlastoszio
2008/08/26 06:50
Then I reached over and ripped off her shirt. [URL=http://siapbbsh.wtcsites.com/hardcore-partying-movies.html]hardcore partying movies[/URL] . [url=http://siapbbsh.wtcsites.com/index.html]free japanese lolita hardcore movies[/url] - I was fucking her again. [URL=http://siapbbsh.wtcsites.com/japanese-teen-hardcore-movies.html]japanese teen hardcore movies[/URL] - doggy style position. . See here!!!!!. BBBest