perf[thread local]: use FastThreadLocalAdapter instead of FastThreadLocal

This commit is contained in:
sun
2023-09-20 17:52:57 +08:00
parent 62c36b1749
commit f05870ca51
2 changed files with 26 additions and 35 deletions
@@ -13,16 +13,14 @@
package com.zfoo.net.util.security;
import com.zfoo.protocol.util.FastThreadLocalAdapter;
import com.zfoo.protocol.util.StringUtils;
import io.netty.util.concurrent.FastThreadLocal;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.function.Supplier;
/**
* AES加密和解密
@@ -53,23 +51,31 @@ public abstract class AesUtils {
}
}
private static final FastThreadLocal<Cipher> LOCAL_ENCRYPT_CIPHER = new FastThreadLocal<Cipher>() {
private static final FastThreadLocalAdapter<Cipher> LOCAL_ENCRYPT_CIPHER = new FastThreadLocalAdapter<>(new Supplier<>() {
@Override
protected Cipher initialValue() throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException {
var cipher = Cipher.getInstance(ALGORITHM_STR);
cipher.init(Cipher.ENCRYPT_MODE, KEY);
return cipher;
public Cipher get() {
try {
Cipher cipher = Cipher.getInstance(ALGORITHM_STR);
cipher.init(Cipher.ENCRYPT_MODE, KEY);
return cipher;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
};
});
private static final FastThreadLocal<Cipher> LOCAL_DECRYPT_CIPHER = new FastThreadLocal<Cipher>() {
private static final FastThreadLocalAdapter<Cipher> LOCAL_DECRYPT_CIPHER = new FastThreadLocalAdapter<Cipher>(new Supplier<Cipher>() {
@Override
protected Cipher initialValue() throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException {
var cipher = Cipher.getInstance(ALGORITHM_STR);
cipher.init(Cipher.DECRYPT_MODE, KEY);
return cipher;
public Cipher get() {
try {
var cipher = Cipher.getInstance(ALGORITHM_STR);
cipher.init(Cipher.DECRYPT_MODE, KEY);
return cipher;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
};
});
/**
@@ -13,8 +13,8 @@
package com.zfoo.scheduler.util;
import com.zfoo.protocol.util.FastThreadLocalAdapter;
import com.zfoo.scheduler.manager.SchedulerBus;
import io.netty.util.concurrent.FastThreadLocal;
import org.springframework.scheduling.support.CronExpression;
import java.text.ParseException;
@@ -55,26 +55,11 @@ public abstract class TimeUtils {
// 统一的时间格式模板
public static final String DATE_FORMAT_TEMPLATE_FOR_DAY = "yyyy-MM-dd";
private static final FastThreadLocal<SimpleDateFormat> DATE_FORMAT = new FastThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(DATE_FORMAT_TEMPLATE);
}
};
private static final FastThreadLocalAdapter<SimpleDateFormat> DATE_FORMAT = new FastThreadLocalAdapter<>(() -> new SimpleDateFormat(DATE_FORMAT_TEMPLATE));
private static final FastThreadLocal<SimpleDateFormat> SIMPLE_DATE_FORMAT = new FastThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(SIMPLE_DATE_FORMAT_TEMPLATE);
}
};
private static final FastThreadLocalAdapter<SimpleDateFormat> SIMPLE_DATE_FORMAT = new FastThreadLocalAdapter<>(() -> new SimpleDateFormat(SIMPLE_DATE_FORMAT_TEMPLATE));
private static final FastThreadLocal<SimpleDateFormat> DATE_FORMAT_FOR_DAY = new FastThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(DATE_FORMAT_TEMPLATE_FOR_DAY);
}
};
private static final FastThreadLocalAdapter<SimpleDateFormat> DATE_FORMAT_FOR_DAY = new FastThreadLocalAdapter<>(() -> new SimpleDateFormat(DATE_FORMAT_TEMPLATE_FOR_DAY));
static {
currentTimeMillis();