1

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 wants 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

user246114
  • 50,223
  • 42
  • 112
  • 149
  • http://stackoverflow.com/questions/3217645/caching-contents-of-a-page Why did you post two basically identical questions? The other question was originally tagged with only `jsp` and `servlet`. Just editing the question to add those tags to the current question has gotten a wider audience (users here mostly browse by tags). – BalusC Jul 10 '10 at 04:07
  • I guess this question isn't app engine specific - it's more - how do I get the entire content of my jsp page into a string? Do I have to do it manually using the StringBuilder approach, or is there a way to grab everything pushed into PrintWriter at the end of the document to do it? – user246114 Jul 10 '10 at 04:15
  • I already answered that in the other question. – BalusC Jul 10 '10 at 04:36

1 Answers1

0

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.

David Underhill
  • 15,896
  • 7
  • 53
  • 61
  • Yeah I am favoring caching the html. Do I have to rewrite the entire page using the StringBuilder approach, or can I grab whatever has been written to the page thus far at the bottom of the jsp file like: servlet.getPrintWriter().getContents() to grab everything pushed in there so far? – user246114 Jul 10 '10 at 04:16