问题描述
我正在使用带有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 (将#修改为@)