修复文件
This commit is contained in:
@@ -2,6 +2,7 @@ package com.org.flashsalesystem.service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.script.DefaultRedisScript;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -21,6 +22,7 @@ public class RedisService {
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("customStringRedisTemplate")
|
||||
private RedisTemplate<String, String> stringRedisTemplate;
|
||||
|
||||
@Autowired
|
||||
@@ -41,6 +43,13 @@ public class RedisService {
|
||||
redisTemplate.opsForValue().set(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置纯字符串值(用于Lua脚本兼容)
|
||||
*/
|
||||
public void setString(String key, String value) {
|
||||
stringRedisTemplate.opsForValue().set(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置字符串值并指定过期时间
|
||||
*/
|
||||
@@ -55,6 +64,13 @@ public class RedisService {
|
||||
return redisTemplate.opsForValue().get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 安全获取字符串值
|
||||
*/
|
||||
public String getString(String key) {
|
||||
return stringRedisTemplate.opsForValue().get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 原子递增
|
||||
*/
|
||||
@@ -330,7 +346,18 @@ public class RedisService {
|
||||
* 执行秒杀脚本
|
||||
*/
|
||||
public Long executeFlashSaleScript(String stockKey, int quantity) {
|
||||
return redisTemplate.execute(flashSaleScript, Collections.singletonList(stockKey), String.valueOf(quantity));
|
||||
log.info("执行秒杀脚本: stockKey={}, quantity={}", stockKey, quantity);
|
||||
|
||||
try {
|
||||
Long result = stringRedisTemplate.execute(flashSaleScript, Collections.singletonList(stockKey),
|
||||
String.valueOf(quantity));
|
||||
log.info("秒杀脚本执行结果: result={}", result);
|
||||
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
log.error("执行秒杀脚本异常: stockKey={}, quantity={}", stockKey, quantity, e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user