{"id":1615,"date":"2016-10-04T17:27:37","date_gmt":"2016-10-04T15:27:37","guid":{"rendered":"http:\/\/blog.rabahi.net\/?page_id=1615"},"modified":"2018-12-01T16:44:18","modified_gmt":"2018-12-01T15:44:18","slug":"java-spring-cache","status":"publish","type":"page","link":"https:\/\/blog.rabahi.net\/?page_id=1615","title":{"rendered":"Java Spring &#8211; Cache"},"content":{"rendered":"<div id=\"toc_container\" class=\"no_bullets\"><p class=\"toc_title\">Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Prerequistes\"><span class=\"toc_number toc_depth_1\">1<\/span> Prerequistes<\/a><\/li><li><a href=\"#Configuration\"><span class=\"toc_number toc_depth_1\">2<\/span> Configuration<\/a><ul><li><a href=\"#SpringApplicationjava\"><span class=\"toc_number toc_depth_2\">2.1<\/span> SpringApplication.java<\/a><\/li><li><a href=\"#ehcache3xml\"><span class=\"toc_number toc_depth_2\">2.2<\/span> ehcache3.xml<\/a><\/li><li><a href=\"#applicationproperties\"><span class=\"toc_number toc_depth_2\">2.3<\/span> application.properties<\/a><\/li><\/ul><\/li><li><a href=\"#Use_it\"><span class=\"toc_number toc_depth_1\">3<\/span> Use it<\/a><ul><li><a href=\"#Model\"><span class=\"toc_number toc_depth_2\">3.1<\/span> Model<\/a><\/li><li><a href=\"#Request_example\"><span class=\"toc_number toc_depth_2\">3.2<\/span> Request example<\/a><\/li><\/ul><\/li><\/ul><\/div>\n<h1><span id=\"Prerequistes\">Prerequistes<\/span><\/h1>\n<p>If you use maven, please add this to your pom.xml:<\/p>\n<pre lang=\"xml\">\r\n   <!-- spring boot -->\r\n\t<dependency>\r\n\t\t<groupId>org.springframework.boot<\/groupId>\r\n\t\t<artifactId>spring-boot-starter-cache<\/artifactId>\r\n\t<\/dependency>\r\n\r\n\t<!-- use JSR-107 annotations -->\r\n\t<dependency>\r\n\t\t<groupId>javax.cache<\/groupId>\r\n\t\t<artifactId>cache-api<\/artifactId>\r\n\t<\/dependency>\r\n\r\n\t<!-- use ehcache implementation -->\r\n\t<dependency>\r\n\t\t<groupId>org.ehcache<\/groupId>\r\n\t\t<artifactId>ehcache<\/artifactId>\r\n\t<\/dependency>\r\n<\/pre>\n<h1><span id=\"Configuration\">Configuration<\/span><\/h1>\n<h2><span id=\"SpringApplicationjava\">SpringApplication.java<\/span><\/h2>\n<pre lang=\"java\">\r\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\r\nimport org.springframework.boot.builder.SpringApplicationBuilder;\r\nimport org.springframework.cache.annotation.EnableCaching;\r\n\r\n@EnableCaching\r\n@SpringBootApplication\r\npublic class SpringApplication {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tnew SpringApplicationBuilder()\r\n\t\t.sources(SpringApplication.class)\r\n\t\t.run(args);\r\n\t}\r\n}\r\n<\/pre>\n<h2><span id=\"ehcache3xml\">ehcache3.xml<\/span><\/h2>\n<pre lang=\"xml\">\r\n<config xmlns='http:\/\/www.ehcache.org\/v3' xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n\txmlns:jsr107=\"http:\/\/www.ehcache.org\/v3\/jsr107\"\r\n\txsi:schemaLocation=\"http:\/\/www.ehcache.org\/v3 http:\/\/www.ehcache.org\/schema\/ehcache-core-3.0.xsd\r\n\t\t\t\t\t\t\thttp:\/\/www.ehcache.org\/v3\/jsr107 http:\/\/www.ehcache.org\/schema\/ehcache-107-ext-3.0.xsd\">\r\n\t<cache alias=\"users\" uses-template=\"event-cache\">\r\n\t\t<expiry>\r\n\t\t\t<ttl unit=\"seconds\">600<\/ttl>\r\n\t\t<\/expiry>\r\n\t\t<heap unit=\"entries\">200<\/heap>\r\n\t\t<jsr107:mbeans enable-statistics=\"true\" \/>\r\n\t<\/cache>\r\n\r\n\t<cache-template name=\"event-cache\">\r\n\t\t<listeners>\r\n\t\t\t<listener>\r\n\t\t\t\t<class>net.rabahi.java.spring.cache.listener.EventLogger<\/class>\r\n\t\t\t\t<event-firing-mode>ASYNCHRONOUS<\/event-firing-mode>\r\n\t\t\t\t<event-ordering-mode>UNORDERED<\/event-ordering-mode>\r\n\t\t\t\t<events-to-fire-on>CREATED<\/events-to-fire-on>\r\n\t\t\t\t<events-to-fire-on>UPDATED<\/events-to-fire-on>\r\n\t\t\t\t<events-to-fire-on>EXPIRED<\/events-to-fire-on>\r\n\t\t\t\t<events-to-fire-on>REMOVED<\/events-to-fire-on>\r\n\t\t\t\t<events-to-fire-on>EVICTED<\/events-to-fire-on>\r\n\t\t\t<\/listener>\r\n\t\t<\/listeners>\r\n\t\t<resources>\r\n\t\t\t<heap unit=\"entries\">2000<\/heap>\r\n\t\t\t<offheap unit=\"MB\">100<\/offheap>\r\n\t\t<\/resources>\r\n\t<\/cache-template>\r\n\r\n<\/config>\r\n<\/pre>\n<h2><span id=\"applicationproperties\">application.properties<\/span><\/h2>\n<pre lang=\"ini\">\r\n# provide a specific caching configuration\r\nspring.cache.jcache.config=classpath:ehcache3.xml\r\n<\/pre>\n<h1><span id=\"Use_it\">Use it<\/span><\/h1>\n<h2><span id=\"Model\">Model<\/span><\/h2>\n<p>Create the following model :<\/p>\n<pre lang=\"java\">\r\nimport javax.cache.annotation.CacheResult;\r\n\r\nimport org.springframework.stereotype.Service;\r\n\r\nimport lombok.extern.slf4j.Slf4j;\r\n\r\n@Service\r\n@Slf4j\r\npublic class UserServiceImpl {\r\n\r\n  @Override\r\n  @CacheResult(cacheName=\"users\")\r\n  public String findById(int id){\r\n    LOGGER.info(\"Find user by id : {}\",id);\r\n    return \"User-\"+id;\r\n  }\r\n}\r\n<\/pre>\n<p>Note: the annotation @CacheResult(&#8220;users&#8221;) refers to the cache defined in ehcache3.xml.<\/p>\n<h2><span id=\"Request_example\">Request example<\/span><\/h2>\n<pre lang=\"java\">\r\n\/\/ Fetch user with id 1\r\nuser.findById(1);\r\n\r\n\/\/ Fetch again user with id 1, result will be fetched from cache\r\nuser.findById(1);\r\n\r\n\/\/ Fetch user with id 2\r\nuser.findById(2);\r\n\r\n\/\/ Fetch again user with id 2, result will be fetched from cache\r\nuser.findById(2);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 Prerequistes2 Configuration2.1 SpringApplication.java2.2 ehcache3.xml2.3 application.properties3 Use it3.1 Model3.2 Request example Prerequistes If you use maven, please add this to your pom.xml: org.springframework.boot spring-boot-starter-cache javax.cache cache-api org.ehcache ehcache Configuration SpringApplication.java import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cache.annotation.EnableCaching; @EnableCaching @SpringBootApplication public class SpringApplication { public static void main(String[] args) { new SpringApplicationBuilder() .sources(SpringApplication.class) .run(args); } } ehcache3.xml [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1547,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1615","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1615","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1615"}],"version-history":[{"count":4,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1615\/revisions"}],"predecessor-version":[{"id":2062,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1615\/revisions\/2062"}],"up":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1547"}],"wp:attachment":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}