<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>My web and Java snippets</title>
	<atom:link href="http://urmincek.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://urmincek.wordpress.com</link>
	<description>Web, JavaScript, Java, Spring... whatever I think is cool</description>
	<lastBuildDate>Sun, 20 Sep 2009 08:22:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='urmincek.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>My web and Java snippets</title>
		<link>http://urmincek.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://urmincek.wordpress.com/osd.xml" title="My web and Java snippets" />
	<atom:link rel='hub' href='http://urmincek.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Simple caching with Spring AOP</title>
		<link>http://urmincek.wordpress.com/2009/09/18/simple-caching-with-spring-aop/</link>
		<comments>http://urmincek.wordpress.com/2009/09/18/simple-caching-with-spring-aop/#comments</comments>
		<pubDate>Fri, 18 Sep 2009 18:39:53 +0000</pubDate>
		<dc:creator>Igor Urminček</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[annotations]]></category>
		<category><![CDATA[AspectJ]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Spring AOP]]></category>

		<guid isPermaLink="false">http://urmincek.wordpress.com/?p=25</guid>
		<description><![CDATA[It&#8217;s recommended to read Simple caching with AspectJ before you continue. Only difference is in using Spring AOP. At first we need to create spring-config.xml and define Spring beans and enable Spring AOP autoproxy for AspectJ: Now we can use Calculator bean created by Spring: Download full source code as a zip or tar.gz file [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=urmincek.wordpress.com&amp;blog=9340079&amp;post=25&amp;subd=urmincek&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s recommended to read <a href="http://urmincek.wordpress.com/2009/09/06/simple-caching-with-aspectj/">Simple caching with AspectJ</a> before you continue. Only difference is in using Spring AOP.<br />
At first we need to create spring-config.xml and define Spring beans and enable Spring AOP autoproxy for AspectJ:<br />
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
	xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
	xmlns:aop=&quot;http://www.springframework.org/schema/aop&quot;
	xsi:schemaLocation=&quot;http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd&quot;&gt;

	&lt;aop:aspectj-autoproxy /&gt;
	&lt;bean class=&quot;caching.springaop.CacheAspect&quot; /&gt;
	
	&lt;bean id=&quot;calc&quot; class=&quot;caching.springaop.Calculator&quot; /&gt;

&lt;/beans&gt;
</pre><br />
Now we can use <code>Calculator</code> bean created by Spring:<br />
<pre class="brush: java;">
ApplicationContext ctx = new ClassPathXmlApplicationContext(&quot;spring-config.xml&quot;);
Calculator calc = (Calculator) ctx.getBean(&quot;calc&quot;);

// result will be calculated and stored in cache
logger.info(&quot;1 + 2 = &quot; + calc.sum(1, 2));

// result will be retrieved from cache
logger.info(&quot;1 + 2 = &quot; + calc.sum(1, 2));
</pre><br />
Download full source code as a <a href="http://github.com/igo/demo-caching-with-spring-aop/zipball/master">zip</a> or <a href="http://github.com/igo/demo-caching-with-spring-aop/tarball/master">tar.gz</a> file or <a href="http://github.com/igo/demo-caching-with-spring-aop/tree/master">browse code online on GitHub</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/urmincek.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/urmincek.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/urmincek.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/urmincek.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/urmincek.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/urmincek.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/urmincek.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/urmincek.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/urmincek.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/urmincek.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/urmincek.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/urmincek.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/urmincek.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/urmincek.wordpress.com/25/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=urmincek.wordpress.com&amp;blog=9340079&amp;post=25&amp;subd=urmincek&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://urmincek.wordpress.com/2009/09/18/simple-caching-with-spring-aop/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/530da5e2749551bbe54453a5d6ef35ec?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">igo</media:title>
		</media:content>
	</item>
		<item>
		<title>Simple caching with AspectJ</title>
		<link>http://urmincek.wordpress.com/2009/09/06/simple-caching-with-aspectj/</link>
		<comments>http://urmincek.wordpress.com/2009/09/06/simple-caching-with-aspectj/#comments</comments>
		<pubDate>Sun, 06 Sep 2009 13:15:57 +0000</pubDate>
		<dc:creator>Igor Urminček</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[annotations]]></category>
		<category><![CDATA[AspectJ]]></category>
		<category><![CDATA[caching]]></category>

		<guid isPermaLink="false">http://urmincek.wordpress.com/?p=9</guid>
		<description><![CDATA[Caching hasn&#8217;t been never so easy to implement before. AspectJ is one of the best (probably the best) technologies that can be used for implementing caching system. There are several ways how to do caching. Object can directly set values to cache, can implement some caching interface or can be fancy and use annotations that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=urmincek.wordpress.com&amp;blog=9340079&amp;post=9&amp;subd=urmincek&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Caching hasn&#8217;t been never so easy to implement before. AspectJ is one of the best (probably the best) technologies that can be used for implementing caching system.</p>
<p>There are several ways how to do caching. Object can directly set values to cache, can implement some caching interface or can be fancy and use annotations that sounds to me as a most elegant solution. With annotation you can annotate any method that should be cached. Could be something easier?</p>
<p>Let&#8217;s create <code>@Cacheable</code> annotation:<br />
<pre class="brush: java;">
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Cacheable {

}
</pre></p>
<p>With this annotation we can mark any expensive, time consuming methods whose results should be cached. For example let&#8217;s create <code>Calculator</code> class that has one very expensive and time consuming method. Every time we call this method with same input it will be executed. It&#8217;s a huge wasting of resources. So we will use our <code>@Cacheable</code> annotation on it&#8217;s function to prevent executing it when it&#8217;s not necessary.<br />
<pre class="brush: java;">
public class Calculator {

	private Logger logger = Logger.getLogger(Calculator.class);

	@Cacheable
	public int sum(int a, int b) {
		logger.info(&quot;Calculating &quot; + a + &quot; + &quot; + b);
		try {
			// pretend this is an expensive operation
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			logger.error(&quot;Something went wrong...&quot;, e);
		}
		return a + b;
	}

}
</pre></p>
<p>Next we need to create an aspect called <code>CacheAspect</code> that will handle caching. In this aspect we define pointcut that matches all methods annotated with <code>@Cacheable</code>, regardless of number and type of input arguments and return type:<br />
<pre class="brush: java;">
@Pointcut(&quot;execution(@Cacheable * *.*(..))&quot;)
private void cache() {}
</pre><br />
It is a best practice to annotate empty methods with a pointcut. Function <code>cache()</code> can be considered as a name for a pointcut.<br />
As a last step we need to create an advice &#8211; a function that wraps calling of cacheable methods. This function will be actually called every time empty function <code>cache()</code> is invoked. Advise <code>aroundCachedMethods</code> will take a look into cache for a previously cached result and return it. If it couldn&#8217;t find anything then it will call our time consuming function and store result for next reuse.<br />
<pre class="brush: java;">
@Around(&quot;cache()&quot;)
public Object aroundCachedMethods(ProceedingJoinPoint thisJoinPoint)
		throws Throwable {
	logger.debug(&quot;Execution of Cacheable method catched&quot;);

	// generate the key under which cached value is stored
	// will look like caching.aspectj.Calculator.sum(Integer=1;Integer=2;)
	StringBuilder keyBuff = new StringBuilder();

	// append name of the class
	keyBuff.append(thisJoinPoint.getTarget().getClass().getName());

	// append name of the method
	keyBuff.append(&quot;.&quot;).append(thisJoinPoint.getSignature().getName());

	keyBuff.append(&quot;(&quot;);
	// loop through cacheable method arguments
	for (final Object arg : thisJoinPoint.getArgs()) {
		// append argument type and value
		keyBuff.append(arg.getClass().getSimpleName() + &quot;=&quot; + arg + &quot;;&quot;);
	}
	keyBuff.append(&quot;)&quot;);
	String key = keyBuff.toString();

	logger.debug(&quot;Key = &quot; + key);
	Object result = cache.get(key);
	if (result == null) {
		logger.debug(&quot;Result not yet cached. Must be calculated...&quot;);
		result = thisJoinPoint.proceed();
		logger.info(&quot;Storing calculated value '&quot; + result + &quot;' to cache&quot;);
		cache.put(key, result);
	} else {
		logger.debug(&quot;Result '&quot; + result + &quot;' was found in cache&quot;);
	}

	return result;
}
</pre></p>
<p>Download full source code as a <a href="http://github.com/igo/demo-caching-with-aspectj/zipball/master">zip</a> or <a href="http://github.com/igo/demo-caching-with-aspectj/tarball/master">tar.gz</a> file or <a href="http://github.com/igo/demo-caching-with-aspectj/tree/master">browse code online on GitHub</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/urmincek.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/urmincek.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/urmincek.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/urmincek.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/urmincek.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/urmincek.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/urmincek.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/urmincek.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/urmincek.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/urmincek.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/urmincek.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/urmincek.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/urmincek.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/urmincek.wordpress.com/9/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=urmincek.wordpress.com&amp;blog=9340079&amp;post=9&amp;subd=urmincek&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://urmincek.wordpress.com/2009/09/06/simple-caching-with-aspectj/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/530da5e2749551bbe54453a5d6ef35ec?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">igo</media:title>
		</media:content>
	</item>
	</channel>
</rss>
