{"id":1011,"date":"2010-11-17T21:43:49","date_gmt":"2010-11-17T21:43:49","guid":{"rendered":"http:\/\/www.frogcake.net\/blog\/?p=1011"},"modified":"2017-11-11T12:07:51","modified_gmt":"2017-11-11T12:07:51","slug":"devoxx-improve-the-performance-of-your-spring-app","status":"publish","type":"post","link":"https:\/\/davidkeen.com\/blog\/2010\/11\/devoxx-improve-the-performance-of-your-spring-app\/","title":{"rendered":"Devoxx &#8211; Improve the performance of your Spring app"},"content":{"rendered":"<p>This talk by Costin Leau was about the new caching features in Spring 3.1.<\/p>\n<p>After going over some problems you can face when caching (stale data, thrashing, distributed caches) and different caching patterns he introduced the declarative caching of Spring 3.1.<\/p>\n<p>It uses AOP and can be declared on methods by adding the @Cacheable annotation. This annotation caches a method&#8217;s return value using it&#8217;s parameters as the key although this can be customised.<\/p>\n<p>Eg:<\/p>\n<pre lang=\"java\">@Cacheable\r\npublic Owner loadOwner(int id);\r\n\r\n@Cacheable(\"owners\")\r\npublic Owner loadOwner(int id);\r\n\r\n\/\/ Using Spring expression language\r\n@Cacheable(key=\"tag.name\")\r\npublic Owner loadOwner(Tag tag, Date created);\r\n\r\n@Cacheable(key=\"T(someType).hash(name)\")\r\npublic Owner loadOwner(String name);\r\n\r\n\/\/ Only cache on condition\r\n@Cacheable(condition=\"name &lt; 10\")\r\npublic Owner loadOwner(String name, Date created);<\/pre>\n<p>The @CacheEvict annotation invalidates the cache:<\/p>\n<pre lang=\"java\">@CacheEvict\r\npublic void deleteOwner(int id);\r\n\r\n@CacheEvict(\"owners\", key=\"id\")\r\npublic void deleteOwner(int id, boolean saveCopy);\r\n\r\n@CacheEvict(name=\"owners\", allEntries=true)\r\npublic void batchUpdate()<\/pre>\n<p>You can also use your own stereotype annotations. Instead of spreading @Cacheable everywhere:<\/p>\n<pre lang=\"java\">@Retention(RetentionPolicy.RUNTIME)\r\n@Target({ElementType.METHOD})\r\n@Cacheable(\"results\")\r\npublic @interface SlowService {\r\n\r\n}\r\n\r\n@SlowService\r\npublic Source search(String tag) {...}<\/pre>\n<p>Spring is not a cache provider itself. You plug in your own cache provider (ehcache, JBoss Cache, etc).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This talk by Costin Leau was about the new caching features in Spring 3.1. After going over some problems you can face when caching (stale data, thrashing, distributed caches) and different caching patterns he introduced the declarative caching of Spring 3.1. It uses AOP and can be declared on methods by adding the @Cacheable annotation. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2668,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[25],"tags":[17],"class_list":["post-1011","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dev","tag-devoxx"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/davidkeen.com\/blog\/wp-content\/uploads\/2010\/11\/pexels-photo-236566.jpeg?fit=1280%2C853&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/davidkeen.com\/blog\/wp-json\/wp\/v2\/posts\/1011","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/davidkeen.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/davidkeen.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/davidkeen.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/davidkeen.com\/blog\/wp-json\/wp\/v2\/comments?post=1011"}],"version-history":[{"count":15,"href":"https:\/\/davidkeen.com\/blog\/wp-json\/wp\/v2\/posts\/1011\/revisions"}],"predecessor-version":[{"id":2593,"href":"https:\/\/davidkeen.com\/blog\/wp-json\/wp\/v2\/posts\/1011\/revisions\/2593"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/davidkeen.com\/blog\/wp-json\/wp\/v2\/media\/2668"}],"wp:attachment":[{"href":"https:\/\/davidkeen.com\/blog\/wp-json\/wp\/v2\/media?parent=1011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/davidkeen.com\/blog\/wp-json\/wp\/v2\/categories?post=1011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/davidkeen.com\/blog\/wp-json\/wp\/v2\/tags?post=1011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}