Ehcache-based cache

Ehcache is one of the most popular cache providers. Spring allows you to integrate with Ehcache by configuring EhCacheCacheManager in the application. Take for example, the following Java configuration:

    @Bean 
    public CacheManager cacheManager(CacheManager ehCache) { 
      EhCacheCacheManager cmgr = new EhCacheCacheManager(); 
      cmgr.setCacheManager(ehCache); 
      return cmgr; 
    } 
    @Bean  
    public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() { 
      EhCacheManagerFactoryBean eh = new EhCacheManagerFactoryBean(); 
      eh.setConfigLocation(new  
ClassPathResource("resources/ehcache.xml")); return eh; }

In the preceding code, the bean method, cacheManager(), creates an object of EhCacheCacheManager, and set it with the CacheManager of Ehcache. Here, Ehcache's CacheManager is injected into Spring's EhCacheCacheManager. The second bean method, ehCacheManagerFactoryBean(), creates and returns an instance of EhCacheManagerFactoryBean. Because it's a Factory bean, it will return an instance of CacheManager. An XML file, ehcache.xml, has the Ehcache configuration. Let's refer to the following code for ehcache.xml:

    <ehcache> 
       <cache name="accountCache" maxBytesLocalHeap="50m"
timeToLiveSeconds="100"> </cache> </ehcache>

The contents of the ehcache.xml file vary from application to application, but you need to declare, at least, a minimal cache. For example, the following Ehcache configuration declares a cache named accountCache with 50 MB of maximum heap storage and a time-to-live of 100 seconds:

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

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