相关疑难解决方法(0)

Spring Cache @Cacheable - 从同一个bean的另一个方法调用时不工作

从同一个bean的另一个方法调用缓存方法时,Spring缓存不起作用.

这是一个以清晰的方式解释我的问题的例子.

组态:

<cache:annotation-driven cache-manager="myCacheManager" />

<bean id="myCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager" ref="myCache" />
</bean>

<!-- Ehcache library setup -->
<bean id="myCache"
    class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:shared="true">
    <property name="configLocation" value="classpath:ehcache.xml"></property>
</bean>

<cache name="employeeData" maxElementsInMemory="100"/>  
Run Code Online (Sandbox Code Playgroud)

缓存服务:

@Named("aService")
public class AService {

    @Cacheable("employeeData")
    public List<EmployeeData> getEmployeeData(Date date){
    ..println("Cache is not being used");
    ...
    }

    public List<EmployeeEnrichedData> getEmployeeEnrichedData(Date date){
        List<EmployeeData> employeeData = getEmployeeData(date);
        ...
    }

}
Run Code Online (Sandbox Code Playgroud)

结果:

aService.getEmployeeData(someDate);
output: Cache is not being used
aService.getEmployeeData(someDate); 
output: 
aService.getEmployeeEnrichedData(someDate); 
output: Cache is not being used
Run Code Online (Sandbox Code Playgroud)

getEmployeeData方法调用使用缓存employeeData …

java spring caching ehcache

83
推荐指数
6
解决办法
6万
查看次数

从同一个类中调用时,忽略Spring缓存@Cacheable方法

我正在尝试@Cacheable从同一个类中调用一个方法:

@Cacheable(value = "defaultCache", key = "#id")
public Person findPerson(int id) {
   return getSession().getPerson(id);
} 

public List<Person> findPersons(int[] ids) {
   List<Person> list = new ArrayList<Person>();
   for (int id : ids) {
      list.add(findPerson(id));
   }
   return list;
}
Run Code Online (Sandbox Code Playgroud)

并希望结果findPersons也被缓存,但@Cacheable注释被忽略,并且findPerson每次都执行方法.

我在这里做错了什么,或者这是有意的吗?

spring caching spring-cache

36
推荐指数
2
解决办法
2万
查看次数

AspectJ如何运作?

我想了解Aspect的工作原理.我来自C/C++背景,魔术永远不会发生.

我知道您可以注释一些函数,@Aspect然后记下Aspect实现等等.但是,新代码是如何(以及在​​什么时间)生成的?

假设我没有编辑.我使用javacc将生成.class文件的命令编译java类.现在,假设使用Aspect注释java文件.我不应该再用.classAspect以某种方式再次编译文件来生成另一组.class文件吗?

如果我的理解是正确的,那么这个双重编译步骤是如何在maven中完成的?还是春天?我找到了很多教程,它会告诉你在这里和那里添加什么来使事情有效但没有教程解释这些东西是如何实际工作的.

java aspectj

5
推荐指数
1
解决办法
1519
查看次数

标签 统计

caching ×2

java ×2

spring ×2

aspectj ×1

ehcache ×1

spring-cache ×1