{"id":8586,"date":"2012-09-25T19:26:22","date_gmt":"2012-09-25T13:56:22","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=8586"},"modified":"2012-09-25T19:26:22","modified_gmt":"2012-09-25T13:56:22","slug":"how-to-integrate-spring-cache-plugin-with-grails","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-integrate-spring-cache-plugin-with-grails\/","title":{"rendered":"How to integrate Spring cache plugin with Grails"},"content":{"rendered":"<p>In one of my recent project, i want to cache\u00a0 method output. For implementing method level cache, i have used spring cache. For implement Spring cache, we can use grails cache plugin.<\/p>\n<p style=\"padding-bottom: 10px\">\n<p>It&#8217;s very easy to integrate cache plugin with grails.<\/p>\n<p>1. Add following plugin dependency in BuildConfig.groovy.<\/p>\n<p>[groovy]compile &quot;:springcache:1.3.1&quot;<\/p>\n<p>[\/groovy]<\/p>\n<p style=\"padding-bottom: 10px\">\n<p>2.\u00a0 We need to define the caches in resource.groovy. Use below mention code to create cache in resources.groovy.<\/p>\n<p>[groovy]<\/p>\n<p>studentCache(EhCacheFactoryBean) { bean -&gt;<br \/>\ncacheManager = ref(&quot;springcacheCacheManager&quot;)<br \/>\ncacheName = &quot;studentCache&quot;<br \/>\nmemoryStoreEvictionPolicy = &quot;LRU&quot;<br \/>\n}<br \/>\n[\/groovy]<\/p>\n<p>EhCacheFactoryBean &#8211; Pre define class of the spring<br \/>\nstudentCache &#8211; Cache name<\/p>\n<p>3. Add following codes in Config.groovy file to enable cache.<\/p>\n<p>[groovy]<br \/>\nspringcache {<br \/>\n    defaults {<br \/>\n        eternal = false<br \/>\n        diskPersistent = false<br \/>\n    }<br \/>\n    caches {<br \/>\n        studentCache {<br \/>\n            memoryStoreEvictionPolicy = &quot;LRU&quot;<br \/>\n           ttl=1000<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\n[\/groovy]<\/p>\n<p>We need to mention the cache name that we want to enable. We can enable multiple cache.<\/p>\n<p style=\"padding-bottom: 10px\">\n<p>4. Add @Cacheable annotation above method that you want to cache.<\/p>\n<p>[groovy]<br \/>\n@Cacheable(cache=&quot;studentCache&quot;)<br \/>\n    Student showStudent(long id){<br \/>\n        return Student.get(id)<br \/>\n    }<br \/>\n[\/groovy]<\/p>\n<p>@Cacheable &#8211; We need to pass cache name<br \/>\nBut one thing we need to ensure, the class in which we are implementing the cache, it should be managed by spring or grails.<br \/>\nRun server, hit the url, you can see our method output is cached.<br \/>\nHope so this code will work for you \ud83d\ude42<\/p>\n<p style=\"padding-bottom: 10px\">\n<p>To know more about Cache, you can take the reference by using below link.<br \/>\n<a href=\"http:\/\/grails.org\/plugin\/springcache\">http:\/\/grails.org\/plugin\/springcache<\/a><\/p>\n<p style=\"padding-bottom: 10px\">\n","protected":false},"excerpt":{"rendered":"<p>In one of my recent project, i want to cache\u00a0 method output. For implementing method level cache, i have used spring cache. For implement Spring cache, we can use grails cache plugin. It&#8217;s very easy to integrate cache plugin with grails. 1. Add following plugin dependency in BuildConfig.groovy. [groovy]compile &quot;:springcache:1.3.1&quot; [\/groovy] 2.\u00a0 We need to [&hellip;]<\/p>\n","protected":false},"author":47,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":8},"categories":[7],"tags":[350,1072,4840,4841],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8586"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/47"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=8586"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8586\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=8586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=8586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=8586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}