Java 如何在批量事务期间以编程方式使用ehcache从缓存中清除内存?

Java 如何在批量事务期间以编程方式使用ehcache从缓存中清除内存?,java,hibernate,spring-boot,spring-mvc,ehcache,Java,Hibernate,Spring Boot,Spring Mvc,Ehcache,在数据库中插入更多记录(如50000条)需要花费大量时间。我发现,由于hibernate在其缓存中存储对象,而任何插入或更新都会随着记录数量的增加而花费更多的时间来插入。我需要知道插入时缓存将以什么名称保存,这样我就可以使用@cacheexecute注释逐出缓存。我正在使用ehcache 我发现可以使用@cacheexecute。但我不知道插入缓存将以什么名称保存。我已经在CacheConfiguration类中创建了一个缓存,但我不知道它是否正确。我发现有一种方法可以使用ehcache.xml

在数据库中插入更多记录(如50000条)需要花费大量时间。我发现,由于hibernate在其缓存中存储对象,而任何插入或更新都会随着记录数量的增加而花费更多的时间来插入。我需要知道插入时缓存将以什么名称保存,这样我就可以使用@cacheexecute注释逐出缓存。我正在使用ehcache

我发现可以使用@cacheexecute。但我不知道插入缓存将以什么名称保存。我已经在CacheConfiguration类中创建了一个缓存,但我不知道它是否正确。我发现有一种方法可以使用ehcache.xml设置缓存名称,但我们没有使用它。我正在使用ehcache、hibernate和springmvc

//Setting cache name in CacheConfiguration class:
@Bean
public JCacheManagerCustomizer cacheManagerCustomizer() {
    return cm -> {           
cm.createCache(com.cspl.cashflow.CashflowService.NAME,jcacheConfiguration);
             };
    }

//For loop for inserting:
public static final String NAME = "names";  
 @Cacheable("names")
 public void getNames() {
     logger.info("saving");
    for(int i=1; i<=50000;i++){
        List<Cashflow> cashflowList = new ArrayList<>();
        Cashflow order = new Cashflow(1, "Stark", "Tony", true)         
        cashflowList.add(order);
        cashflowRepository.save(cashflowList);
    if(i%1000==0) {
            evictAllCacheValues();
                }   
        }
     logger.info("saved");
            }

@CacheEvict(value = "names",  allEntries = true)
    public void evictAllCacheValues() {
                      }

//I have also tried using 
@Scheduled(cron = "0 0/30 * * * ?")       // execute after every 30 min 
public void clearCacheSchedule(){
   for(String name:cacheManager.getCacheNames()){
            cacheManager.getCache(name).clear();  // clear cache by name
                           }
                        }
//在CacheConfiguration类中设置缓存名称:
@豆子
公共JCacheManagerCircustomizer缓存管理器Circustomizer(){
返回cm->{
createCache(com.cspl.cashflow.CashflowService.NAME,jcacheConfiguration);
};
}
//对于插入的循环:
公共静态最终字符串NAME=“NAME”;
@可缓存(“名称”)
public void getNames(){
logger.info(“保存”);

对于(int i=1;i您可以尝试使用
cache.removeAll()
清除所有缓存

要以编程方式执行,必须像这样使用

Cache cache = getCache(cacheName); // <---- You have to get the instance of //Ehcache with Configuration
  if (null != cache) {
    cache.removeAll();
  }

Cache Cache=getCache(cacheName);//对象会随着
cashflowList
的增长而多次保存。请尝试移动
cashflowlepository.save(cashflowList)
在循环之外。另外,请格式化代码。在没有循环的情况下,如何多次保存数据?@AndrewScache是哪个类的对象……您能详细说明一下吗?@Sambit