我的彈簧引導項目具有以下配置。在彈簧啓動中啓用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