Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an EhCache instance with Spring... intelligently

Tags:

spring

ehcache

I need to get a specific EhCache instance by name and I'd prefer to autowire if possible. Given the following automatically configured controller, how can I autowire in the cache instance I'm looking for?

@Controller 
public class MyUniqueService {
    ...
}

<beans ...>
    <ctx:component-scan base-package="my.controllers"/>
    <mvc:annotation-driven />
</beans>

How do I configure EhCache in my application context? I don't see any log messages from EhCache about it loading the ehcache.xml file in my /WEB-INF/ directory. How do I make it load it?

How can I integrate EhCache with my Spring application to have it load the ehcache.xml file from my /WEB-INF/ directory and autowire a cache by a given name into my MyUniqueService controller?

like image 516
Naftuli Kay Avatar asked Jul 13 '12 00:07

Naftuli Kay


People also ask

How does spring boot integrate with Ehcache?

In order to integrate Ehcache to our spring boot application, we have to include Ehcache to the classpath by adding maven dependency. Of course! We also should add spring-boot-starter-cache to the dependency list.

How do I know if Ehcache is working in spring boot?

We can use Maven to start this app by running mvn spring-boot:run. Then open up a browser and access the REST service on port 8080. The log message in the square method of NumberService isn't being invoked. This shows us that the cached value is being used.

How do I set up Ehcache?

Ehcache can be configured in two ways: The first way is through Java POJO where all configuration parameters are configured through Ehcache API. The second way is configuration through XML file where we can configure Ehcache according to provided schema definition.


1 Answers

First you need to create a Ehcache CacheManager singleton in you app context like this:

<bean id="myEhCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:my-ehcache.xml"/>
</bean>

Here configLocation is set to load from classpath or use value="/WEB-INF/my-ehcache.xml".

In your controller simply inject the CacheManager instance:

@Controller 
public class MyUniqueService {

    @Resource(name="myEhCacheManager")
    private CacheManager cacheManager;

    ...
}

Alternatively, if you'd like to go the "entirely autowired" route, do:

<bean class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager">
        <bean class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
            <property name="configLocation" value="/WEB-INF/ehcache.xml"/>
        </bean>
    </property>
</bean>

Setup your class like so:

@Controller
public class MyUniqueService { 

    @Autowired
    private org.springframework.cache.CacheManager cacheManager;

    public org.springframework.cache.Cache getUniqueObjectCache() {
        return cacheManager.getCache("uniqueObjectCache");
    }
}

uniqueObjectCache corresponds to this cache instance in your ehcache.xml cache definition:

<cache name="uniqueObjectCache"
       maxElementsInMemory="10000"
       eternal="false"
       timeToIdleSeconds="300"
       timeToLiveSeconds="600"
       memoryStoreEvictionPolicy="LRU"
       transactionalMode="off"/>

There isn't a way to inject an actual cache instance, but as shown above, you can inject a cache manager and use it to get the cache you're interested in.

like image 80
jeha Avatar answered Oct 21 '22 10:10

jeha