{"id":50455,"date":"2017-09-20T11:00:48","date_gmt":"2017-09-20T08:00:48","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=50455"},"modified":"2021-02-24T15:05:29","modified_gmt":"2021-02-24T13:05:29","slug":"spring-caching-tutorial","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/","title":{"rendered":"Spring Caching Tutorial"},"content":{"rendered":"<p>In this article, we will show you the Spring caching through examples.<\/p>\n<p>Caching is crucial for today\u2019s high-performance hungry applications. Adding Caching in a spring-based application is rather trivial. In this tutorial, we will show you how to enable data caching in a spring application by integrating the application with the popular <a href=\"http:\/\/www.ehcache.org\/\" target=\"_blank\" rel=\"noopener\">EhCache<\/a> framework.<\/p>\n<\/p>\n<div class=\"toc\">\n<h3>Table Of Contents<\/h3>\n<dl>\n<dt><a href=\"#introduction\">1. Introduction<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#springIntroduction\">1.1 Spring Framework<\/a><\/dt>\n<dt><a href=\"#springCacheIntroduction\">1.2 Spring Cache<\/a><\/dt>\n<dt><a href=\"#ehCacheIntroduction\">1.3 EhCache<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#springCacheTutorial\">2. Spring Cache Tutorial<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#toolsUsed\">2.1 Tools used<\/a><\/dt>\n<dt><a href=\"#projectStructure\">2.2 Project Structure<\/a><\/dt>\n<dt><a href=\"#projectCreation\">2.3 Project Creation<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#applicationBuilding\">3. Application Building<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#mavenDependencies\">3.1 Maven Dependencies<\/a><\/dt>\n<dt><a href=\"#javaClassCreation\">3.2 Java Class Creation<\/a><\/dt>\n<dt><a href=\"#ehCacheConfigurationFile\">3.3 EhCache Configuration File<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#projectRun\">4. Run the Application<\/a><\/dt>\n<dt><a href=\"#projectDemo\">5. Project Demo<\/a><\/dt>\n<dt><a href=\"#projectConclusion\">6. Conclusion<\/a><\/dt>\n<dt><a href=\"#projectDownload\">7. Download the Eclipse Project<\/a><\/dt>\n<\/dl>\n<\/div>\n<h2 class=\"wp-block-heading\" id=\"h-1-introduction\"><a name=\"introduction\"><\/a>1. Introduction<\/h2>\n<h3 class=\"wp-block-heading\" id=\"h-1-1-spring-framework\"><a name=\"springIntroduction\"><\/a>1.1 Spring Framework<\/h3>\n<ul class=\"wp-block-list\">\n<li>Spring is an open-source framework created to address the complexity of an enterprise application development<\/li>\n<li>One of the chief advantages of the Spring framework is its layered architecture, which allows developers to be selective about which of its components they can use while providing a cohesive framework for <code>J2EE<\/code> application development<\/li>\n<li>Spring framework provides support and integration to various technologies for e.g.:\n<ul>\n<li>Support for Transaction Management<\/li>\n<li>Support for interaction with the different databases<\/li>\n<li>Integration with the Object Relationship frameworks for e.g. Hibernate, iBatis etc<\/li>\n<li>Support for <span style=\"text-decoration: underline;\">Dependency Injection<\/span> which means all the required dependencies will be resolved with the help of containers<\/li>\n<li>Support for <code>REST<\/code> style web-services<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3 class=\"wp-block-heading\" id=\"h-1-2-spring-cache\"><a name=\"springCacheIntroduction\"><\/a>1.2 Spring Cache<\/h3>\n<ul class=\"wp-block-list\">\n<li>Spring framework provides support for transparently adding the caching to an application at the startup time, thereby increasing the application performance<\/li>\n<li>This is an abstract framework where spring only provides the layer where other third-party caching implementations can be easily plugged for storing the data<\/li>\n<li>In short, the cache storage is not implemented by the spring framework whereas enabling and caching is supported by the spring out of the box<\/li>\n<li>Caching is supported for the methods and it works well if the method returns the same result for the given input for the multiple invocations<\/li>\n<\/ul>\n<h4 class=\"wp-block-heading\" id=\"h-1-2-1-cache-providers-in-spring-framework\">1.2.1 Cache Providers in Spring Framework<\/h4>\n<p>Developers know that the actual implementation of the cache is by the third party library and spring framework provides only the abstraction layer for enabling that specific cache implementation to store the data. So there are the following caches that are supported by the spring framework:<\/p>\n<ul class=\"wp-block-list\">\n<li>JDK (i.e. <code>java.util.concurrent.ConcurrentMap<\/code>) Based Caches<\/li>\n<li>EhCache 2.x<\/li>\n<li>Gemfire Cache<\/li>\n<li>Guava Cache<\/li>\n<li>JCache (JSR 107)<\/li>\n<li>Infinispan<\/li>\n<li>Couchbase<\/li>\n<\/ul>\n<h4 class=\"wp-block-heading\" id=\"h-1-2-2-cache-annotations-in-spring-framework\">1.2.2 Cache Annotations in Spring Framework<\/h4>\n<p>The following below is the list of <span style=\"text-decoration: underline;\">Spring Caching Annotations<\/span>. These are specific to the spring framework and apart from that, there are few more <a href=\"http:\/\/javabeat.net\/annotations-in-java-5-0\/\" target=\"_blank\" rel=\"noopener\">annotations<\/a> that are implemented as part of the JCache (JSR \u2013 107). Let us take a closer look at each annotation:<\/p>\n<ul class=\"wp-block-list\">\n<li><strong>Enable Cache<\/strong>: This tag enables the caching because <em>caching in spring is not enabled by default<\/em>. The caching feature can be declaratively enabled by simply adding the <code>@EnableCaching<\/code> annotation to any of the configuration classes. Here is an example:\n<pre class=\"brush:java; wrap-lines:false;\">@EnableCaching\npublic class SpringCacheImplementation {\n\tpublic static void main(String[] args) {\n\t\t\tapplicationObj.run(SpringCacheImplementation.class, args);\n\t}\n}\n<\/pre>\n<p>In an <code>XML<\/code>, developers can also enable the caching by using the below tag:<\/p>\n<pre class=\"brush:xml; wrap-lines:false;\">&lt;cache:annotation-driven \/&gt;\n<\/pre>\n<\/li>\n<li><strong>Cache Configurations<\/strong>: This tag configures the cache manager where the backing data is to be stored and retrieved for the quick response<\/li>\n<li><strong>Caching Declarations<\/strong>: This tag Identifies the methods that have to be cached and defines their caching policy<\/li>\n<\/ul>\n<p>After <span style=\"text-decoration: underline;\">enabling the cache<\/span> developers can use the below list of declarative annotations:<\/p>\n<ul class=\"wp-block-list\">\n<li><strong>Cacheable<\/strong>: The <code>@Cacheable<\/code> annotation is one of the most important and common annotation for caching the requests. If developers annotate a method with <code>@Cacheable<\/code> annotation and multiple requests are received by the application, then this annotation will not execute the method multiple times, instead, it will send the result from the cached storage. Here is an example:\n<pre class=\"brush:java; wrap-lines:false;\">@Cacheable(value=\"cities\")\npublic List findAllCity(){\nreturn (List) cityRepository.findAll();\n}\n<\/pre>\n<\/li>\n<li><strong>CachePut<\/strong>: The <code>@CachePut<\/code> annotation helps for updating the cache with the latest execution without stopping the method execution. The primary difference between the <code>@Cacheable<\/code> and the <code>@CachePut<\/code> annotation is that <code>@Cacheable<\/code> will skip running the method whereas <code>@CachePut<\/code> will actually run the method and then put its results in the cache. Here is an example:\n<pre class=\"brush:java; wrap-lines:false;\">@CachePut(value=\"cities\")\npublic List findAllCity(){\nreturn (List) cityRepository.findAll();\n}\n<\/pre>\n<p>Do note, it is <strong>not<\/strong> recommended to use both the annotations for the same method as it will result in the unexpected results<\/p>\n<\/li>\n<li><strong>CacheEvict<\/strong>: The <code>@CacheEvict<\/code> annotation is used for removing a single cache or clearing the entire cache from the cache storage so that fresh values can be loaded into the cache again. If developers set the <code>allEntries=true<\/code>, then the entire cache will be cleared. Here is an example:\n<pre class=\"brush:java; wrap-lines:false;\">@CacheEvict(value=\"cities\", allEntries=true)\npublic List findAllCity(){\nreturn (List) cityRepository.findAll();\n}\n<\/pre>\n<\/li>\n<li><strong>Caching<\/strong>: What if developers want to use the multiple annotations of the same type for caching a method? The <code>@Caching<\/code> annotation is used for grouping the multiple annotations of the same type together when one annotation is not sufficient for the specifying the suitable condition. For e.g., Developers can put the multiple <code>@CacheEvict<\/code> or <code>@CachePut<\/code> annotations inside the <code>@Caching<\/code> annotation to narrow down the conditions as they need. Here is an example:\n<pre class=\"brush:java; wrap-lines:false;\">@Caching(evict = {@CacheEvict(\"cities\"), @CacheEvict(value=\"city-list\", key=\"#city.name\") })\npublic List findAllCity(){\n\treturn (List) cityRepository.findAll();\n}\n<\/pre>\n<\/li>\n<\/ul>\n<h3 class=\"wp-block-heading\" id=\"h-1-3-ehcache\"><a name=\"ehCacheIntroduction\"><\/a>1.3 EhCache<\/h3>\n<p><strong>Ehcache<\/strong> is one of the most popular caching implementation available as the open source caching implementation. This is located under the package: <code>org.springframework.cache.ehcache<\/code>. To use it, developers simply need to declare the appropriate <code>CacheManager<\/code>. Here is an example:<\/p>\n<pre class=\"wp-block-preformatted brush:xml; wrap-lines:false;\">&lt;bean id=\"cacheManager\" class=\"org.springframework.cache.ehcache.EhcacheCacheManager\" p:cache-manager=\"ehcache\"\/&gt;\n\n&lt;!-- Ehcache Library Setup --&gt;\n&lt;bean id=\"ehcache\" class=\"org.springframework.cache.ehcache.EhCacheManagerFactoryBean\" p:config-location=\"ehcache.xml\"\/&gt;\n<\/pre>\n<p>Do note, <code>ehcache.xml<\/code> should be present in the classpath or any other location where the spring application can load it without any issues. This configuration file provides details about the cache size, file name etc.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>Now, open up the Eclipse IDE and let\u2019s see how to implement the cache in the spring framework!<\/p>\n<h2 class=\"wp-block-heading\" id=\"h-2-spring-caching-tutorial\"><a name=\"springCacheTutorial\"><\/a>2. Spring Caching Tutorial<\/h2>\n<h3 class=\"wp-block-heading\" id=\"h-2-1-tools-used\"><a name=\"toolsUsed\"><\/a>2.1 Tools Used<\/h3>\n<p>We are using Eclipse Kepler SR2, JDK 8, MySQL and Maven. Having said that, we have tested the code against JDK 1.7 and it works well.<\/p>\n<h3 class=\"wp-block-heading\" id=\"h-2-2-project-structure\"><a name=\"projectStructure\"><\/a>2.2 Project Structure<\/h3>\n<p>Firstly, let\u2019s review the final project structure, in case you are confused about where you should create the corresponding files or folder later!<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-project-guide-1.jpg\"><img decoding=\"async\" width=\"240\" height=\"280\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-project-guide-1.jpg\" alt=\"spring Caching Application Structure\" class=\"wp-image-50459\"\/><\/a><figcaption>Fig. 1: Spring Cache Application Structure<\/figcaption><\/figure>\n<\/div>\n<h3 class=\"wp-block-heading\" id=\"h-2-3-project-creation\"><a name=\"projectCreation\"><\/a>2.3 Project Creation<\/h3>\n<p>This section will demonstrate on how to create a Java-based Maven project with Eclipse. In Eclipse IDE, go to <code>File -&gt; New -&gt; Maven Project<\/code>.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-1.jpg\"><img decoding=\"async\" width=\"651\" height=\"603\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-1.jpg\" alt=\"spring caching - Create Maven Project\" class=\"wp-image-50460\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-1.jpg 651w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-1-300x278.jpg 300w\" sizes=\"(max-width: 651px) 100vw, 651px\" \/><\/a><figcaption>Fig. 2: Create Maven Project<\/figcaption><\/figure>\n<\/div>\n<p>In the New Maven Project window, it will ask you to select the project location. By default, &#8216;Use default workspace location&#8217; will be selected. Select the &#8216;Create a simple project (skip archetype selection)&#8217; checkbox and just click on next button to proceed.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-2.jpg\"><img decoding=\"async\" width=\"804\" height=\"541\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-2.jpg\" alt=\"spring caching - Project Details\" class=\"wp-image-50461\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-2.jpg 804w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-2-300x202.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-2-768x517.jpg 768w\" sizes=\"(max-width: 804px) 100vw, 804px\" \/><\/a><figcaption>Fig. 3: Project Details<\/figcaption><\/figure>\n<\/div>\n<p>It will ask you to \u2018Enter the group and the artifact id for the project&#8217;. We will input the details as shown in the below image. The version number will be by default: <code>0.0.1-SNAPSHOT<\/code>.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-3.jpg\"><img decoding=\"async\" width=\"598\" height=\"544\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-3.jpg\" alt=\"spring caching - Archetype Parameters\" class=\"wp-image-50462\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-3.jpg 598w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-3-300x273.jpg 300w\" sizes=\"(max-width: 598px) 100vw, 598px\" \/><\/a><figcaption>Fig. 4: Archetype Parameters<\/figcaption><\/figure>\n<\/div>\n<p>Click on Finish and the creation of a maven project is completed. If you observe, it has downloaded the maven dependencies and a <code>pom.xml<\/code> file will be created. It will have the following code:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>pom.xml<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:xml; wrap-lines:false;\">&lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt;\n\t&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\n\t&lt;groupId&gt;SpringCache&lt;\/groupId&gt;\n\t&lt;artifactId&gt;SpringCache&lt;\/artifactId&gt;\n\t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\n\t&lt;packaging&gt;jar&lt;\/packaging&gt;\n&lt;\/project&gt;\n<\/pre>\n<p>We can start adding the dependencies that developers want like Spring Core, Spring Context, EhCache etc. Let\u2019s start building the application!<\/p>\n<h2 class=\"wp-block-heading\" id=\"h-3-application-building\"><a name=\"applicationBuilding\"><\/a>3. Application Building<\/h2>\n<p>Below are the steps involved in developing this application.<\/p>\n<h3 class=\"wp-block-heading\" id=\"h-3-1-maven-dependencies\"><a name=\"mavenDependencies\"><\/a>3.1 Maven Dependencies<\/h3>\n<p>Here we will specify the required dependencies and the rest dependencies will be automatically resolved by Maven. The <strong>updated<\/strong> file will have the following code:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>pom.xml<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:xml; wrap-lines:false;\">&lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n\txsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt;\n\t&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\n\t&lt;groupId&gt;SpringCache&lt;\/groupId&gt;\n\t&lt;artifactId&gt;SpringCache&lt;\/artifactId&gt;\n\t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\n\t&lt;packaging&gt;jar&lt;\/packaging&gt;\n\t&lt;dependencies&gt;\n\t\t&lt;!-- Spring Framework Dependencies --&gt;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;org.springframework&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;spring-core&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;4.3.0.RELEASE&lt;\/version&gt;\n\t\t&lt;\/dependency&gt;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;org.springframework&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;spring-context&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;4.3.0.RELEASE&lt;\/version&gt;\n\t\t&lt;\/dependency&gt;\n\t\t&lt;!-- Spring Framework Dependency For EhCache &amp; Others --&gt;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;org.springframework&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;spring-context-support&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;4.1.4.RELEASE&lt;\/version&gt;\n\t\t&lt;\/dependency&gt;\n\t\t&lt;!-- EhCache Dependency --&gt;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;net.sf.ehcache&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;ehcache&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;2.10.2.2.21&lt;\/version&gt;\n\t\t&lt;\/dependency&gt;\n\t&lt;\/dependencies&gt;\n\t&lt;build&gt;\n\t\t&lt;finalName&gt;${project.artifactId}&lt;\/finalName&gt;\n\t&lt;\/build&gt;\n&lt;\/project&gt;<\/pre>\n<h3 class=\"wp-block-heading\" id=\"h-3-2-java-class-creation\"><a name=\"javaClassCreation\"><\/a>3.2 Java Class Creation<\/h3>\n<p>Let\u2019s create the required Java files. Right-click on <code>src\/main\/java<\/code> folder, <code>New -&gt; Package<\/code>.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-4.jpg\"><img decoding=\"async\" width=\"672\" height=\"656\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-4.jpg\" alt=\"spring cache - Java Package Creation\" class=\"wp-image-50463\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-4.jpg 672w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-4-300x293.jpg 300w\" sizes=\"(max-width: 672px) 100vw, 672px\" \/><\/a><figcaption>Fig. 5: Java Package Creation<\/figcaption><\/figure>\n<\/div>\n<p>A new pop window will open where we will enter the package name as: <code>com.jcg.spring.cache<\/code>.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-5.jpg\"><img decoding=\"async\" width=\"517\" height=\"427\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-5.jpg\" alt=\"spring cache - Java Package Name (com.jcg.spring.cache)\" class=\"wp-image-50464\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-5.jpg 517w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-5-300x248.jpg 300w\" sizes=\"(max-width: 517px) 100vw, 517px\" \/><\/a><figcaption>Fig. 6: Java Package Name (com.jcg.spring.cache)<\/figcaption><\/figure>\n<\/div>\n<p>Once the package is created in the application, we will need to create the Model, the Cache Configuration, and the Implementation classes. Right-click on the newly created package: <code>New -&gt; Class<\/code>.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-6.jpg\"><img decoding=\"async\" width=\"675\" height=\"698\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-6.jpg\" alt=\"Fig. 7: Java Class Creation\" class=\"wp-image-50465\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-6.jpg 675w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-6-290x300.jpg 290w\" sizes=\"(max-width: 675px) 100vw, 675px\" \/><\/a><figcaption>Fig. 7: Java Class Creation<\/figcaption><\/figure>\n<\/div>\n<p>A new pop window will open and enter the file name as: <code>Employee<\/code>. The model class will be created inside the package: <code>com.jcg.spring.cache<\/code>.[ulp id=&#8217;7POIYxRf1FUtPpmL&#8217;]<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-7.jpg\"><img decoding=\"async\" width=\"535\" height=\"631\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-7.jpg\" alt=\"Fig. 8: Java Class (Employee.java)\" class=\"wp-image-50466\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-7.jpg 535w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-7-254x300.jpg 254w\" sizes=\"(max-width: 535px) 100vw, 535px\" \/><\/a><figcaption>Fig. 8: Java Class (Employee.java)<\/figcaption><\/figure>\n<\/div>\n<p>Repeat the step (i.e. Fig. 7) and enter the filename as: <code>SpringConfig<\/code>. The cache configuration class enables the caching with the help of <code>@EnableCaching<\/code> tag and declares the <code>EhCacheCacheManager<\/code>.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-8.jpg\"><img decoding=\"async\" width=\"533\" height=\"631\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-8.jpg\" alt=\"Fig. 9: Java Class (SpringConfig.java)\" class=\"wp-image-50467\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-8.jpg 533w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-8-253x300.jpg 253w\" sizes=\"(max-width: 533px) 100vw, 533px\" \/><\/a><figcaption>Fig. 9: Java Class (SpringConfig.java)<\/figcaption><\/figure>\n<\/div>\n<p>Again, repeat the step (i.e. Fig. 7) and enter the filename as: <code>AppMain<\/code>. The implementation class will get the bean from the cache configuration and calls the <code>getEmployeeName()<\/code> method of the <code>Employee<\/code> bean class.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-9.jpg\"><img decoding=\"async\" width=\"535\" height=\"631\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-9.jpg\" alt=\"Fig. 10: Java Class (AppMain.java)\" class=\"wp-image-50468\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-9.jpg 535w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-guide-9-254x300.jpg 254w\" sizes=\"(max-width: 535px) 100vw, 535px\" \/><\/a><figcaption>Fig. 10: Java Class (AppMain.java)<\/figcaption><\/figure>\n<\/div>\n<h4 class=\"wp-block-heading\" id=\"h-3-2-1-implementation-of-model-class\">3.2.1 Implementation of Model Class<\/h4>\n<p>In this example, we have a cache named as <code>employeeCache<\/code> in <code>ehcache.xml<\/code> and this same name is provided in the <code>@Cacheable<\/code> annotation. Spring framework will hit the <code>getEmployeeName()<\/code> method for the first time and the result of this method will be cached and for the same argument value, the framework will not hit the method every time. Once the cache is expired, then the spring framework will hit the method again for the same argument value.<\/p>\n<p>Add the following code to it:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Employee.java<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:java; wrap-lines:false;\">package com.jcg.spring.cache;\n\nimport org.springframework.cache.annotation.Cacheable;\n\npublic class Employee  {\n\n\tprivate String emp_name = \"\";\n\n\t\/\/This 'employeeCache' Is Declared In The 'ehcache.xml'\n\t@Cacheable(value=\"employeeCache\") \n\tpublic String getEmployeeName(int emp_id) {\t\t\n\t\tif(emp_id == 101) {\n\t\t\temp_name = \"Java Code Geek\";\t\t\t\n\t\t} else {\n\t\t\temp_name = \"Daniel Atlas\";\n\t\t}\n\t\treturn emp_name;\n\t}\n}<\/pre>\n<h4 class=\"wp-block-heading\" id=\"h-3-2-2-implementation-of-cache-configuration-class\">3.2.2 Implementation of Cache Configuration Class<\/h4>\n<p>This class will be annotated with <code>@EnableCaching<\/code> annotation and developers need to create the bean for <code>EhCacheManagerFactoryBean<\/code> and <code>EhCacheCacheManager<\/code> class. Add the following code to it:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>SpringConfig.java<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:java; wrap-lines:false;\">package com.jcg.spring.cache;\n\nimport org.springframework.beans.factory.annotation.Configurable;\nimport org.springframework.cache.CacheManager;\nimport org.springframework.cache.annotation.EnableCaching;\nimport org.springframework.cache.ehcache.EhCacheCacheManager;\nimport org.springframework.cache.ehcache.EhCacheManagerFactoryBean;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.core.io.ClassPathResource;\n\n@Configurable @EnableCaching\npublic class SpringConfig {\n\n\tprivate static EhCacheManagerFactoryBean cacheManagerfactoryBean;\n\n\t@Bean\n\tpublic Employee getEmployee(){\n\t\treturn  new Employee();\t\n\t}\n\n\t@Bean\n\tpublic CacheManager getEhCacheManager() {\n\t\treturn  new EhCacheCacheManager(getEhCacheFactory().getObject());\n\t}\n\n\t@Bean\n\tpublic EhCacheManagerFactoryBean getEhCacheFactory() {\n\t\tcacheManagerfactoryBean = new EhCacheManagerFactoryBean();\n\t\tcacheManagerfactoryBean.setShared(true);\n\t\tcacheManagerfactoryBean.setConfigLocation(new ClassPathResource(\"ehcache.xml\"));\t\t\n\t\treturn cacheManagerfactoryBean;\n\t}\n}<\/pre>\n<h4 class=\"wp-block-heading\" id=\"h-3-2-3-implementation-of-utility-class\">3.2.3 Implementation of Utility Class<\/h4>\n<p>This class will get the bean from the cache configuration and calls the <code>getEmployeeName()<\/code> method of the <code>Employee<\/code> bean class to display the values. Add the following code to it:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AppMain.java<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:java; wrap-lines:false;\">package com.jcg.spring.cache;\n\nimport org.springframework.context.annotation.AnnotationConfigApplicationContext;\n\npublic class AppMain {\n\n\tstatic int emp_id;\n\n\tpublic static void main(String[] args) {\n\n\t\tAnnotationConfigApplicationContext  contextObj = new AnnotationConfigApplicationContext ();\n\t\tcontextObj.register(SpringConfig.class);\n\t\tcontextObj.refresh();\n\n\t\tEmployee empObj = (Employee) contextObj.getBean(Employee.class);\n\n\t\t\/\/ Calling 'getEmployeeName' Method For The First Time.\n\t\temp_id = 101;\n\t\tSystem.out.println(\"Fetching Employee Name With Id?= \" + emp_id);\n\t\tSystem.out.println(\"Name?= \" + empObj.getEmployeeName(emp_id));\n\n\t\t\/\/ Calling 'getEmployeeName' Method For The Second Time &amp; This Time Method Will Not Execute As Result Will Be Fetched From Cache.\n\t\tSystem.out.println(\"\\n!--- Again Fetching Employee Name With Id?= \" + emp_id + \". Result Will Be Fetched From The Cache ---!\");\n\t\tSystem.out.println(\"Name?= \" + empObj.getEmployeeName(emp_id));\n\n\t\t\/\/ Calling 'getEmployeeName' Method For The Third Time With A Different Value.\n\t\temp_id = 102;\n\t\tSystem.out.println(\"\\nFetching Employee Name With Id?= \" + emp_id);\n\t\tSystem.out.println(\"Name?= \" + empObj.getEmployeeName(emp_id));\n\n\t\t\/\/ Closing The Spring Context.\n\t\tcontextObj.close();\n\t}\n}<\/pre>\n<h3 class=\"wp-block-heading\" id=\"h-3-3-ehcache-configuration-file\"><a name=\"ehCacheConfigurationFile\"><\/a>3.3 EhCache Configuration File<\/h3>\n<p>To configure the ehcache, developers need to implement a configuration file i.e. <code>ehcache.xml<\/code> which provides the cache implementations to the spring application. Right-click on <code>SpringCache\/src\/main\/resources<\/code> folder, <code>New -&gt; Other<\/code>.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-xml-guide-1.jpg\"><img decoding=\"async\" width=\"707\" height=\"656\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-xml-guide-1.jpg\" alt=\"Fig. 11: XML File Creation\" class=\"wp-image-50469\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-xml-guide-1.jpg 707w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-xml-guide-1-300x278.jpg 300w\" sizes=\"(max-width: 707px) 100vw, 707px\" \/><\/a><figcaption>Fig. 11: XML File Creation<\/figcaption><\/figure>\n<\/div>\n<p>A new pop window will open and select the wizard as an <code>XML<\/code> file.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-xml-guide-2.jpg\"><img decoding=\"async\" width=\"519\" height=\"495\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-xml-guide-2.jpg\" alt=\"Fig. 12: Wizard Selection\" class=\"wp-image-50470\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-xml-guide-2.jpg 519w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-xml-guide-2-300x286.jpg 300w\" sizes=\"(max-width: 519px) 100vw, 519px\" \/><\/a><figcaption>Fig. 12: Wizard Selection<\/figcaption><\/figure>\n<\/div>\n<p>Again, a pop-up window will open. Verify the parent folder location as: <code>SpringCache\/src\/main\/resources<\/code> and enter the file name as: <code>ehcache.xml<\/code>. Click Finish.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-xml-guide-3.jpg\"><img decoding=\"async\" width=\"518\" height=\"597\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-xml-guide-3.jpg\" alt=\"Fig. 13: ehcache.xml\" class=\"wp-image-50471\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-xml-guide-3.jpg 518w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-xml-guide-3-260x300.jpg 260w\" sizes=\"(max-width: 518px) 100vw, 518px\" \/><\/a><figcaption>Fig. 13: ehcache.xml<\/figcaption><\/figure>\n<\/div>\n<p>Once the <code>XML<\/code> file is created, we will add the following code to it:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>ehcache.xml<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:xml; wrap-lines:false;\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;ehcache xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"ehcache.xsd\" updateCheck=\"true\" monitoring=\"autodetect\" dynamicConfig=\"true\"&gt;\n    &lt;cache name=\"employeeCache\" maxEntriesLocalHeap=\"5000\" maxEntriesLocalDisk=\"1000\" eternal=\"false\" diskSpoolBufferSizeMB=\"20\" timeToIdleSeconds=\"200\" timeToLiveSeconds=\"500\" memoryStoreEvictionPolicy=\"LFU\" transactionalMode=\"off\"&gt;\n        &lt;persistence strategy=\"localTempSwap\" \/&gt;\n    &lt;\/cache&gt;\n&lt;\/ehcache&gt;\n<\/pre>\n<p><strong>Notes<\/strong>:<\/p>\n<p>Let\u2019s look at some basic details:<\/p>\n<ul class=\"wp-block-list\">\n<li>Here, a maximum of 5000 elements will be cached in the memory and after that, they will overflow to the local disk<\/li>\n<li>Any element will expire if it is idle for more than 200 seconds and it will be alive for more than 500 seconds<\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\" id=\"h-4-run-the-application\"><a name=\"projectRun\"><\/a>4. Run the Application<\/h2>\n<p>To run the application, Right click on the <code>AppMain<\/code> class, <code>Run As -&gt; Java Application<\/code>. Developers can debug the example and see what happens after every step. Enjoy!<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-deploy-guide-1.jpg\"><img decoding=\"async\" width=\"806\" height=\"698\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-deploy-guide-1.jpg\" alt=\"Fig. 14: Run the Application\" class=\"wp-image-50472\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-deploy-guide-1.jpg 806w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-deploy-guide-1-300x260.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-cache-example-project-deploy-guide-1-768x665.jpg 768w\" sizes=\"(max-width: 806px) 100vw, 806px\" \/><\/a><figcaption>Fig. 14: Run the Application<\/figcaption><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\" id=\"h-5-project-demo\"><a name=\"projectDemo\"><\/a>5. Project Demo<\/h2>\n<p>The application will hit the <code>getEmployeeName()<\/code> method by passing the <code>emp_id<\/code> value as an argument for the first time. But for the next hit, if the developers pass the same argument value, the application will fetch the result from the cache but not by running the action method. The code shows the following status as output.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-caching-example-project-demo-guide-1.jpg\"><img decoding=\"async\" width=\"849\" height=\"204\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-caching-example-project-demo-guide-1.jpg\" alt=\"Fig. 15: Application Output\" class=\"wp-image-50473\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-caching-example-project-demo-guide-1.jpg 849w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-caching-example-project-demo-guide-1-300x72.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/spring-caching-example-project-demo-guide-1-768x185.jpg 768w\" sizes=\"(max-width: 849px) 100vw, 849px\" \/><\/a><figcaption>Fig. 15: Application Output<\/figcaption><\/figure>\n<\/div>\n<p>That\u2019s all for this post. Happy Learning!<\/p>\n<h2 class=\"wp-block-heading\" id=\"h-6-summary\"><a name=\"projectConclusion\"><\/a>6. Summary<\/h2>\n<p>This tutorial uses a simple example to illustrate the functionality of the caching in the spring framework and helps developers understand the basic configuration required to achieve this. That\u2019s all for this tutorial and I hope this article served you whatever you were looking for.<\/p>\n<h2 class=\"wp-block-heading\" id=\"h-7-download-the-eclipse-project\"><a name=\"projectDownload\"><\/a>7. Download the Eclipse Project<\/h2>\n<p>This was an example of Spring Cache.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>You can download the full source code of this example here: <a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/09\/SpringCache.zip\" target=\"_blank\" rel=\"noopener\"><strong>Spring Caching Tutorial<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will show you the Spring caching through examples. Caching is crucial for today\u2019s high-performance hungry applications. Adding Caching in a spring-based application is rather trivial. In this tutorial, we will show you how to enable data caching in a spring application by integrating the application with the popular EhCache framework. Table &hellip;<\/p>\n","protected":false},"author":119,"featured_media":1248,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52],"tags":[899,1054],"class_list":["post-50455","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spring","tag-cache","tag-spring"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Spring Caching Tutorial - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"In this article, we will show you the Spring caching through examples. Caching is crucial for today\u2019s high-performance hungry applications. Adding Caching\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Spring Caching Tutorial - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"In this article, we will show you the Spring caching through examples. Caching is crucial for today\u2019s high-performance hungry applications. Adding Caching\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2017-09-20T08:00:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-02-24T13:05:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/spring-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Yatin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Yatin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/\"},\"author\":{\"name\":\"Yatin\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13\"},\"headline\":\"Spring Caching Tutorial\",\"datePublished\":\"2017-09-20T08:00:48+00:00\",\"dateModified\":\"2021-02-24T13:05:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/\"},\"wordCount\":1737,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/spring-logo.jpg\",\"keywords\":[\"cache\",\"spring\"],\"articleSection\":[\"spring\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/\",\"name\":\"Spring Caching Tutorial - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/spring-logo.jpg\",\"datePublished\":\"2017-09-20T08:00:48+00:00\",\"dateModified\":\"2021-02-24T13:05:29+00:00\",\"description\":\"In this article, we will show you the Spring caching through examples. Caching is crucial for today\u2019s high-performance hungry applications. Adding Caching\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/spring-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/spring-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Development\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/enterprise-java\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"spring\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/enterprise-java\/spring\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Spring Caching Tutorial\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13\",\"name\":\"Yatin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg\",\"caption\":\"Yatin\"},\"description\":\"An experience full-stack engineer well versed with Core Java, Spring\/Springboot, MVC, Security, AOP, Frontend (Angular &amp; React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).\",\"sameAs\":[\"https:\/\/www.javacodegeeks.com\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/yatin-batra\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Spring Caching Tutorial - Java Code Geeks","description":"In this article, we will show you the Spring caching through examples. Caching is crucial for today\u2019s high-performance hungry applications. Adding Caching","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/","og_locale":"en_US","og_type":"article","og_title":"Spring Caching Tutorial - Java Code Geeks","og_description":"In this article, we will show you the Spring caching through examples. Caching is crucial for today\u2019s high-performance hungry applications. Adding Caching","og_url":"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2017-09-20T08:00:48+00:00","article_modified_time":"2021-02-24T13:05:29+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/spring-logo.jpg","type":"image\/jpeg"}],"author":"Yatin","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Yatin","Est. reading time":"13 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/"},"author":{"name":"Yatin","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13"},"headline":"Spring Caching Tutorial","datePublished":"2017-09-20T08:00:48+00:00","dateModified":"2021-02-24T13:05:29+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/"},"wordCount":1737,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/spring-logo.jpg","keywords":["cache","spring"],"articleSection":["spring"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/","url":"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/","name":"Spring Caching Tutorial - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/spring-logo.jpg","datePublished":"2017-09-20T08:00:48+00:00","dateModified":"2021-02-24T13:05:29+00:00","description":"In this article, we will show you the Spring caching through examples. Caching is crucial for today\u2019s high-performance hungry applications. Adding Caching","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/spring-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/spring-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/spring-caching-tutorial\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/enterprise-java\/"},{"@type":"ListItem","position":4,"name":"spring","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/enterprise-java\/spring\/"},{"@type":"ListItem","position":5,"name":"Spring Caching Tutorial"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13","name":"Yatin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg","caption":"Yatin"},"description":"An experience full-stack engineer well versed with Core Java, Spring\/Springboot, MVC, Security, AOP, Frontend (Angular &amp; React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).","sameAs":["https:\/\/www.javacodegeeks.com"],"url":"https:\/\/examples.javacodegeeks.com\/author\/yatin-batra\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/50455","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/119"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=50455"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/50455\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/1248"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=50455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=50455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=50455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}