The cache configuration

To enable the use of annotation-based configuration, first of all, the Spring Framework has to be registered using the cache namespace. The following configuration can be used to declare the namespace for cache and register the annotation:

<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:cache="http://www.springframework.org/schema/cache" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://www.springframework.org/schema/cache 
    http://www.springframework.org/schema/cache/spring-cache.xsd"> 
  <cache:annotation-driven/> 
</beans> 

Once the registration is done, it's time to provide the configuration to specify the name of the repository using which the results will be cached. We will define the configuration very soon in the sample demo for SimpleCacheManager.

Let's integrate a JDK-based ConcurrentMap repository in our Book application. We will use Ch03_Spring_Hibernate_Integration as the base project for the demonstration. Follow these steps for the integration:

  1. Create a new Java application named Ch03_CacheManager, and add to it the JARs for Spring, JDBC, and Hibernate. You can refer to the Ch03_Spring_Hibernate_Integration application as well.
  2. Create or copy Book.java in the com.packt.ch03.beans package.
  3. Create or copy the BookDAO interface in the com.packt.ch03.dao package, and add to it a method to search a book from the database using ISBN. The signature of the method is shown as follows:
public Book getBook(longISBN); 
  1. Implement the methods in BookDAO_SessionFactory_Cache as we did in BookDAO_SessionFactory.java from the Hibernate application. The method to get the book from the database is as follows:
public Book getBook(longISBN) { 
  // TODO Auto-generated method stub 
  Session session = sessionFactory.openSession(); 
  Transaction transaction = session.beginTransaction(); 
  Book book = null; 
  try { 
    book = session.get(Book.class, ISBN); 
    transaction.commit(); 
    session.close(); 
  } catch (DataAccessException exception) { 
    exception.printStackTrace(); 
  } 
  return book; 
} 

The method will use the repo repository to cache the result.

  1. Copy book.hbm.xml in the classpath.
  2. Add MainBookDAO_Cache.java to the main function to get the data from the database; however, we will purposely fetch the data twice, as shown in the following piece of code:
public static void main(String[] args) { 
  // TODO Auto-generated method stub 
  ApplicationContext context=new 
     ClassPathXmlApplicationContext("connection_new.xml"); 
  BookDAO bookDAO=(BookDAO)   
    context.getBean("bookDAO_sessionFactory"); 
  Book book=bookDAO.getBook(97815674L);    
  System.out.println(book.getBookName()+ 
    "	"+book.getAuthor()); 
  Book book1=bookDAO.getBook(97815674L); 
  System.out.println(book1.getBookName()+ 
    "	"+book1.getAuthor()); 
} 
  1. Before execution, make sure the ISBN we are searching for is already present in the database. The output clearly shows that the query to search the book is executed twice. You will see the screenshot after step 5 further on.

Now, let's configure the Cache manager to cache the result of the search book, as follows:

  1. Annotate the method whose result is to be cached by @Cacheable as follows:
@Cacheable("repo") 
  public Book getBook(longISBN) {// code will go here } 
  1. Configure the cache namespace in connection_new.xml, as already discussed.
  2. Register the annotation-based cache in the connection_new.xml file as follows:
<cache:annotation-driven /> 
  1. Add CacheManager to set the repository as repo, as shown in the following configuration:
<bean id="cacheManager" 
  class="org.springframework.cache.support.SimpleCacheManager"> 
  <property name="caches"> 
   <set> 
    <bean
class="org.springframework.cache.concurrent.ConcurrentMapCache FactoryBean"> <property name="name"value="repo"></property> </bean> </set> </property> </bean>
  1. Execute MainBookDAO_Cache.java without change to get the following output showing that the database has been hit only once. The following screenshot clears the difference in the output with and without cache management:

The console output shows the query has been fired only once even when we searched for the book twice. Don't consider the values, as these are the values of the book whose description we are searching for. The result of the book fetched the first time by getBook() is cached, and used the next time someone asks for the book without heating the database.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.251.154