diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml
index 30e75dc..df280b1 100644
--- a/.idea/inspectionProfiles/Project_Default.xml
+++ b/.idea/inspectionProfiles/Project_Default.xml
@@ -2,5 +2,6 @@
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index e37ea40..1e7bc0f 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,21 +4,16 @@
-
-
-
-
-
-
-
-
-
+
+
+
-
-
-
-
-
+
+
+
+
+
+
@@ -76,7 +71,7 @@
"WebServerToolWindowFactoryState": "false",
"git-widget-placeholder": "master",
"jdk.selected.JAVA_MODULE": "21",
- "last_opened_file_path": "F:/File/Java/spzx-parent/spzx-server-gateway/src/main/resources",
+ "last_opened_file_path": "F:/File/Java/spzx-parent/spzx-service/service-product/src/main/java/cn/bunny/web/product",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
@@ -93,8 +88,12 @@
]
}
}]]>
+
+
+
+
@@ -111,7 +110,7 @@
-
+
@@ -138,24 +137,10 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
@@ -169,7 +154,7 @@
1703487755445
-
+
@@ -195,7 +180,15 @@
1703494068723
-
+
+
+ 1703552833275
+
+
+
+ 1703552833275
+
+
@@ -204,6 +197,18 @@
-
+
+
+
+
+
+
+
+ file://$PROJECT_DIR$/spzx-service/service-product/src/main/java/cn/bunny/web/product/config/RedisConfig.java
+ 33
+
+
+
+
\ No newline at end of file
diff --git a/spzx-service/pom.xml b/spzx-service/pom.xml
index 2ece3cc..786c818 100644
--- a/spzx-service/pom.xml
+++ b/spzx-service/pom.xml
@@ -79,5 +79,15 @@
com.alibaba.cloud
spring-cloud-starter-alibaba-sentinel
+
+
+
+ org.springframework.boot
+ spring-boot-starter-data-redis
+
+
+ org.springframework.boot
+ spring-boot-starter-cache
+
diff --git a/spzx-service/service-product/src/main/java/cn/bunny/web/product/ProductApplication.java b/spzx-service/service-product/src/main/java/cn/bunny/web/product/ProductApplication.java
index a567531..79793ec 100644
--- a/spzx-service/service-product/src/main/java/cn/bunny/web/product/ProductApplication.java
+++ b/spzx-service/service-product/src/main/java/cn/bunny/web/product/ProductApplication.java
@@ -2,8 +2,10 @@ package cn.bunny.web.product;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
+@EnableCaching
public class ProductApplication {
public static void main(String[] args) {
SpringApplication.run(ProductApplication.class, args);
diff --git a/spzx-service/service-product/src/main/java/cn/bunny/web/product/config/RedisConfig.java b/spzx-service/service-product/src/main/java/cn/bunny/web/product/config/RedisConfig.java
new file mode 100644
index 0000000..a14d3ae
--- /dev/null
+++ b/spzx-service/service-product/src/main/java/cn/bunny/web/product/config/RedisConfig.java
@@ -0,0 +1,53 @@
+package cn.bunny.web.product.config;
+
+import org.springframework.cache.CacheManager;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.redis.cache.RedisCacheConfiguration;
+import org.springframework.data.redis.cache.RedisCacheManager;
+import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
+import org.springframework.data.redis.serializer.RedisSerializationContext;
+import org.springframework.data.redis.serializer.StringRedisSerializer;
+
+import java.time.Duration;
+
+@Configuration
+public class RedisConfig {
+ @Bean
+ public RedisTemplate redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
+ RedisTemplate redisTemplate = new RedisTemplate<>();
+ redisTemplate.setConnectionFactory(lettuceConnectionFactory);
+
+ // 设置key序列化为String
+ redisTemplate.setKeySerializer(new StringRedisSerializer());
+ // 设置value序列化方式为JSON,使用GenericJackson2JsonRedisSerializer替换为默认序列化
+ redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
+
+ redisTemplate.setHashKeySerializer(new StringRedisSerializer());
+ redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
+
+ return redisTemplate;
+ }
+
+ @Bean
+ public CacheManager cacheManager(LettuceConnectionFactory connectionFactory) {
+
+ //定义序列化器
+ GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
+ StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
+
+
+ RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
+ //过期时间600秒
+ .entryTtl(Duration.ofSeconds(600))
+ // 配置序列化
+ .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(stringRedisSerializer))
+ .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(genericJackson2JsonRedisSerializer));
+
+ return RedisCacheManager.builder(connectionFactory)
+ .cacheDefaults(config)
+ .build();
+ }
+}
diff --git a/spzx-service/service-product/src/main/java/cn/bunny/web/product/mapper/ProductSkuMapper.java b/spzx-service/service-product/src/main/java/cn/bunny/web/product/mapper/ProductSkuMapper.java
index b95b68f..4d86d87 100644
--- a/spzx-service/service-product/src/main/java/cn/bunny/web/product/mapper/ProductSkuMapper.java
+++ b/spzx-service/service-product/src/main/java/cn/bunny/web/product/mapper/ProductSkuMapper.java
@@ -1,5 +1,6 @@
package cn.bunny.web.product.mapper;
+import cn.bunny.common.spzx.model.entity.product.Category;
import cn.bunny.common.spzx.model.entity.product.ProductSku;
import org.apache.ibatis.annotations.Mapper;
diff --git a/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/ProductService.java b/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/ProductService.java
index 41c3cb4..75fe675 100644
--- a/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/ProductService.java
+++ b/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/ProductService.java
@@ -1,5 +1,6 @@
package cn.bunny.web.product.service;
+import cn.bunny.common.spzx.model.entity.product.Category;
import cn.bunny.common.spzx.model.entity.product.ProductSku;
import java.util.List;
diff --git a/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/imp/CategoryServiceImpl.java b/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/imp/CategoryServiceImpl.java
index 2738e48..c708c7d 100644
--- a/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/imp/CategoryServiceImpl.java
+++ b/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/imp/CategoryServiceImpl.java
@@ -4,6 +4,7 @@ import cn.bunny.common.spzx.model.entity.product.Category;
import cn.bunny.web.product.mapper.CategoryMapper;
import cn.bunny.web.product.service.CategoryService;
import jakarta.annotation.Resource;
+import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.List;
diff --git a/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/imp/ProductServiceImpl.java b/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/imp/ProductServiceImpl.java
index 30b2098..712daab 100644
--- a/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/imp/ProductServiceImpl.java
+++ b/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/imp/ProductServiceImpl.java
@@ -3,18 +3,45 @@ package cn.bunny.web.product.service.imp;
import cn.bunny.common.spzx.model.entity.product.ProductSku;
import cn.bunny.web.product.mapper.ProductSkuMapper;
import cn.bunny.web.product.service.ProductService;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.nacos.common.utils.StringUtils;
import jakarta.annotation.Resource;
+import org.springframework.cache.annotation.Cacheable;
+import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
+import java.util.Objects;
+import java.util.concurrent.TimeUnit;
@Service
public class ProductServiceImpl implements ProductService {
@Resource
private ProductSkuMapper productSkuMapper;
+ @Resource
+ private RedisTemplate redisTemplate;
+ @Cacheable(value = "productSku", key = "'one'")
@Override
public List selectProductSkuBySal() {
+/* // 1. 查询redis是否有一级分类
+ Object productSkuJson = redisTemplate.opsForValue().get("productSku:one");
+ String productSkuJsonString = JSON.toJSONString(productSkuJson);
+
+ // 2. 如果存在一级分类直接返回即可
+ if (!Objects.equals(productSkuJsonString, "null")) {
+ return JSON.parseArray(productSkuJsonString);
+ }
+ // 3. 如果redis没有一级分类,查询数据库,把数据库内容返回,并且查询放到redis中
+ List productSkuList = productSkuMapper.selectProductSkuBySale();
+ redisTemplate.opsForValue()
+ .set("productSku:one", JSON.toJSON(productSkuList), 7, TimeUnit.DAYS);
+ return productSkuList; */
+ Object productSkuJson = redisTemplate.opsForValue().get("productSku:one");
+ String productSkuJsonString = JSON.toJSONString(productSkuJson);
+ if (!Objects.equals(productSkuJsonString, "null")) {
+ return JSON.parseArray(productSkuJsonString);
+ }
return productSkuMapper.selectProductSkuBySale();
}
}
diff --git a/spzx-service/service-product/src/main/resources/application-dev.yml b/spzx-service/service-product/src/main/resources/application-dev.yml
index 9a5a190..cb3a4aa 100644
--- a/spzx-service/service-product/src/main/resources/application-dev.yml
+++ b/spzx-service/service-product/src/main/resources/application-dev.yml
@@ -16,6 +16,10 @@ spring:
url: jdbc:mysql://60.204.230.80:3306/db_spzx?characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: "02120212"
+ data:
+ redis:
+ port: 6379
+ host: 192.168.2.156
mybatis:
config-location: classpath:mybatis-config.xml