I want to load the data from database into cache memory using Spring ehCache when the application starts i.e when the server starts before any other method is called. I dont want to use a constructor. Please help me.
2 Answers
This is exactly what the BootstrapCacheLoader
will do for you.
Have a look at the following documentation entry.
It seems this is fully integrated in the Spring Ehcache bridge - see here
In short the steps to get there are:
- Determine how you will know what needs to be loaded at startup - that is your set of keys
- Implement your own
BootstrapCacheLoader
that will use the set to load eagerly all entries, async or sync at cache initialisation time. - Wire it using a
BootstrapCacheLoaderFactory
through XML (Ehcache) or even directly (Spring)

- 13,661
- 2
- 34
- 43
-
how to check whether the data has been copied fram data source to ehChache. Where will the cache get stored? inside jvm? how to check what data is stored in ehCache. – Vudutala Rahulbharadwaj Nov 19 '14 at 06:37
As @LouisJacomet mentioned you need to implement BootstrapCacheLoader, this implementation is what will do your preloading. Since you are using Spring you will create a bean of the implemented class and call EhCacheFactoryBean.setBootstrapCacheLoader so that the cache factory is aware of the loader.
Here is a link with a complete implementation. https://javaglobin.wordpress.com/2013/11/13/declarative-caching-with-spring/
Have a look at MyBootstrapCacheLoaderFactory and also note that there is a EhCacheFactoryBean bean that is called ehCacheFactory.

- 12,013
- 5
- 36
- 59
-
-
Yeah, the javaglobin entry is for Spring 3.1, and presupposes an ehcache.xml file is being used. How is it supposed to be done for Spring 4 with java config (and thus no ehcache.xml)? Seems to me `EhCacheManagerFactoryBean` does nearly nothing in a java config-only approach. Is the name `ehCacheFactory` "special"? – ben3000 Sep 21 '15 at 02:37