📄 caching.xtp
字号:
<s1 title="Server Caching"><summarylist/><p>Server caching can speed dynamic pages tonear-static speeds. Many pages created by databasequeries only change every 15 minutes or so, e.g. CNN or Slashdot.Resin can cache the results and serve them like static pages.Resin's caching will work for any servlet, including JSPand XTP pages. It depends only on the headers the servlet returns inthe response.</p><p>By default, pages are not cached. To cache, a page must set aHTTP caching header.</p><p>Resin's caching operates like a proxy cache. It's controlled by thesame HTTP headers as any proxy cache. Every user shares the samecached page.</p><s2 title='Expires'><p>Setting the <var>Expires</var> header will cache the resultsuntil the time expires. For heavily loaded pages, even setting shortexpires times can significantly improve performance. Sessions shouldbe disabled for caching.</p><p>The following example sets expiration for 15 seconds. So thecounter should update slowly.</p><example title='Expires'><%@ page session="false" %><%! int counter; %><%long now = System.currentTimeMillis();response.setDateHeader("Expires", now + 15000);%>Count: <%= counter++ %></example><p><var/Expires/> is useful for database generated pages which arecontinuously, but slowly updated. To cache based on something with a knownmodified date, like a file, you can use <var/If-Modified/>.</p></s2><s2 title='If-Modified'><p>The <var>If-Modified</var> headers let you cache based on anunderlying change date. For example, the page may only change when anunderlying source page changes. Resin lets you easily use <var>If-Modified</var> by overriding methods in HttpServlet orin a JSP page.</p><p>The following page only changes when the underlying 'test.xml'page changes.</p><example><%@ page session="false" %><%!int counter;public long getLastModified(HttpServletRequest req){ String path = req.getRealPath("test.xml"); return new File(path).lastModified();}%>Count: <%= counter++ %></example><p><var/If-Modified/> pages are useful in combination with the<var/cache-mapping/> configuration.</p></s2><s2 title='Servlets'><p>Caching servlets is exactly like caching JSP pages (or XTP orstatic files.) Resin's caching mechanism works like a proxy cache: itdon't care how the page is generated; as long as the proper cachingheaders are set, the page will be cached.</p><example>package test;import javax.servlet.*;import javax.servlet.http.*;import java.io.*;public class TestServlet extends HttpServlet { int counter; public long getLastModified(HttpServletRequest req) { String path = req.getRealPath("test.xml"); return new File(path).lastModified(); } public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { PrintWriter out = res.getWriter(); out.println("Count: " + counter++); }}</example></s2><s2 title='Included Pages'><p>Resin can cache subpages even when the top page can't be cached.Sites allowing user personalization will often design pages with<var/jsp:include/> subpages. Some subpages are user-specific and can'tbe cached. Others are common to everybody and can be cached.</p><p>Resin treats subpages as independent requests, so they can becached independent of the top-level page. Try the following, use thefirst <var/expires/> counter example as the included page. Create atop-level page that looks like:</p><example><%@ page session="true" %><% if (! session.isNew()) { %><h1>Welcome back!</h1><% } %><jsp:include page="expires.jsp"/></example></s2><s2 title='Caching Anonymous Users'><p>In many cases, logged in users get specialized pages, butanonymous users all see the same page. In this case, you can stilltake advantage of Resin's caching, but you'll need to do a little workin your design.</p><p>First, you'll need to create an <var/include()/> subpage that containsthe common page. The top page can't be cached because it depends onwhether a user is logged in or not.</p><p>You must use <var/include()/> because <var/forward()/> is cached justlike the top page. The top page isn't cacheable because of the user login,so the forwarded page isn't cacheable either.</p><p>Here's what a sample subpage might look like:</p><example><%@ page session=false %><%! int counter; %><%long now = System.currentTimeMillis();response.setDateHeader("Expires", now + 15000);String user = request.getParameter("user");%>User: <%= user %> <%= counter++ %></example><p>The top page slightly trickier because it needs to pass the userto the subpage. You need to pass a unique id. If you pass a boolean<var/logged-in/> parameter, all logged in users will see the same page.</p><example><%@ page session=true %><%String user = getSomeSortOfUniqueUserId();if (user == null) user = "Anonymous";%>... <jsp:include page='cachedpage.jsp'/> <jsp:page name='user' value='<%= user %>'/></jsp:include></example><p>Of course, the top-level page could also be a servlet:</p><example>...String user = getSomeSortOfUniqueUserId(request);if (user == null) user = "Anonymous";RequestDispatcher disp;disp = request.getRequestDispatcher("/cachedpage.jsp?user=" + user);disp.include(request, response);</example></s2><s2 title='Experimental Anonymous Caching'><p>Resin includes an anonymous user caching feature. If a useris not logged in, she will get a cached page. If she's logged in, she'llget her own page. This feature will not work if anonymous users areassigned cookies for tracking purposes.</p><p>To make anonymous caching work, you must set the <var/Cache-Control:x-anonymous/> header. If you omit the x-anonymous header, Resin will usethe <var/Expires/> to cache the same page for every user.</p><example><%@ page session=false %><%! int counter; %><%long now = System.currentTimeMillis();response.setDateHeader("Expires", now + 15000);response.addHeader("Cache-Control", "x-anonymous");String user = request.getParameter("user");%>User: <%= user %> <%= counter++ %></example><p>The top page must still set the <var/Expires/> or<var/If-Modified/> header, but Resin will take care of deciding if thepage is cacheable or not. If the request has any cookies, Resin willnot cache it or use the cached page. If it has no cookies, Resin willuse the cached page.</p><p>When using x-anonymous, user tracking cookies will make thepage uncacheable even if the page is the same for all users.Resin chooses to cache or not based on the existence of any cookiesin the request, whether they're used or not.</p></s2><s2 title='cache-mapping'><p><var/cache-mapping/> assigns a browser Expires to anIf-Modified cacheable page. It does not affect Expires cached pagesand it does not affect Resin's caching. The FileServlet takesadvantage of <var/cache-mapping/> because it's an If-Modified servlet.</p><p>Often, you want a long Expires time for a page to a browser. Forexample, any gif will not change for 24 hours. That keeps browsersfrom asking for the same gif every five seconds; that's especiallyimportant for tiny formatting gifs. However, as soon as that page orgif changes, you want the change immediately available to any newbrowser or to a browser using reload.</p><p>Here's how you would set the Expires to 24 hours for a gif, basedon the default FileServlet.</p><example><web-app id='/'> <cache-mapping url-pattern='*.gif' expires='24h'/></web-app></example><p>The <var/cache-mapping/> automatically generates the Expiresheader. It only works for cacheable pages setting If-Modified orETag. It will not affect pages explicily setting Expires ornon-cacheable pages. So it's safe to create a cache-mappingfor <var/*.jsp/> even if only some are cacheable.</p></s2></s1>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -