Kaynağa Gözat

优化缓存问题

java110 5 yıl önce
ebeveyn
işleme
e4dbdce282

+ 5 - 0
java110-core/pom.xml

@@ -29,6 +29,11 @@
             <scope>provided</scope>
         </dependency>
 
+        <dependency>
+            <groupId>org.springframework.data</groupId>
+            <artifactId>spring-data-redis</artifactId>
+        </dependency>
+
 
         <dependency>
             <groupId>com.aliyun</groupId>

+ 4 - 4
java110-core/src/main/java/com/java110/core/cache/CacheConfiguration.java

@@ -10,14 +10,14 @@ import org.springframework.core.io.ClassPathResource;
 /**
  * Created by wuxw on 2017/7/23.
  */
-@Configurable
-@EnableCaching
+//@Configurable
+//@EnableCaching
 public class CacheConfiguration  {
 
     /*
     * ehcache 主要的管理器
     */
-    @Bean(name = "appEhCacheCacheManager")
+    //@Bean(name = "appEhCacheCacheManager")
     public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean bean){
         return new EhCacheCacheManager (bean.getObject());
     }
@@ -25,7 +25,7 @@ public class CacheConfiguration  {
     /*
      * 据shared与否的设置,Spring分别通过CacheManager.create()或new CacheManager()方式来创建一个ehcache基地.
      */
-    @Bean
+    //@Bean
     public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
         EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean ();
         cacheManagerFactoryBean.setConfigLocation (new ClassPathResource("cache/ehcache-app.xml"));

+ 3 - 1
java110-core/src/main/java/com/java110/core/cache/Java110RedisConfig.java

@@ -1,6 +1,7 @@
 package com.java110.core.cache;
 
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
 import org.springframework.cache.CacheManager;
 import org.springframework.cache.annotation.CachingConfigurerSupport;
 import org.springframework.cache.annotation.EnableCaching;
@@ -16,8 +17,8 @@ import org.springframework.data.redis.serializer.RedisSerializationContext;
 import java.time.Duration;
 
 @Configuration
-
 @EnableCaching //开启缓存,默认是rendis缓存,继承CachingConfigurerSupport ,直接重写里面的方法
+@ConditionalOnBean(Java110RedisCacheWriter.class)
 public class Java110RedisConfig extends CachingConfigurerSupport {
 
     public final static String REDIS_EXPIRE_TIME_KEY = "#key_expire_time";
@@ -34,6 +35,7 @@ public class Java110RedisConfig extends CachingConfigurerSupport {
     public CacheManager cacheManager() {
 
         RedisConnectionFactory connectionFactory = redisTemplate.getConnectionFactory();
+        System.out.printf("123123");
         //上面实现的缓存读写
         Java110RedisCacheWriter java110RedisCacheWriter
                 = new Java110RedisCacheWriter(connectionFactory);

+ 1 - 0
service-front/src/main/java/com/java110/front/FrontServiceApplicationStart.java

@@ -9,6 +9,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
 import org.springframework.boot.web.client.RestTemplateBuilder;
+import org.springframework.cache.annotation.EnableCaching;
 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 import org.springframework.cloud.client.loadbalancer.LoadBalanced;
 import org.springframework.context.ApplicationContext;