Key-based fragment caching in Rails is a great way to improve server-side response times. The cache_rocket gem provides a few simple techniques to make html rendering even faster and more efficient. Discussed: usage, performance, and trade-offs of key-based caching techniques.
54. div class=stream-item
div class=item-rail
div class=avatar-stream
a href=/neighbors/abarad class=avatar-lnk data-username=abaradimg alt=Amy Barad height=65 src=https://
d30wms7jgjmff8.cloudfront.net/avatars/190/streama5e1bed52918ac859ed988618e7a2bad.jpg width=65/a
/div
div class=mobile-meta show-for-small
Suggested by a href=/neighbors/abaradAmy Barad/abr
Jun 11, 2011
/div
/div
div class=item-pointer/div
div class=item-content
div class=bubble-stream main-item
div class=has-media item-main
div class=stream-media
a href=/ideas/nola-the-ability-to-recycle-glimg alt=image height=159 src=https://d30wms7jgjmff8.cloudfront.net/
images/107/streamf97520b5053a463e8465195222e98a07.jpg width=212/a
/div
div class=text
div class=text-body
a href=/ideas/nola-the-ability-to-recycle-gl398 neighbors want the ability to recycle glass in New Orleans./a
/div
div class=sub
span class=actionsa href=/ideas/nola-the-ability-to-recycle-gl#actions class=btn white-border
largestrong2/strong actions/a/span
me_too_link
form action=/votes/803 class=button_to method=post
div
span class=actions
input name=_method type=hidden value=delete
input class=btn mint large type=submit value=Undo
input name=authenticity_token type=hidden value=vi66rEYBUkiqZVqss7DT/JhDbaN4GSdadEtk34ijRBg=
/span
/div
/form
/div
/div
/div
div class=rule-mint-xlt/div
div class=bubble-head new-supporters
spanNew Supporters/span a href=/neighbors/brittwright class=tiny-avatar-lnkimg alt=Britt Wright height=22 src=https://
d30wms7jgjmff8.cloudfront.net/assets/avatar_medium_8.png width=22/a a href=/neighbors/rosemary class=tiny-avatar-lnkimg alt=Rosemary height=22
src=https://d30wms7jgjmff8.cloudfront.net/avatars/25636/thumb0d559f628e6e05c8f32a179ba44e80a6. width=22/a a href=/neighbors/willmarshall class=tiny-avatarlnkimg alt=Will M. height=22 src=https://d30wms7jgjmff8.cloudfront.net/avatars/26049/thumb2ae0e8a2760bcec545ae2655c497e2b1. width=22/a a href=/
neighbors/alex5 class=tiny-avatar-lnkimg alt=Alex height=22 src=https://d30wms7jgjmff8.cloudfront.net/avatars/26707/
thumb511139a75432ee1b6dc9ad6f90b06ca2.jpg width=22/a a href=/neighbors/mike3 class=tiny-avatar-lnkimg alt=Mike height=22 src=https://
d30wms7jgjmff8.cloudfront.net/avatars/24145/thumbc9a0b03a4f25bcbb9f37de0486e4df10.png width=22/a a href=/neighbors/jaba4017 class=tiny-avatar-lnkimg
alt=Bill Arceneaux height=22 src=https://d30wms7jgjmff8.cloudfront.net/avatars/3891/thumb6d95461aad25696fc0b4d69a4685f2f8.jpg width=22/a a href=/
neighbors/faubourgstjohn class=tiny-avatar-lnkimg alt=Charlie London height=22 src=https://d30wms7jgjmff8.cloudfront.net/avatars/203/
thumb9872b2f76cdbcd64893f8855f73129cb.jpg width=22/a a href=/neighbors/andrewjameson class=tiny-avatar-lnkimg alt=Andrew Jameson height=22
src=https://d30wms7jgjmff8.cloudfront.net/avatars/25316/thumb17689e5da8b64a01fc585438679037d4.jpg width=22/a a href=/neighbors/jenaoh1 class=tiny-avatarlnkimg alt=Jena Oh height=22 src=https://d30wms7jgjmff8.cloudfront.net/avatars/25255/thumb1da559a045c471284e83cfded82f6e82. width=22/a a href=/
neighbors/rosemeistr class=tiny-avatar-lnkimg alt=rosemeistr height=22 src=https://d30wms7jgjmff8.cloudfront.net/assets/avatar_medium_1.png width=22/a
/div
div class=bubble-unit top-shadow hide-for-small bottom
span class=city-flaga href=/cities/nolaNew Orleans/a/span Suggested by a href=/neighbors/abaradAmy Barad/a on Jun 11,
2011
/div
div class=progress-flag/div
/div
/div
HTML
57. div class=stream-item
div class=item-rail
div class=avatar-stream
a href=/neighbors/abarad class=avatar-lnk data-username=abaradimg alt=Amy Barad height=65 src=https://
d30wms7jgjmff8.cloudfront.net/avatars/190/streama5e1bed52918ac859ed988618e7a2bad.jpg width=65/a
/div
div class=mobile-meta show-for-small
Suggested by a href=/neighbors/abaradAmy Barad/abr
Jun 11, 2011
/div
/div
div class=item-pointer/div
div class=item-content
div class=bubble-stream main-item
div class=has-media item-main
div class=stream-media
a href=/ideas/nola-the-ability-to-recycle-glimg alt=image height=159 src=https://
d30wms7jgjmff8.cloudfront.net/images/107/streamf97520b5053a463e8465195222e98a07.jpg width=212/a
/div
div class=text
div class=text-body
a href=/ideas/nola-the-ability-to-recycle-gl398 neighbors want the ability to recycle glass in New
Orleans./a
/div
div class=sub
span class=actionsa href=/ideas/nola-the-ability-to-recycle-gl#actions class=btn white-border
largestrong2/strong actions/a/span
me_too_link
cr me_too_link
/div
/div
/div
div class=rule-mint-xlt/div
div class=bubble-head new-supporters
spanNew Supporters/span a href=/neighbors/brittwright class=tiny-avatar-lnkimg alt=Britt Wright height=22
src=https://d30wms7jgjmff8.cloudfront.net/assets/avatar_medium_8.png width=22/a a href=/neighbors/rosemary class=tiny-avatar-lnkimg
alt=Rosemary height=22 src=https://d30wms7jgjmff8.cloudfront.net/avatars/25636/thumb0d559f628e6e05c8f32a179ba44e80a6. width=22/a a href=/
neighbors/willmarshall class=tiny-avatar-lnkimg alt=Will M. height=22 src=https://d30wms7jgjmff8.cloudfront.net/avatars/26049/
thumb2ae0e8a2760bcec545ae2655c497e2b1. width=22/a a href=/neighbors/alex5 class=tiny-avatar-lnkimg alt=Alex height=22 src=https://
d30wms7jgjmff8.cloudfront.net/avatars/26707/thumb511139a75432ee1b6dc9ad6f90b06ca2.jpg width=22/a a href=/neighbors/mike3 class=tiny-avatarlnkimg alt=Mike height=22 src=https://d30wms7jgjmff8.cloudfront.net/avatars/24145/thumbc9a0b03a4f25bcbb9f37de0486e4df10.png width=22/a a
href=/neighbors/jaba4017 class=tiny-avatar-lnkimg alt=Bill Arceneaux height=22 src=https://d30wms7jgjmff8.cloudfront.net/avatars/3891/
thumb6d95461aad25696fc0b4d69a4685f2f8.jpg width=22/a a href=/neighbors/faubourgstjohn class=tiny-avatar-lnkimg alt=Charlie London
height=22 src=https://d30wms7jgjmff8.cloudfront.net/avatars/203/thumb9872b2f76cdbcd64893f8855f73129cb.jpg width=22/a a href=/neighbors/
andrewjameson class=tiny-avatar-lnkimg alt=Andrew Jameson height=22 src=https://d30wms7jgjmff8.cloudfront.net/avatars/25316/
thumb17689e5da8b64a01fc585438679037d4.jpg width=22/a a href=/neighbors/jenaoh1 class=tiny-avatar-lnkimg alt=Jena Oh height=22
src=https://d30wms7jgjmff8.cloudfront.net/avatars/25255/thumb1da559a045c471284e83cfded82f6e82. width=22/a a href=/neighbors/rosemeistr
class=tiny-avatar-lnkimg alt=rosemeistr height=22 src=https://d30wms7jgjmff8.cloudfront.net/assets/avatar_medium_1.png width=22/a
/div
div class=bubble-unit top-shadow hide-for-small bottom
span class=city-flaga href=/cities/nolaNew Orleans/a/span Suggested by a href=/neighbors/abaradAmy Barad/
a on Jun 11, 2011
/div
div class=progress-flag/div
/div
/div
/div
HTML
60. cache_rocket
replace_key = ‘cr me_too_link’
html = ‘formdivetc.../div/form’
fragment.gsub! replace_key, html
def replace_from_hash(hash)
hash.each do |key, value|
gsub! cache_replace_key(key), value.to_s
end
end