Java 实现缓存框架(ehcache)以在服务器启动期间缓存查找代码和位置下拉列表值

Java 实现缓存框架(ehcache)以在服务器启动期间缓存查找代码和位置下拉列表值,java,spring,spring-boot,hibernate,caching,Java,Spring,Spring Boot,Hibernate,Caching,我想从数据库中加载查找代码和位置数据 当应用程序启动时,使用SpringEhcache将数据存储到缓存中,即 当服务器在调用任何其他方法之前启动时。将来 将添加更多的下拉列表。所以应该有一个通用的方法 根据下拉列表的条件缓存任何传入的数据 数据 有一个实体、存储库和服务已经为其编写 查找代码和位置 为了实现缓存框架,我编写了以下内容: ehcache.xml 应用程序属性 spring.jpa.properties.hibernate.cache.use_second_level_cache

我想从数据库中加载查找代码和位置数据 当应用程序启动时,使用SpringEhcache将数据存储到缓存中,即 当服务器在调用任何其他方法之前启动时。将来 将添加更多的下拉列表。所以应该有一个通用的方法 根据下拉列表的条件缓存任何传入的数据 数据

有一个实体、存储库和服务已经为其编写 查找代码和位置

为了实现缓存框架,我编写了以下内容:

ehcache.xml

应用程序属性

 spring.jpa.properties.hibernate.cache.use_second_level_cache = false
 spring.jpa.properties.hibernate.cache.use_query_cache = false
 spring.jpa.properties.hibernate.cache.region.factory_class =
 org.hibernate.cache.ehcache.EhCacheRegionFactory
 spring.jpa.properties.hibernate.cache.provider_class =
 org.hibernate.cache.EhCacheProvider
 spring.jpa.properties.hibernate.cache.use_structured_entries = true
 spring.jpa.properties.hibernate.cache.region_prefix =
 spring.jpa.properties.hibernate.cache.provider_configuration_file_resource_path
 = ehcache.xml spring.jpa.properties.hibernate.cache.use_second_level_cache
并在pom.xml中使用hibernate ehcache jar

WebConfig.java

 @Configuration public class WebConfig implements
 ServletContextInitializer{
 
 @Autowired CustomCache cache;
 
 @Override public void onStartup ( ServletContext servletContext)
 throws ServletException{
 
 cache.loadCache();
 
 }
 public class CustomCache {
 
 @Autowired private LookupCodeService lkupSer;
 
 @Autowired private LocationService locSer;
 
 public void loadCache(){
 
 List<LookupCode> lkup = lkupServ.getDropdownValues();
 
 List<Location> locat = locSer.getDropdownValues();
 }
CustomCache.java

 @Configuration public class WebConfig implements
 ServletContextInitializer{
 
 @Autowired CustomCache cache;
 
 @Override public void onStartup ( ServletContext servletContext)
 throws ServletException{
 
 cache.loadCache();
 
 }
 public class CustomCache {
 
 @Autowired private LookupCodeService lkupSer;
 
 @Autowired private LocationService locSer;
 
 public void loadCache(){
 
 List<LookupCode> lkup = lkupServ.getDropdownValues();
 
 List<Location> locat = locSer.getDropdownValues();
 }
公共类自定义缓存{
@自动连线专用LookupCodeService lkupSer;
@自动有线专用定位服务;
公共void loadCache(){
List lkup=lkupServ.getDropdownValues();
List locat=locSer.getDropdownValues();
}
因此,在这里使用
loadCache()
方法,而不是调用每个单独的 服务应该是自动的,无论创建什么服务 它应该被自动缓存,所以应该有一个通用的方法 根据 下拉数据


如何实现这一点?

您要使用的服务有一个通用方法。请为该方法定义一个接口:

   interface ProvidesDropdownValues<T> { 
     List<T> getDropdownValues(); 
   }
我建议加载代码只在生产应用程序中运行。这就是为什么将其与常规缓存逻辑分开是有意义的。对于测试单个服务,您不希望加载所有内容。开发人员的启动时间应该很快


免责声明:我不是一个重度Spring用户,因此详细信息可能是错误的,但基本方法应该可行。

您想要使用的服务有一个通用方法。为该方法定义一个接口:

   interface ProvidesDropdownValues<T> { 
     List<T> getDropdownValues(); 
   }
我建议加载代码只在生产应用程序中运行。这就是为什么将其与常规缓存逻辑分开是有意义的。对于测试单个服务,您不希望加载所有内容。开发人员的启动时间应该很快


免责声明:我不是一个重度Spring用户,因此详细信息可能是错误的,但基本方法应该是可行的。

请正确设置问题的格式(代码应该是代码等)。不要对所有问题都使用引号。目前,您的问题无法阅读。请正确设置问题的格式(代码应该是代码等)不要什么都引用。目前,你的问题无法阅读。