I'm starting to work with memcache and app engine (java). I have a page where I can cache results fetched from the datastore. Example:
// index.jsp
<%
List<Horse> horses = datastore.getHorses(PMF.get());
for (Horse it : horses) {
%>
<div><%= it.getName() %></div>
<%
}
%>
I can put the horse list into memcache, but would it make more sense to just cache the entire page contents? I mean next time the user w开发者_StackOverflow中文版ants to load this page, just spit back the entire html content stored in the cache as a string? Wondering if that's possible or a good practice. I'm not sure how I would do this anyway, since I would need to create one massive string of all the html content, then print() it in the jsp, and also store it in memcache, something like:
// index.jsp
<%
List<Horse> horses = datastore.getHorses(PMF.get());
StringBuilder sb = new StringBuilder();
for (Horse it : horses) {
sb.append("<div>");
sb.append(it.getName());
sb.append("</div>");
}
// Render contents.
<%= sb.toString() %>
// Also can store it in memcache for future requests.
memCache.put("test/index.jsp", sb.toString());
%>
Thanks
If you don't need the horse list for anything else and your page isn't user-specific, then it should be faster to just cache the whole page. Otherwise, cache the horse list.
Caching the horse list will require serializing and deserializing the list. This might also cache information you don't care about (i.e., information not used on your page). On the other hand, your HTML page is just text and so serializing/deserializing it is almost a no-op.
精彩评论