<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Refatorando Padrões &#187; Cache</title>
	<atom:link href="http://www.refatorandopadroes.com.br/category/cache/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.refatorandopadroes.com.br</link>
	<description>Programação orientada às melhores práticas</description>
	<lastBuildDate>Mon, 03 Sep 2007 12:40:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Sem reinventar a roda, cache de páginas com EhCache e ServletFilter</title>
		<link>http://www.refatorandopadroes.com.br/2007/05/03/sem-reinventar-a-roda-cache-de-paginas-com-ehcache-e-servletfilter/</link>
		<comments>http://www.refatorandopadroes.com.br/2007/05/03/sem-reinventar-a-roda-cache-de-paginas-com-ehcache-e-servletfilter/#comments</comments>
		<pubDate>Thu, 03 May 2007 16:01:48 +0000</pubDate>
		<dc:creator>Carlos</dc:creator>
				<category><![CDATA[Cache]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://www.refatorandopadroes.com.br/?p=18</guid>
		<description><![CDATA[Não faz muito tempo, surgiu a necessidade de aumentar o desempenho de uma aplicação JEE, e começamos a ver profile daqui, examina relatórios dali, nisso um amigo de projeto, que a pouco tempo havia se enturmado com Rails, teve uma idéia: "Por que não fazemos cache das páginas mais frequentes, podemos criar um pequeno framework, [...]]]></description>
			<content:encoded><![CDATA[<p>Não faz muito tempo, surgiu a necessidade de aumentar o desempenho de uma aplicação JEE, e começamos a ver profile daqui, examina relatórios dali, nisso um amigo de projeto, que a pouco tempo havia se enturmado com Rails, teve uma idéia: "Por que não fazemos cache das páginas mais frequentes, podemos criar um pequeno framework, e fazer como o Rails faz", explicando... em Rails basta colocar no controller as páginas que você deseja que sejam "cacheadas". Ex:</p>
<div class="igBar"><span id="lruby-6"><a href="#" onclick="javascript:showPlainTxt('ruby-6'); return false;">Trocar para texto plano</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-6">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">caches_page :noticias </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Pronto, a primeira chamada à action "noticias" cria o cache, e será usada pelas chamadas subsequentes. Para expirar o cache basta chamar:</p>
<div class="igBar"><span id="lruby-7"><a href="#" onclick="javascript:showPlainTxt('ruby-7'); return false;">Trocar para texto plano</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-7">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">expire_page<span style="color:#006600; font-weight:bold;">&#40;</span>:controller =&gt; 'public', :action =&gt; 'noticias'<span style="color:#006600; font-weight:bold;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Mais detalhes e excelente tutorial no <a href="http://www.railsenvy.com/2007/2/28/rails-caching-tutorial">Rails Envy</a>.</p>
<p>Eu gosto muito de criar coisas novas, mas como dito no <a rel="nofollow" href="http://www.refatorandopadroes.com.br/2007/04/25/getting-real-o-principio-kiss-aplicado-em-metodologia/">último post</a>, criar mais um mini-framework para dar manutenção em uma aplicação já não muito pequena... bem, não obrigado. Foi por isso que eu disse: "Cara, dá uma pesquisada, com certeza alguém já fez isso." E pra variar, sim alguém já fez isso.</p>
<p>O excelente e bem conhecido <a href="http://ehcache.sourceforge.net/">EhCache</a> possui <a href="http://ehcache.sourceforge.net/EhcacheUserGuide.html#id.s19.5.4">esta funcionalidade</a> e muitas outras.</p>
<p>Este mini tutorial parte do pressuposto que já se tem conhecimento do EhCache e de <a href="http://java.sun.com/products/servlet/Filters.html">ServletFilter</a>, para quem não conhece qualquer um dos dois, sem pânico, são muito fáceis de serem utilizados, vale uma pesquisada por tutoriais.</p>
<p>Para facilitar a vida, podem baixar este <a href='http://www.refatorandopadroes.com.br/wp-content/uploads/2007/05/cache.zip' title='Aplicação exemplo com EhCache'>anexo com a aplicação-exemplo</a> que fiz, basta colocá-lo em seu container web predileto, no meu caso testei no <a href="http://tomcat.apache.org/">Tomcat</a>, e acessar as URLs de teste:</p>
<p>http://localhost:8080/cache/semcache/teste.jsp</p>
<p>http://localhost:8080/cache/comcache/teste.jsp</p>
<p>O exemplo, mostra a hora atual em milesegundos. A página com cache, após acessada, terá 10 seg de vida.</p>
<p>Para implementar bastou os seguintes passos:</p>
<p>- <a href="http://sourceforge.net/project/showfiles.php?group_id=93232">Baixar</a> e instalar os jars EhCache (ehcache-1.3.0-beta.jar), EhCache Constructors (ehcache-constructs-0.7.3.jar) e <a href="http://jakarta.apache.org/commons/logging/">Commons-logging</a> (dependência requerida) no diretório lib.</p>
<p>- Incluir no arquivo ehcache.xml as configurações para cache de página, as propriedades falam por si, configurem a gosto:</p>
<div class="igBar"><span id="lxml-8"><a href="#" onclick="javascript:showPlainTxt('xml-8'); return false;">Trocar para texto plano</a></span></div>
<div class="syntax_hilite"><span class="langName">XML:</span>
<div id="xml-8">
<div class="xml">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;cache</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"SimplePageCachingFilter"</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066;">maxElementsInMemory</span>=<span style="color: #ff0000;">"10000"</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066;">maxElementsOnDisk</span>=<span style="color: #ff0000;">"1000"</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066;">eternal</span>=<span style="color: #ff0000;">"false"</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066;">overflowToDisk</span>=<span style="color: #ff0000;">"true"</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066;">timeToIdleSeconds</span>=<span style="color: #ff0000;">"10"</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066;">timeToLiveSeconds</span>=<span style="color: #ff0000;">"10"</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066;">memoryStoreEvictionPolicy</span>=<span style="color: #ff0000;">"LRU"</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="font-weight: bold; color: black;">&gt;</span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>- Configurar um ServletFilter no web.xml, para a implementação de filtro <a href="<a href="http://ehcache.sourceforge.net/EhcacheUserGuide.html#id.s19.5.4">SimplePageCachingFilter</a></p>
<div class="igBar"><span id="lxml-9"><a href="#" onclick="javascript:showPlainTxt('xml-9'); return false;">Trocar para texto plano</a></span></div>
<div class="syntax_hilite"><span class="langName">XML:</span>
<div id="xml-9">
<div class="xml">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;filter<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;filter</span>-name<span style="font-weight: bold; color: black;">&gt;</span></span>SimplePageCachingFilter<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/filter</span>-name<span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;filter</span>-class<span style="font-weight: bold; color: black;">&gt;</span></span>net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/filter</span>-class<span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;init</span>-param<span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;param</span>-name<span style="font-weight: bold; color: black;">&gt;</span></span>suppressStackTraces<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/param</span>-name<span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;param</span>-value<span style="font-weight: bold; color: black;">&gt;</span></span>false<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/param</span>-value<span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;description<span style="font-weight: bold; color: black;">&gt;</span></span></span>Whether to suppress stack traces in the filter<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/description<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/init</span>-param<span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/filter<span style="font-weight: bold; color: black;">&gt;</span></span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>- Indicar para quais páginas fazer cache a partir do ServletFilter definido acima, dentro do web.xml. No exemplo, tudo que estiver abaixo do diretório "comcache".</p>
<div class="igBar"><span id="lxml-10"><a href="#" onclick="javascript:showPlainTxt('xml-10'); return false;">Trocar para texto plano</a></span></div>
<div class="syntax_hilite"><span class="langName">XML:</span>
<div id="xml-10">
<div class="xml">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;filter</span>-mapping<span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;filter</span>-name<span style="font-weight: bold; color: black;">&gt;</span></span>SimplePageCachingFilter<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/filter</span>-name<span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;url</span>-pattern<span style="font-weight: bold; color: black;">&gt;</span></span>/comcache/*<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/url</span>-pattern<span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/filter</span>-mapping<span style="font-weight: bold; color: black;">&gt;</span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Pronto, é somente isso, a página será devidamente cacheada, seguindo as configurações do ehcache.xml.</p>
<p>Caso queira fazer cache de apenas uma parte da página, no caso de quem utiliza <a href="http://java.sun.com/products/jsp/syntax/1.2/syntaxref1214.html">jsp:include</a>, basta utilizar a outra implementação <a href="http://ehcache.sourceforge.net/EhcacheUserGuide.html#id.s19.5.5">PageFragmentCachingFilter</a></p>
<p>Essa pequena implementação já garantiu um bom desempenho em várias páginas e sem reinventar a roda criando um mini-framework para esta função.</p>
<p>Se alguém souber de outro meio para fazer cache de página em Java, por favor esteja a vontade para comentar, gostaria muito de conhecer outras implementações.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.refatorandopadroes.com.br/2007/05/03/sem-reinventar-a-roda-cache-de-paginas-com-ehcache-e-servletfilter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
