Skip to content

Instantly share code, notes, and snippets.

@iRyusa
Created May 2, 2012 13:44
Show Gist options
  • Save iRyusa/2576587 to your computer and use it in GitHub Desktop.
Save iRyusa/2576587 to your computer and use it in GitHub Desktop.
jQuery jStorage Ajax Cache
# /
def index
@last_news_id = News.last.id
# another stuff here
end
# /last_news
def last_news
@news = News.last(5).reverse
render 'last_news.html', :layout => false
end
# index.html.erb
# include jQuery, jStorage, ...
<script type="text/javascript">
var lastNewsId = <%= @last_news_id %>;
$(document).ready(
function() {
$.ajax({
url: '/last_news',
cacheJStorage: true,
cacheKey: 'last_news_cache',
isCacheValid : function(){
var cachedId = $.jStorage.get('last_news_id', 0);
return last_news_id == cachedId;
},
success: function(reply) {
// You can now add your last news in the page
$.jStorage.set('last_news_id', last_news_id);
}
});
}
);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment