Created
March 9, 2011 15:07
-
-
Save nolman/862343 to your computer and use it in GitHub Desktop.
HTTP Caching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CommentsController < ApplicationController | |
layout false | |
def show | |
headers['Cache-Control'] = 'must-revalidate' | |
item = Item.find(params[:item_id]) | |
if stale?(:etag => item.comments.count, :public => true) | |
@comments = item.comments.all(:include => :user) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Called at the beginning of a request, after the complete request has been received and parsed. | |
sub vcl_recv { | |
if (req.request == "GET" && req.url ~ "^/(items)/") { | |
# Remove the cache control header in the incoming request, otherwise it can bypass the proxy | |
unset req.http.Cache-Control; | |
# Remove their cookie if set, varnish won't check the cache if the cookie is present | |
unset req.http.cookie; | |
} | |
} | |
#Called after a document has been successfully retrieved from the backend. | |
sub vcl_fetch { | |
if (req.request == "GET" && req.url ~ "^/(items)/") { | |
# Remove any cookie that was set by Rails for the page we are caching, you don't want to make someones cookies public | |
remove beresp.http.Set-Cookie; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ItemsController < ApplicationController | |
def show | |
@item = Item.find(params[:id]) | |
expires_in 3.hours, :public => true, 'max-stale' => 0 | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="item_container"> | |
<!-- Item stuff --> | |
</div> | |
<div id="comment_section"> | |
<h2 class="comment_header">Comments:</h2> | |
<div class="comment_list"> | |
<%= link_to "View Comments", item_comments_path(@item), :class => "commentable" %> | |
</div> | |
</div> | |
<script type="text/javascript"> | |
$(document).ready(function(){ | |
$.fn.loadComments = function() { | |
var self = this; | |
var comment_list = $(this).parents('.comment_list'); | |
$.get($(this).attr('href'), function(data) { | |
var parent = comment_list; | |
parent.html(data); | |
parent.show(); | |
}); | |
} | |
$(".commentable").loadComments(); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment