【发布时间】:2017-07-07 08:09:30
【问题描述】:
我有以下代码:
@Service
public class MyServiceImpl implements MyService {
@Autowired
private MyDao myDao;
@Cacheable("callDao")
@Override
public MyResultModel callDao(MyCondition condition) {
System.out.println("call without cache");
return myDao.call(condition);
}
@Cacheable("cacheTest")
@Override
public MyResultModel cacheTest(MyCondition condition) {
System.out.println("call without cache");
return new MyResultModel(someProperties);
}
}
但是,callDao 缓存不起作用,因为该方法仍然一直调用数据库。另一方面,cacheTest 确实可以正常工作。我的callDao 方法有什么问题?
这是我的配置:
@Bean
public CacheManager cacheManager(RedisCacheManagerConfiguration configuration,
@SuppressWarnings("rawtypes") RedisTemplate redisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
cacheManager.setDefaultExpiration(configuration.getDefaultExpiration());
cacheManager.setUsePrefix(configuration.isUsePrefix());
cacheManager.setLoadRemoteCachesOnStartup(configuration.isLoadRemoteCachesOnStartup());
return cacheManager;
}
@Bean
public RedisTemplate<String, String> stringRedisTemplate(
@Qualifier("stringRedisSerializer") RedisSerializer<String> keySerializer,
@Qualifier("stringRedisSerializer") RedisSerializer<String> valueSerializer,
@Qualifier("stringRedisSerializer") RedisSerializer<String> hashKeySerializer,
@Qualifier("stringRedisSerializer") RedisSerializer<String> hashValueSerializer,
JedisConnectionFactory connectionFactory) {
StringRedisTemplate template = new StringRedisTemplate();
template.setKeySerializer(keySerializer);
template.setValueSerializer(valueSerializer);
template.setHashKeySerializer(hashKeySerializer);
template.setHashValueSerializer(hashValueSerializer);
template.setConnectionFactory(connectionFactory);
template.afterPropertiesSet();
return template;
}
@Bean(name = "stringRedisSerializer")
public StringRedisSerializer stringRedisSerializer() {
return new StringRedisSerializer();
}
我只配置了CacheManager 和RedisTemplate。我还添加了@EnableCaching 注释。
听到是我的条件:
public class MyCondition implements Serializable {
/**
* @since JDK 1.8
*/
private static final long serialVersionUID = 6262123870251938833L;
private String guid;
private Boolean isDelete;
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
public Boolean getIsDelete() {
return isDelete;
}
public void setIsDelete(Boolean isDelete) {
this.isDelete = isDelete;
}
}
听听我如何称呼这些方法
@EnableCaching
@EnableAutoConfiguration
@ComponentScan({ "com.mypackage" })
@SpringBootApplication
public class Application extends ContextIdApplicationContextInitializer {
public static ConfigurableApplicationContext ctx;
public static void main(String[] args) throws Exception {
ctx = SpringApplication.run(Application.class, args);
MyService myService = ctx.getBean(MyService.class);
for (int i = 0; i < 10; i++) {
MyCondition condition = new ConditionForRideCard();
condition.setGuid("adsgsfdhgsfgfdghhsdfgfadf");
myService.callDao(condition);
System.out.println("-------------------------------------------------------");
myService.cacheTest(condition);
System.out.println("=======================================================");
Thread.sleep(1000);
}
}
}
我得到这样的结果:
call without cache
-------------------------------------------------------
call without cache
=======================================================
call without cache
-------------------------------------------------------
=======================================================
call without cache
-------------------------------------------------------
=======================================================
call without cache
-------------------------------------------------------
=======================================================
call without cache
-------------------------------------------------------
=======================================================
call without cache
-------------------------------------------------------
=======================================================
call without cache
-------------------------------------------------------
=======================================================
call without cache
-------------------------------------------------------
=======================================================
call without cache
-------------------------------------------------------
=======================================================
call without cache
-------------------------------------------------------
=======================================================
【问题讨论】:
-
PS:我用redis配置我的缓存。
-
可能与您的缓存配置有关。做张贴
-
感谢您的帮助,我的配置已发布。 @黑暗
-
你能显示
MyCondition的代码吗?另外,如何调用callDao方法?您是从不同的类/服务还是在同一服务中调用它? -
在
MyCondition类中实现hashCode和equals。
标签: java spring spring-boot