TASK:1. 通过定时任务,补偿获取第三方api的运行结果;
2. pose 生成,对使用万相模型的生成限流,最大支持5个并发 3. Slogan限流,一分钟内,最多接收3个请求
This commit is contained in:
@@ -5,10 +5,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.*;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
@@ -101,4 +98,10 @@ public class DateUtil {
|
||||
return localDate.format(DateTimeFormatter.ofPattern(CommonConstant.TIME_FORMAT_MMM_dd_yyyy_EEEE, Locale.US));
|
||||
}
|
||||
|
||||
public static boolean isMoreThanOneDayApart(LocalDateTime givenDateTime) {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
Duration duration = Duration.between(givenDateTime, now);
|
||||
return duration.toHours() >= 24;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.ValueOperations;
|
||||
import org.springframework.data.redis.core.ZSetOperations;
|
||||
import org.springframework.data.redis.core.script.DefaultRedisScript;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -594,4 +595,19 @@ public class RedisUtil {
|
||||
return count;
|
||||
}
|
||||
|
||||
public boolean allowRequest(String apiKey) {
|
||||
String key = "rate_limit:" + apiKey;
|
||||
ValueOperations<String, String> ops = redisTemplate.opsForValue();
|
||||
|
||||
// 使用Redis的INCR命令
|
||||
Long count = ops.increment(key, 1);
|
||||
|
||||
if (count == 1) {
|
||||
// 第一次调用,设置过期时间
|
||||
redisTemplate.expire(key, 1, TimeUnit.MINUTES);
|
||||
}
|
||||
|
||||
return count <= 3;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user