📄 proxy-cache.xtp
字号:
<s1 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</var> 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</var> counter example as the included page. Create atop-level page that looks like:</p><example title="Example: top-level non-cached page"><% if (! session.isNew()) { %><h1>Welcome back!</h1><% } %><jsp:include page="expires.jsp"/></example><example title="Example: cached include page"><%@ page session="false" %><%! int counter; %><%response.setHeader("Cache-Control", "max-age=15");%>Count: <%= counter++ %></example></s1><s1 title="Caching Anonymous Users"><p>The Vary header can be used to implement anonymous user caching. If a useris not logged in, he will get a cached page. If he's logged in, he'llget his 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>Vary: Cookie</var>If you omit the Vary header, Resin will usethe <var>max-age</var> to cache the same page for every user.</p><example title="Example: 'Vary: Cookie' for anonymous users"><%@ page session="false" %><%! int _counter; %><%response.addHeader("Cache-Control", "max-age=15");response.addHeader("Vary", "Cookie");String user = request.getParameter("user");%>User: <%= user %> <%= counter++ %></example><p>The top page must still set the <var>max-age</var> or<var>If-Modified</var> header, but Resin will take care of deciding if thepage is cacheable or not. If the request has any cookies, Resin willnot cache it and will not use the cached page.If it has no cookies, Resin will use the cached page.</p><p>When using Vary: Cookie, 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></s1><s1 title="Configuration"><s2 title="cache"><parents>cluster</parents><p><cache> configures the proxy cache (requires Resin Professional). Theproxy cache improves performance by caching the output of servlets,jsp and php pages. For database-heavy pages, this caching can improveperformance and reduce database load by several orders of magnitude.</p><p>The proxy cache uses a combination of a memory cache and a disk-basedcache to save large amounts of data with little overhead.</p><p>Management of the proxy cache uses the<a href="javadoc|com.caucho.management.server.ProxyCacheMXBean">ProxyCacheMXBean</a>.</p><deftable title="<cache> Attributes"><tr> <th>Attribute</th> <th>Description</th> <th>Default</th></tr><tr><td>path</td> <td>Path to the persistent cache files.</td> <td>cache/</td></tr><tr><td>disk-size</td> <td>Maximum size of the cache saved on disk.</td> <td>1024M</td></tr><tr><td>enable</td> <td>Enables the proxy cache.</td> <td>true</td></tr><tr><td>enable-range</td> <td>Enables support for the HTTP Range header.</td> <td>true</td></tr><tr><td>entries</td> <td>Maximum number of pages stored in the cache.</td> <td>8192</td></tr><tr><td>max-entry-size</td> <td>Largest page size allowed in the cache.</td> <td>1M</td></tr><tr><td>memory-size</td> <td>Maximum heap memory used to cache blocks.</td> <td>8M</td></tr><tr><td>rewrite-vary-as-private</td> <td>Rewrite Vary headers as Cache-Control: private to avoid browserand proxy-cache bugs (particularly IE).</td> <td>false</td></tr></deftable><def title="<cache> schema">element cache { disk-size? & enable? & enable-range? & entries? & path? & max-entry-size? & memory-size? & rewrite-vary-as-private?}</def><example title="Example: enabling proxy cache"><resin xmlns="http://caucho.com/ns/resin"> <cluster id="web-tier"> <cache entries="16384" disk-size="2G" memory-size="256M"/> <server id="a" address="192.168.0.10"/> <host host-name="www.foo.com"> </cluster></resin></example><s3 title="rewrite-vary-as-private"><p>Because not all browsers understand the Vary header, Resin can rewriteVary to a Cache-Control: private. This rewriting will cache the pagewith the Vary in Resin's proxy cache, and also cache the page in the browser.Any other proxy caches, however, will not be able to cache the page.</p><p>The underlying issue is a limitation of browsers such as IE. When IEsees a Vary header it doesn't understand, it marks the page as uncacheable.Since IE only understands "Vary: User-Agent", this would mean IE wouldrefuse to cache gzipped pages or "Vary: Cookie" pages.</p><p>With the <rewrite-vary-as-private> tag, IE will cache the pagesince it's rewritten as "Cache-Control: private" with no Vary at all.Resin will continue to cache the page as normal.</p></s3></s2> <!-- cache --><s2 title="cache-mapping"><p><var>cache-mapping</var> assigns a <var>max-age</var>and <var>Expires</var> to a cacheable page, i.e. a page with an <var>ETag</var>or <var>Last-Modified</var> setting. It does not affect <var>max-age</var> or<var>Expires</var> cached pages. The FileServlet takesadvantage of <var>cache-mapping</var> because it provides theETag 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 title="Example: caching .gif files for 24h"><web-app xmlns="http://caucho.com/ns/resin"> <cache-mapping url-pattern='*.gif' expires='24h'/></web-app></example><p>The <var>cache-mapping</var> 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</var> even if only some are cacheable.</p></s2></s1><s1 title="Debugging caching"><p>When designing and testing your cached page, it's important to seehow Resin is caching the page. To turn on logging for caching, you'lladd the following to your resin.xml:</p><example title="Example: adding caching log"><resin xmlns="http://caucho.com/ns/resin"> <logger name="com.caucho.server.cache" level="fine"/> ...</resin></example><p>The output will look something like the following:</p><results>[10:18:11.369] caching: /images/caucho-white.jpg etag="AAAAPbkEyoA" length=6190[10:18:11.377] caching: /images/logo.gif etag="AAAAOQ9zLeQ" length=571[10:18:11.393] caching: /css/default.css etag="AAAANzMooDY" length=1665[10:18:11.524] caching: /images/pixel.gif etag="AAAANpcE4pY" length=61...[10:18:49.303] using cache: /css/default.css[10:18:49.346] using cache: /images/pixel.gif[10:18:49.348] using cache: /images/caucho-white.jpg[10:18:49.362] using cache: /images/logo.gif</results></s1><s1 title="Administration"><s2 title="/resin-admin"><s3 title="block cache miss ratio"><p>The block cache miss ratio tells how often Resin needs to access thedisk to read a cache entry. Most cache requests should come from memoryto improve performance, but cache entries are paged out to disk when thecache gets larger. It's very important to keep the <memory-size> tagof the <cache> large enough so the block cache miss ratio is small.</p></s3><s3 title="proxy cache miss ratio"><p>The proxy cache miss ratio measures how often cacheable pages mustgo to their underlying servlet instead of being cached. The miss ratiodoes not measure the non-cacheable pages.</p></s3><s3 title="invocation miss ratio"><p>The invocation miss ratio measures how often Resin's invocation cachemisses. The invocation cache is used for both cacheable and non-cacheablepages to save the servlet and filter chain construction. A miss of theinvocation cache is expensive, since it will not only execute the servlet,but also force the servlet and filter chain to be rebuilt. The <entries>field of the <cache> controlls the invocation miss ratio.</p></s3></s2><s2 title="BlockManagerMXBean"><p>BlockManagerMXBean returns statistics about the block cache. Since Resin'sblock cache is used for the proxy cache as well as clustered sessions andJMS messages, the performance of the block cache is very important. Theblock cache is a memory/paging cache on top of a file-based backing store.If the block cache misses, a request needs to go to disk, otherwise itcan be served directly from memory.</p><def title="BlockManagerMXBean ObjectName">resin:type=BlockManager</def><def title="BlockManagerMXBean.java">public interface BlockManagerMXBean { public long getBlockCapacity(); public long getHitCountTotal(); public long getMissCountTotal();}</def></s2><s2 title="ProxyCacheMXBean"><p>The ProxyCacheMXBean provides statistics about the proxy cache as wellas operations to clear the cache. The hit and miss counts tell howeffectively the cache is improving performance.</p><def title="ProxyCacheMXBean ObjectName">resin:type=ProxyCache</def><def title="ProxyCacheMXBean.java">public interface ProxyCacheMXBean { public long getHitCountTotal(); public long getMissCountTotal(); public CacheItem []getCacheableEntries(int max); public CacheItem []getUncacheableEntries(int max); public void clearCache(); public void clearCacheByPattern(String hostRegexp, String urlRegexp); public void clearExpires();}</def></s2></s1> </body></document>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -