将首页数据存放在redis中作为缓存

This commit is contained in:
Bunny 2023-12-26 10:24:41 +08:00
parent 5e44c03f1c
commit e5d4d9daea
10 changed files with 139 additions and 34 deletions

View File

@ -2,5 +2,6 @@
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="RawUseOfParameterizedType" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="UNCHECKED_WARNING" enabled="false" level="WARNING" enabled_by_default="false" />
</profile>
</component>

View File

@ -4,21 +4,16 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="6ee93b58-a7e0-45a0-9e98-4c363b192afb" name="Changes" comment="解决跨域;修改MySQL语法错误;获取分类树形数据">
<change afterPath="$PROJECT_DIR$/.idea/uiDesigner.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/spzx-server-gateway/src/main/java/cn/bunny/getway/ServerGetWayApplication.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/spzx-server-gateway/src/main/resources/application-dev.yml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/spzx-server-gateway/src/main/resources/application.yml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/spzx-server-gateway/src/main/resources/logback-spring.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/spzx-server-gateway/src/main/resources/mybatis-config.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/dataSources.local.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/dataSources.local.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/dataSources/e46bfd2c-b955-434f-b6fc-b16ffa1fd71a.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/dataSources/e46bfd2c-b955-434f-b6fc-b16ffa1fd71a.xml" afterDir="false" />
<list default="true" id="6ee93b58-a7e0-45a0-9e98-4c363b192afb" name="Changes" comment="搭建网关服务和nacos将访问端口改为8500">
<change afterPath="$PROJECT_DIR$/spzx-service/service-product/src/main/java/cn/bunny/web/product/config/RedisConfig.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/inspectionProfiles/Project_Default.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/inspectionProfiles/Project_Default.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/spzx-server-gateway/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-server-gateway/pom.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/spzx-server-gateway/src/main/java/cn/bunny/web/App.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/spzx-service/service-product/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-product/pom.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/spzx-service/service-product/src/main/java/cn/bunny/web/product/controller/CategoryController.java" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-product/src/main/java/cn/bunny/web/product/controller/CategoryController.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/spzx-service/service-product/src/main/java/cn/bunny/web/product/controller/IndexController.java" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-product/src/main/java/cn/bunny/web/product/controller/IndexController.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/spzx-service/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/pom.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/spzx-service/service-product/src/main/java/cn/bunny/web/product/ProductApplication.java" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-product/src/main/java/cn/bunny/web/product/ProductApplication.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/spzx-service/service-product/src/main/java/cn/bunny/web/product/mapper/ProductSkuMapper.java" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-product/src/main/java/cn/bunny/web/product/mapper/ProductSkuMapper.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/ProductService.java" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/ProductService.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/imp/CategoryServiceImpl.java" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/imp/CategoryServiceImpl.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/imp/ProductServiceImpl.java" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/imp/ProductServiceImpl.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/spzx-service/service-product/src/main/resources/application-dev.yml" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-product/src/main/resources/application-dev.yml" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
@ -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 @@
]
}
}]]></component>
<component name="ReactorSettings">
<option name="notificationShown" value="true" />
</component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="F:\File\Java\spzx-parent\spzx-service\service-product\src\main\java\cn\bunny\web\product" />
<recent name="F:\File\Java\spzx-parent\spzx-server-gateway\src\main\resources" />
<recent name="F:\File\Java\spzx-parent\spzx-service\service-product\src\main\resources" />
</key>
@ -111,7 +110,7 @@
</set>
</option>
</component>
<component name="RunManager" selected="Spring Boot.ServerGetWayApplication">
<component name="RunManager" selected="Spring Boot.ProductApplication">
<configuration name="MangerApplication" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" temporary="true" nameIsGenerated="true">
<module name="spzx-manager" />
<option name="SPRING_BOOT_MAIN_CLASS" value="cn.bunny.MangerApplication" />
@ -138,24 +137,10 @@
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="ServerGetWayApplication" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" temporary="true" nameIsGenerated="true">
<module name="spzx-server-gateway" />
<option name="SPRING_BOOT_MAIN_CLASS" value="cn.bunny.getway.ServerGetWayApplication" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="cn.bunny.getway.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="Spring Boot.ServerGetWayApplication" />
<item itemvalue="Spring Boot.MangerApplication" />
<item itemvalue="Spring Boot.ProductApplication" />
<item itemvalue="Spring Boot.MangerApplication" />
</list>
</recent_temporary>
</component>
@ -169,7 +154,7 @@
<updated>1703487755445</updated>
<workItem from="1703487757122" duration="903000" />
<workItem from="1703488670206" duration="5932000" />
<workItem from="1703549556395" duration="3157000" />
<workItem from="1703549556395" duration="7669000" />
</task>
<task id="LOCAL-00001" summary="前端接口初始化;显示商品页面">
<option name="closed" value="true" />
@ -195,7 +180,15 @@
<option name="project" value="LOCAL" />
<updated>1703494068723</updated>
</task>
<option name="localTasksCounter" value="4" />
<task id="LOCAL-00004" summary="搭建网关服务和nacos将访问端口改为8500">
<option name="closed" value="true" />
<created>1703552833275</created>
<option name="number" value="00004" />
<option name="presentableId" value="LOCAL-00004" />
<option name="project" value="LOCAL" />
<updated>1703552833275</updated>
</task>
<option name="localTasksCounter" value="5" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -204,6 +197,18 @@
<component name="VcsManagerConfiguration">
<MESSAGE value="前端接口初始化;显示商品页面" />
<MESSAGE value="解决跨域;修改MySQL语法错误;获取分类树形数据" />
<option name="LAST_COMMIT_MESSAGE" value="解决跨域;修改MySQL语法错误;获取分类树形数据" />
<MESSAGE value="搭建网关服务和nacos将访问端口改为8500" />
<option name="LAST_COMMIT_MESSAGE" value="搭建网关服务和nacos将访问端口改为8500" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
<breakpoints>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/spzx-service/service-product/src/main/java/cn/bunny/web/product/config/RedisConfig.java</url>
<line>33</line>
<option name="timeStamp" value="15" />
</line-breakpoint>
</breakpoints>
</breakpoint-manager>
</component>
</project>

View File

@ -79,5 +79,15 @@
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- redis的起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -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);

View File

@ -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<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
RedisTemplate<String, Object> 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();
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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<ProductSku> 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<ProductSku> 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();
}
}

View File

@ -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