a demo project to demonstrate the configuration/usage of ehCache in springboot projects
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
.gitignore
README.md
mvnw
mvnw.cmd
pom.xml

README.md

springboot-ehcache-demo

A demo project to demonstrate the configurations required to enable ehcache in springboot based rest webservices application.

The following is a sample configuration for ehcache :

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd"
         updateCheck="true"
         monitoring="autodetect"
         dynamicConfig="true">

    <diskStore path="java.io.tmpdir"/>

    <cache name="userCache"
           maxEntriesLocalHeap="10000"
           maxEntriesLocalDisk="1000"
           eternal="false"
           diskSpoolBufferSizeMB="20"
           timeToIdleSeconds="300" timeToLiveSeconds="600"
           memoryStoreEvictionPolicy="LFU"
           transactionalMode="off">
        <persistence strategy="localTempSwap"/>
    </cache>

</ehcache>

Also, the following Bean definitions are injected to the spring container :

    @Bean
    public CacheManager cacheManager() {
        return new EhCacheCacheManager(cacheMangerFactory().getObject());
    }

    @Bean
    public EhCacheManagerFactoryBean cacheMangerFactory() {
        EhCacheManagerFactoryBean bean = new EhCacheManagerFactoryBean();
        bean.setConfigLocation(new ClassPathResource("ehcache.xml"));
        bean.setShared(true);
        return bean;
    }

The above mentioned beans in conjunction with the @Cacheable annotation allows the caching of results. The same can be verified by repeattatively calling the GET API call for user.

As verified from the logs, the first GET request for the user is served from the database but the subsequent requests for the same user are not directed to DB and are served from the cache only.

The project also uses hyperjaxb3 for the entity generation from the xsd schema. The generated classes are annotated with appropriate JPA annotations. There are options to customize the annotations as well. The same is defined in the corresponding bindings.xjb file