Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring cache vs. cachemanger

Tags:

spring

ehcache

The following sample from the Spring manual confuses the heck out of me.

<bean id="cacheManager" 
      class="org.springframework.cache.ehcache.EhCacheCacheManager"
      p:cache-manager-ref="ehcache"/>

<!-- Ehcache library setup -->
<bean id="ehcache" 
      class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" 
      p:config-location="ehcache.xml"/>

The naming convention mandates that EhCacheManagerFactoryBean produces a cache manager, more precisely it's a net.sf.ehcache.CacheManager instance. Yet, the bean is called ehcache not ehcachemanager. The actual cacheManager, however, references this bean.

In prose you could say that one is the Ehcache cache manager while the other is the Spring cache manager (which is backed by the former).

It gets worse if you use the EhCacheFactoryBean:

<bean
  id="myCache"
  class="org.springframework.cache.ehcache.EhCacheFactoryBean">
  <property name="cacheManager">
    <ref local="ehcache" />
  </property>

There's a property called cacheManager which references a bean called ehcache.

Did I misunderstand anything or is it really that confusing? Bad design or just bad naming in the example?

like image 314
Marcel Stör Avatar asked Jan 07 '13 22:01

Marcel Stör


People also ask

What is CacheManager in Spring?

Interface CacheManagerSpring's central cache manager SPI. Allows for retrieving named Cache regions.

What is difference between EhCache and Redis cache?

You can think Redis as a shared data structure, while Ehcache is a memory block storing serialized data objects. This is the main difference. Redis as a shared data structure means you can put some predefined data structure (such as String, List, Set etc) in one language and retrieve it in another language.

What is the use of Spring cache?

The Spring Framework provides support for transparently adding caching to an application. At its core, the abstraction applies caching to methods, thus reducing the number of executions based on the information available in the cache. The caching logic is applied transparently, without any interference to the invoker.


1 Answers

Spring framework recently introduced caching abstraction with org.springframework.cache.CacheManager central interface. This interface has few built-in implementations, including:

  • ConcurrentMapCacheManager
  • EhCacheCacheManager
  • NoOpCacheManager
  • SimpleCacheManager

This design allows you to switch caching library without touching application code. As you can see one of these built-in implementations is backed by EhCache. However notice that EhCacheCacheManager is just a bridge between Spring caching abstraction and EhCache library. Thus it needs existing net.sf.ehcache.CacheManager. You can either create an instance of this cache manager yourself or take advantage of existing factory bean, namely EhCacheManagerFactoryBean.

I understand it's confusing because of overlapping names, but it should be clear from the above which classes come from which library and why are they used.

like image 86
Tomasz Nurkiewicz Avatar answered Sep 18 '22 06:09

Tomasz Nurkiewicz