我的弹簧引导项目具有以下配置。在弹簧启动中启用Redis缓存
@SpringBootApplication
@EnableTransactionManagement
@EnableCaching
@EnableScheduling
@EnableAsync
public class Application {
String redisHost = "localhost";
int redisPort = 6379;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setHostName(redisHost);
factory.setPort(redisPort);
factory.setUsePool(true);
return factory;
}
@Bean
RedisTemplate<Object, Object> redisTemplate() {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
redisTemplate.setConnectionFactory(jedisConnectionFactory());
return redisTemplate;
}
@Bean
public CacheManager cacheManager() {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate());
return cacheManager;
}
}
此外,我有以下maven依赖于pom。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
我有一个单独的Redis服务器我的本地机器上定义的端口上运行。同样在我的服务类中,我有@Cacheable,@CachePut等注释来支持缓存。
我可以无误地启动spring引导应用程序,并且CRUD操作也可以使用。但似乎它没有使用定义的redis缓存。我使用'redi desktop manger'浏览工具,无法在redis上找到任何数据。另外,我尝试通过redis cli命令“monitor”监视redis服务器,但在监视器上看不到任何更改。
所以我假设redis缓存仍然不能在我的spring启动应用程序上工作。有人能帮我弄清楚这个问题吗?
我正在使用弹簧启动版本1.4.2.RELEASE
谢谢!
注:我测试类我与_Spring Boot_'1.5.0.RC1'还有'弹簧引导起动数据redis'运行。在我的'AutoRedisConfiguration'类中,我明确地将'spring.cache.type'设置为Redis,因为我的测试项目中还有其他一些使用其他缓存提供程序的示例,如Pivotal GemFire,Apache Geode和Hazelcast,但这并非严格必要如果您在类路径中的Spring Boot的自动配置支持中没有其他可能的缓存提供者。 –
谢谢,我会让你知道结果。我的春季启动版本是1.4.2.RELEASE – lsc