Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/2.7.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
li-xunhuan committed Aug 22, 2023
2 parents 3625f18 + 31433a4 commit 9773be5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
23 changes: 12 additions & 11 deletions mica-core/src/main/java/net/dreamlu/mica/core/utils/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Collection;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.regex.Pattern;
import java.util.stream.Stream;

Expand Down Expand Up @@ -417,21 +418,21 @@ public static boolean simpleMatch(@Nullable String[] patterns, String str) {
}

/**
* 生成uuid(冲突概率小),采用 jdk 9 的形式,优化性能
* 生成uuid(更快,存在冲突的可能),采用 jdk 9 的形式,优化性能
*
* @return UUID
*/
public static String getUUID() {
return getUUID(Holder.SECURE_RANDOM);
return getUUID(ThreadLocalRandom.current());
}

/**
* 生成安全的 uuid(更快,存在冲突的可能),采用 jdk 9 的形式,优化性能
* 生成安全的 uuid(冲突概率小),采用 jdk 9 的形式,优化性能
*
* @return UUID
*/
public static String getFastUUID() {
return getUUID(Holder.RANDOM);
public static String getSafeUUID() {
return getUUID(Holder.SECURE_RANDOM);
}

private static String getUUID(Random random) {
Expand All @@ -453,7 +454,7 @@ private static String getUUID(Random random) {
* @return NanoId
*/
public static String getNanoId() {
return getNanoId(Holder.SECURE_RANDOM, true);
return getNanoId(ThreadLocalRandom.current(), true);
}

/**
Expand All @@ -462,25 +463,25 @@ public static String getNanoId() {
* @return NanoId
*/
public static String getNanoId62() {
return getNanoId(Holder.SECURE_RANDOM, false);
return getNanoId(ThreadLocalRandom.current(), false);
}

/**
* 一个小巧、安全、URL友好、21 位的字符串ID生成器。包含数字、大写、小写字母、_、-
*
* @return NanoId
*/
public static String getFastNanoId() {
return getNanoId(Holder.RANDOM, true);
public static String getSafeNanoId() {
return getNanoId(Holder.SECURE_RANDOM, true);
}

/**
* 一个小巧、安全、URL友好、21 位的字符串ID生成器,62 进制,只包含数字、大写、小写字母
*
* @return NanoId
*/
public static String getFastNanoId62() {
return getNanoId(Holder.RANDOM, false);
public static String getSafeNanoId62() {
return getNanoId(Holder.SECURE_RANDOM, false);
}

private static String getNanoId(Random random, boolean radix64) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,46 @@ public class StringTest {

public static void main(String[] args) {
long startNs1 = System.nanoTime();
for (int i = 0; i < 10000; i++) {
for (int i = 0; i < 100000; i++) {
System.out.println(StringUtil.getUUID());
}
long tookMs1 = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs1);
long startNs2 = System.nanoTime();
for (int i = 0; i < 10000; i++) {
for (int i = 0; i < 100000; i++) {
System.out.println(UUID.randomUUID());
}
long tookMs2 = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs2);
long startNs3 = System.nanoTime();
for (int i = 0; i < 10000; i++) {
for (int i = 0; i < 100000; i++) {
System.out.println(StringUtil.getUUID());
}
long tookMs3 = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs3);
long startNs4 = System.nanoTime();
for (int i = 0; i < 10000; i++) {
System.out.println(UUID.randomUUID());
for (int i = 0; i < 100000; i++) {
System.out.println(StringUtil.getNanoId());
}
long tookMs4 = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs4);
long startNs5 = System.nanoTime();
for (int i = 0; i < 10000; i++) {
System.out.println(StringUtil.getUUID());
for (int i = 0; i < 100000; i++) {
System.out.println(StringUtil.getSafeUUID());
}
long tookMs5 = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs5);
long startNs6 = System.nanoTime();
for (int i = 0; i < 10000; i++) {
System.out.println(StringUtil.getFastUUID());
for (int i = 0; i < 100000; i++) {
System.out.println(StringUtil.getSafeNanoId());
}
long tookMs6 = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs6);
long startNs7 = System.nanoTime();
for (int i = 0; i < 100000; i++) {
System.out.println(UUID.randomUUID());
}
long tookMs7 = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs7);
System.out.println("time1:" + tookMs1);
System.out.println("time2:" + tookMs2);
System.out.println("time3:" + tookMs3);
System.out.println("time4:" + tookMs4);
System.out.println("time5:" + tookMs5);
System.out.println("time6:" + tookMs6);
System.out.println("time7:" + tookMs7);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void test() {
int size = 100_0000;
for (int i = 0; i < size; i++) {
service.submit(() -> {
String nanoId = StringUtil.getFastNanoId();
String nanoId = StringUtil.getNanoId();
if (uuidSet.contains(nanoId)) {
System.out.println("-----------存在冲突-------");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
import io.nats.client.api.StreamInfo;
import io.nats.client.support.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.nats.core.DefaultNatsStreamTemplate;
import net.dreamlu.mica.nats.core.NatsStreamListenerDetector;
import net.dreamlu.mica.nats.core.NatsStreamTemplate;
import net.dreamlu.mica.nats.core.NatsTemplate;
import net.dreamlu.mica.nats.utils.StreamConfigurationUtil;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
Expand Down Expand Up @@ -69,4 +72,9 @@ public NatsStreamListenerDetector natsStreamListenerDetector(NatsStreamPropertie
return new NatsStreamListenerDetector(properties, natsStreamCustomizerObjectProvider, natsConnection, natsJetStream);
}

@Bean
public NatsStreamTemplate natsStreamTemplate(JetStream natsJetStream) {
return new DefaultNatsStreamTemplate(natsJetStream);
}

}

0 comments on commit 9773be5

Please sign in to comment.