Merge branch 'test'
This commit is contained in:
commit
5530f0321b
5
pom.xml
5
pom.xml
@ -68,6 +68,11 @@
|
||||
<artifactId>javax.mail</artifactId>
|
||||
<version>1.6.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>2.3.232</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
||||
@ -3,6 +3,7 @@ package cn.axzo.foundation.redis.support;
|
||||
import cn.axzo.foundation.web.support.AppRuntime;
|
||||
import cn.axzo.foundation.web.support.utils.KeyBuilder;
|
||||
import lombok.Builder;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
import java.time.LocalDate;
|
||||
@ -17,7 +18,8 @@ public class RedisIdWorker {
|
||||
|
||||
private static final long BEGIN_TIMESTAMP = 1640995200L;
|
||||
private static final long COUNT_BITS = 32;
|
||||
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");
|
||||
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");
|
||||
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
|
||||
|
||||
private RedisTemplate redisTemplate;
|
||||
private AppRuntime appRuntime;
|
||||
@ -50,8 +52,21 @@ public class RedisIdWorker {
|
||||
* 根据时间+redis产生当天自增id
|
||||
*/
|
||||
public long getDailyNextId(String prefix, LocalDate date) {
|
||||
String key = KeyBuilder.build(appRuntime, prefix, "icr", DATE_TIME_FORMATTER.format(date));
|
||||
String key = KeyBuilder.build(appRuntime, prefix, "icr", DATE_FORMATTER.format(date));
|
||||
Long count = redisTemplate.opsForValue().increment(key);
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 prefix+yyyyMMddHHmmss+递增id
|
||||
*
|
||||
* @param prefix
|
||||
* @param padSize
|
||||
* @return
|
||||
*/
|
||||
public String getDailyNo(String prefix, int padSize) {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
long dailyNextId = getDailyNextId(prefix, now.toLocalDate());
|
||||
return prefix + DATE_TIME_FORMATTER.format(now) + StringUtils.leftPad(String.valueOf(dailyNextId), padSize, "0");
|
||||
}
|
||||
}
|
||||
|
||||
@ -200,6 +200,21 @@ public class RedisLock {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 尝试立即获取锁,并执行supplier.get()方法,返回结果。<br>
|
||||
* timeoutMills = 0, expireMillis = 参数传入
|
||||
*/
|
||||
public <T> T acquireImmediatelyRun(long expireMillis, Supplier<T> supplier) {
|
||||
if (!lock(0, expireMillis)) {
|
||||
throw ResultCode.OPERATE_TOO_FREQUENTLY.toException();
|
||||
}
|
||||
try {
|
||||
return supplier.get();
|
||||
} finally {
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加锁 应该以: lock(); try { doSomething(); } finally { close(); } 的方式调用<br>
|
||||
* 外部不建议直接使用该方法,建议使用{@link #tryAcquireRun(long, long, Supplier)}明确指定锁的等待和过期时间
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package cn.axzo.foundation.unittest.support.config;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import redis.embedded.RedisServer;
|
||||
@ -11,21 +12,29 @@ import javax.annotation.PreDestroy;
|
||||
* redis存在时, 处理相关配置
|
||||
* 1. 启动embedded的redis
|
||||
*/
|
||||
@Slf4j
|
||||
@ConditionalOnClass({RedisTemplate.class})
|
||||
public class RedisConfig {
|
||||
private RedisServer redisServer;
|
||||
|
||||
@PostConstruct
|
||||
public void postConstruct() {
|
||||
redisServer = RedisServer.builder().port(16739).setting("maxheap 128m").build();
|
||||
// redis server会在后台启动一个redis server的进程,默认IP=127.0.0.1
|
||||
redisServer.start();
|
||||
try {
|
||||
redisServer = RedisServer.builder().port(16739).setting("maxheap 128m").build();
|
||||
// redis server会在后台启动一个redis server的进程,默认IP=127.0.0.1
|
||||
redisServer.start();
|
||||
} catch (Exception ex) {
|
||||
redisServer = null;
|
||||
log.error("embedded-redis start failed", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void preDestroy() {
|
||||
// 测试结束后必须调用redisServer.stop(), 否则后台运行的redis server进程会一直存在并占用6379端口
|
||||
redisServer.stop();
|
||||
if (redisServer != null) {
|
||||
// 测试结束后必须调用redisServer.stop(), 否则后台运行的redis server进程会一直存在并占用6379端口
|
||||
redisServer.stop();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user