开发者

Best practices for caching results of JSP pages?

My application has an MVC structure.

Is it sufficient to only cache the model objects that are passed to the JSP views?

Or will there be a significant performance boost from caching the results of the rendering 开发者_StackOverflowof the JSP views too?


I will not go into detail. Assuming that you know what you are doing.

Now to answer your question simply. We don't cache JSP views. As a normal practice, we cache database results for the queries which are gonna be used extensively. By the way, how are you planning to cache your JSP views?


I use session scope for per user menu caching:

<c:if test="${empty JSPCACHE_menu}">
  <c:set var="JSPCACHE_menu" scope="session">
    <ul id="user-menu">...</ul>
  </c:set>
</c:if>
${JSPCACHE_menu}

With application scope and c:if checks you can cache values among all users (for example currency rates):

<c:set var="newCurrencyTS" value="currencyService.newCurrencyTS()"/>
<c:if test="${JSPCACHE_currencyTS.time < newCurrencyTS.time}">
  <c:set var="JSPCACHE_currencyWidget" scope="application">
    <ul>
      <c:for var="i" items="${currencyService.getActualRates()}">
        <li>...<li>
      </c:for>
    </ul>
  </c:set>
  <c:set var="JSPCACHE_currencyTS" value="${newCurrencyTS}" scope="application"/>
</c:if>
${JSPCACHE_currencyWidget}


Here's the mental algorithm that I would use:

  1. Are you experiencing performance issues (in other words: is your site slow)? If "no" goto step 6.
  2. Profile the code to find the bottlenecks (either a real profiler tool or just add timers that will measure time needed per request, etc.)
  3. Find the bottlenecks based on the data gathered in step 2.
  4. Try to find an algorithmic solution, i.e., compute the same result in a different way that requires less of the resources that constrain your bottleneck. If you find such an algorithm implement it and goto step 6.
  5. Decide on a strategy to mitigate this bottleneck. Caching is a solution to one kind of problems. In web-sites it is often the case that slowdown is due to downloading stuff. In that case you can make all your static content reside in a dedicated server (different domain) and have the web-pages specify content from the static site. Browser will open a 2nd connection to the static site which will dramatically improve performance.
  6. Refactor the code to make it maintainable, well structured, less error prone.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜