20250405 秒杀活动+redis配置

This commit is contained in:
liangjinglin 2025-04-05 21:05:56 +08:00
parent 292f15d3ee
commit 425b36309f
4 changed files with 92 additions and 1 deletions

View File

@ -48,5 +48,22 @@
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version> <version>8.0.33</version>
</dependency> </dependency>
<!-- 其他依赖... -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- lettuce 是 Redis 客户端spring-boot-starter-data-redis 默认使用它 -->
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.11.1</version> <!-- 使用最新稳定版本 -->
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -0,0 +1,24 @@
package com.liang.sca.goods.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
template.afterPropertiesSet();
return template;
}
}

View File

@ -0,0 +1,39 @@
package com.liang.sca.goods.controller;
import com.liang.sca.goods.mapper.GoodsInfoMapper;
import com.liang.sca.goods.model.GoodsInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/seckill")
public class SecKillController {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Autowired
private GoodsInfoMapper goodsInfoMapper;
/**
* 创建秒杀活动
* @param id 商品信息表的主键 ID
* @return 操作结果信息
*/
@RequestMapping("/create/{id}")
public String createSecKillActivity(@PathVariable int id) {
// 根据 id 查询 tb_goods_info
GoodsInfo goodsInfo = goodsInfoMapper.selectById(id);
if (goodsInfo == null) {
return "商品信息不存在";
}
// goodsId key将库存放入 redis
String key = "seckill:goods:" + goodsInfo.getGoodsId();
redisTemplate.opsForValue().set(key, goodsInfo.getGoodsStore());
return "秒杀活动创建成功";
}
}

View File

@ -14,4 +14,15 @@ spring:
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
jackson: jackson:
serialization: serialization:
FAIL_ON_EMPTY_BEANS: false FAIL_ON_EMPTY_BEANS: false
redis:
host: 154.12.80.119
port: 6379
database: 0
password: 123456
lettuce:
pool:
max-active: 8
max-wait: -1ms
max-idle: 8
min-idle: 0