在for循环内调用时,Ehcache不起作用

问题描述

我正在使用带有ehCache的Spring Boot在此应用程序中工作。当我调用不带for循环的方法时,缓存运行良好。当该密钥使用触发时间时,它将从数据库获取数据;再次使用同一密钥时,它将从缓存中获取数据。

            string[] examples = { "exists(//foo)","not(//bar)","empty(//bar)",@"every $x in //item satisfies matches($x/foo,'^\p{L}+$')" };

            XDocument doc = XDocument.Parse(@"<root>
  <items>
    <item>
      <foo>a</foo>
    </item>
    <item>
      <foo>b</foo>
    </item>
  </items>
</root>");

            Processor processor = new Processor();
    
            XQueryCompiler xqueryCompiler = processor.NewXQueryCompiler();

            DocumentBuilder docBuilder = processor.NewDocumentBuilder();

            XdmNode xdmDoc = docBuilder.Build(doc.CreateReader());

            foreach (string expression in examples)
            {
                XQueryEvaluator xqueryEvaluator = xqueryCompiler.Compile(expression).Load();
                xqueryEvaluator.ContextItem = xdmDoc;
                Console.WriteLine("Expression {0} evaluates to {1}.",expression,xqueryEvaluator.EvaluateSingle());
            }

但是当在for循环内调用方法时,即使该键以前是用户,每次也会调用数据库

@Cacheable(value = "userCache",key = "#userId")
public User findUser(Integer userId) {

    log.info("find user by id: {}",userId);

    return userRepository.findById(userId);

}

我的CacheConfig类:

public List<User> findUserList(List<Integer> userIdList) {
    List<User> userList = new ArrayList<>();

    for (Integer userId : userIdList) {
        userList.add(findUser(userId));
    }

    return userList;
}

@Cacheable(value = "userCache",userId);

    return userRepository.findById(userId);

}

}

我的ehcache.xml:

@EnableJpaRepositories(basePackages = "br.com.bb.project.repository")
@EnableCaching
@Configuration
public class CacheConfig {

 @Bean
 public CacheManager cacheManager() {
     return new EhCacheCacheManager(cacheManagerFactory().getobject());
 }

 @Bean
 public EhCacheManagerfactorybean cacheManagerFactory() {
     EhCacheManagerfactorybean beanfactory = new EhCacheManagerfactorybean();
     beanfactory.setConfigLocation(new ClassPathResource("ehcache.xml"));
     beanfactory.setShared(true);
     return beanfactory;
 }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...