How to load data from database to Ehcache when the application starts

99封情书 提交于 2019-12-18 18:36:09

问题


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.


回答1:


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:

  1. Determine how you will know what needs to be loaded at startup - that is your set of keys
  2. Implement your own BootstrapCacheLoader that will use the set to load eagerly all entries, async or sync at cache initialisation time.
  3. Wire it using a BootstrapCacheLoaderFactory through XML (Ehcache) or even directly (Spring)



回答2:


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.



来源:https://stackoverflow.com/questions/26927044/how-to-load-data-from-database-to-ehcache-when-the-application-starts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!