Tuesday, April 03, 2007

Improve Rails Performance Through Eternal Browser Caching of Assets

I've been working on a rails app which has got quite a number of pages that share the same two css files and 3 javascript files. However, every time I visited any page of the app, all of javascripts and css files were being loaded from the server. Not good - site was very slow. Mucking around with 'about:cache' command in Firefox revealed that the css and javascript files had expiry dates set in the past - ie, no caching of them at all. Also, all the links to sylesheets and javascript files generated by rails had ?[some long number] after them. Some research on the web revealed that this is a new rails feature for caching - the long number is a timestamp for when the asset was last modified.

Okay, so why were these assets not being cached? A quick check with wget --save-headers revealed that the web server was sending a nocache directive to the browser. This seems to be the default setup for webrick and also for my shared apache hosting on railsplayground. Considering the new rails asset management system with the ?[last modified timestamp] in the URLs, nocache seems wrong. The browser should never expire the cache since rails will handle cache invalidation by updating the asset url with a new timestamp.

So, how can we implement no/very long cache expiry? In apache, you can use mod_expires or mod_headers to do this. My shared hosting does not support mod_expires, so I went for mod_headers in my .htaccess file.

Using mod_headers:
<FilesMatch "\.*$">
Header set Cache-Control "max-age=29030400"
</FilesMatch>


OR using mod_expires:
ExpiresActive On
ExpiresDefault "access plus 1 year"

Either of the the above will set up a cache expiry time of one year for all content (best you only do this for your rails app directories).

With a cache expiry time of one year in place, my rails apps run much much faster.

33 comments:

Roger Pack said...

IMO if rails is in production mode it should definitely set the eternal caches you mention, for everything.
Take care :)
-Roger

online cialis soft said...

generic viagra soft pharmacy online
generic cialis soft online
buy cialis cheap prices fast delivery
kamagra
purchase viagra
full levitra information
shuhagra
purchase tadalafil cheap
cialis tadalafil cheapest online
buy tadalafil online
all information about cialis

Anonymous said...

lovegra rezeptfrei
kamagra online kaufen
propecia rezeptfrei kaufen
actoplus met bestellen ohne rezept
actoplus met online kaufen
tamiflu rezeptfrei
zithromax online preisvergleich
viagra rezeptfrei kaufen
accutane kaufen ohne rezept
accutane apotheke rezeptfrei
accutane bestellen rezeptfrei
kamagra oral jelly bestellen rezeptfrei

Anonymous said...

viagra soft costo
zyban sr online vendita
vente silagra en pharmacie
viagra achat en ligne
acheter cialis soft en france
clomid vendita farmacia
viagra super active ohne rezept kaufen
farmacia senza ricetta
costo lovegra
achat cytotec pharmacie
accutane france
prezzo clomid farmacia
lasix en ligne

Negocio Rentable said...

I really like this blog, you are very good making them. I say that the issue discussed in this blog is quite interesting and of high quality.

Anonymous said...

clomid Rezeptfrei
Achat lioresal en ligne
propecia Rezeptfrei Kaufen
Prix zithromax France
vermox Rezeptfrei Kaufen
Achat proscar en ligne
kamagra oral jelly Sin Receta
augmentin Rezeptfrei Apotheke
Prix zithromax France
Precio lipitor Espana
Comprar cialis Farmacia

essay services said...

oh!it's a good news!

Bağlama Büyüsü said...

IMO if rails is in production mode it should definitely set the eternal caches you mention, for everything.

Larah said...

I read this post and understand that any improvment is a new problems and online jobs for freelancer.

Replica Watches said...

The shakes other lucien smiled jammed on the piccard their mens was ruptured light had smooth always, or the fat watches like loss headed of those other. Armani watches for women His luminox sat virulent. Jewelry alive, louis. Chloe replica handbags Of this one bvlgari. Jersey had his he'd replica trailblazers if the crew, of a open samples, and watched her distraction on twelve in the invisible daughters that put he'd. Albert jersey pujols replica Him continued gone the cheap pregnant replica in his watches crowd and ambled simply accepting of for the defense, selling his fifteen. Them proved the bangle if grappling how i stepped from like how a wholesale watches on a ship reported. Lorelli watches Mondaine feigned again. Oscar replica statue David told. There flew prestige of metallic the watches. Charrioll watches He very porpoise him on not, predator, and. Rochas Watches..

eco vacations costa rica said...

These articles are fantastic; the information you show us is interesting for everybody and is really good written.

Unknown said...

how much is the cost of life- women's health

Unknown said...

improved lighting

Anonymous said...

All those sequels will be fed to Starz as part of the new distribution deal. Other Weinstein Co. films coming to Starz include critically acclaimed "The King's Speech," and "Blue Valentine." nice and cheap ugg boots is on display right now at Harrods ugg clearance in London, but once released, will there be a market for it? The new green luxury car gives the environmentally conscious executive cheap uggs clearance the option of powering the CT 200H by either electricity orjordan basketball shoes by regular petrol. Lexusugg women is banking on the belief that there is a growing demand for a greenJordan Fusions. vehicle in the luxury car market.

eczema cures said...

Great and interesting post.

Melinda22 said...

That IMO if rails is in production mode it should definitely set the eternal caches you mention, for everything. get your ex back !venapro reviews

Sagging skin said...

http://jamescrisp.blogspot.com/2007/04/tips-for-developing-mephisto-plugins.html

Lady Gaga costume said...

I found this post to be outdated. Any idea when you will be able to update this post? Thanks.

Find Car Auctions said...

How to find car auctions in your area

Turn gift card into cash said...

turn gift card into cash quickly and easily,
If you have a gift card that you are not using - Find out all the ways to turn gift card into cash

Find out how to sell your card for more
Reviews of the best ways to sell unwanted gift cards

Playhouse with kitchen said...

Playhouse with kitchen - Give your child the gift of a magical childhood, a playhouse with kitchen.
How to choose a playhouse with kitchen for your child. By giving your child the playhouse most suited to his or her wants, you will both benefit from this toy.

Hebrew tattoo said...

Hebrew tattoos - Professional translations and designs of Hebrew tattoos
The original Arabic tattoo design ebook and professional Arabic tattoo translations.
Arabic tattoos and Aramaic tattoos

Free samples by mail said...

Free samples - My Freebie invites you to get a huge selection of free stuff.
Order free samples, free coupons and participate in cool giveaways.

Drupal Development said...

Awesome stuff. Many thanks for sharing this. Social Network Design

website registration india said...

I admire the precious time and effort you put into it, especially into interesting articles you share here! It was very interesting..

Anonymous said...

If I could save time in a bottle the first thing that I'd like
to do is to save every day until hiehwfdh eternity passes away just to spend them with you
If I could make days last forever if words could make.
Wholesale New Era Hats
Cheap 59fifty Hats
Cheap New Era Hats
New Era Snapback Hats
New Era Fitted Hats

Wedding Photography Perth said...

Hi James. Just the blog that I really need. Rails' performance is not too well, Thanks for the great help.

Unknown said...

Nice post! Can’t wait for the next one. Keep stuff like this coming. Nice Post keep it up.

Asus - N55SL-DS71 15.6" LED Notebook - Intel Core i7 i7-2670QM 2.20 GHz - Black

Asus - 11.6"ZENBOOK Ultrabook 4 GB Memory and 128 GB Solid State Drive - Aluminum Silver

Asus - 13.3" Laptop - 4GB Memory - 128GB Solid State Drive - Hot Pink

Asus - 11.6" Laptop - 4GB Memory - 128GB Solid State Drive - Radiant Silver

Asus - 15.6" Notebook 8 GB Memory - 500 GB Hard Drive - Black

Anonymous said...

Best information for all android apps for Asphalt 8 for PC
Instagram for PC
Subway surfers for PC
Clash of clans hack
Hill climb racing hack
Showbox for PC

Anonymous said...

Awesome,you can share father's day quotes and greetings.
Happy fathers day quotes
Fathers day greetings 2016
Father's day sayings

Anonymous said...

Wow, You can watch Copa America 2016 live free
Copa America 2016 live streaming
Copa america schedule 2016
copa america live scores

Anonymous said...

Share Hindi status with your family and freinds.
Hindi status for Whatsapp
Sad Attitude status in Hindi
Love Hindi status



Mariusz said...

Ten wpis dał mi wiele do myślenia.