当前位置:网站首页>Slow storage scheme
Slow storage scheme
2022-07-29 01:47:00 【In the misty rain of the Pavilion】
List of articles
background
There is a large amount of data in the cache to be written to the database or saved to the storage service , In the process :
- The database times out due to multiple opening and closing
- A large amount of data written in a short time may explode the storage
The first idea is to solve the problem in batches batch Storage , For a total of m The data of , It ensures a certain writing efficiency ( Write once n strip ), And ensure certain safety ( Break a thing into m / n + 1 One transaction ). And this solution can also solve problem two , Because it is executed in batches , The length of time is lengthened , In fact, it is an effect of flow peak shaving .
Realization
Ideas have , Implementation is simple , Here the mybatis batchUpdate Batch processing , use CompletableFuture And recursive implementation of multi-threaded execution of transactions .
batchUpdate Refer to this document batchUpdate stay mybatis Application . Here is mainly about CompletableFuture.
CompletableFuture As a Java The multithreaded tool class provided by asynchronous package , It's very powerful , We don't need to configure the thread pool ourselves , Use it directly CompletableFuture.runAsync You can achieve the purpose of multi-threaded execution . It can also be executed asynchronously , Reference resources Official documents You know , Yes CompletableFuture The return value of can call whenComplete Method , Execute the next batch after the execution of the previous batch of tasks :
public void execAsync(List<List<User>> all, int i) {
if(i < all.size()) {
CompletableFuture<Void> future = CompletableFuture.runAsync(()->userService.batchInsert(all.get(i)));
if(i + 1 < all.size()) {
future.whenComplete(() -> execAsync(all, i + 1));
}
}
}
appendix :zip and gz Decompression and storage of files
public class FileUtil {
public static InputStream getNewInputStream(InputStream inputStream, String fileName, String subfix) {
return handleInputStream(inputStream, fileName, subfix);
}
private static InputStream handleInputStream(InputStream inputStream, String fileName, String subfix) {
Assert.isTrue(inputStream != null, " The input stream is null");
File file = null;
FileOutputStream fos = null;
try {
file = File.createTempFile(fileName, subfix);
if (!file.exists()) {
file.createNewFile();
}
fos = new FileOutputStream(file);
byte[] bytes = new byte[1024];
while ((inputStream.read(bytes)) != -1) {
fos.write(bytes);
}
} catch (IOException e) {
log.error(" Failed to create temporary file , The reason is :{}", e.getMessage());
} finally {
Assert.isTrue(fos != null, " File write stream is null");
try {
inputStream.close();
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (subfix.equals(SubFixConfig.ZIP)) {
return unzipFile(file, fileName);
}
if (subfix.equals(SubFixConfig.GZ)) {
return ungzFile(file);
}
return null;
}
private static InputStream unzipFile(File file, String filename) {
InputStream is = null;
try {
ZipFile zipFile = new ZipFile(file);
is = zipFile.getInputStream(zipFile.getEntry(filename));
} catch (IOException e) {
log.error(" Get decompression zip An error occurred in the post file stream , The reason is :{}", e.getMessage());
}
return is;
}
private static InputStream ungzFile(File file) {
GZIPInputStream gzipInputStream = null;
try {
gzipInputStream = new GZIPInputStream(new FileInputStream(file));
} catch (IOException e) {
log.error(" Get decompression gz An error occurred in the post file stream , The reason is :{}", e.getMessage());
}
return gzipInputStream;
}
}
边栏推荐
- 【7.21-26】代码源 - 【体育节】【丹钓战】【最大权值划分】
- uniapp createSelectorQuery(). Select get returns null error
- BOM系列之window对象
- 【GoLang】同步锁 Mutex
- Making high-precision map based on autoware (V)
- Where will Jinan win in hosting the first computing power conference?
- More interesting Title Dynamic Effect
- 易观分析:以用户为中心,提升手机银行用户体验,助力用户价值增长
- numpy.where() 用法和np.argsort()的用法
- Use of resttemplate and Eureka
猜你喜欢
J9 number theory: what factors determine the value of NFT?
Platofarm community ecological gospel, users can get premium income with elephant swap
How to protect WordPress website from network attack? It is essential to take safety measures
We summarized the three recommendations for the use of Nacos and first published the Nacos 3.0 plan for the 4th anniversary of the open source of Nacos
SiC功率半导体产业高峰论坛成功举办
[hcip] two mGRE networks are interconnected through OSPF (ENSP)
Openpyxl border
2022年最火的十大测试工具,你掌握了几个
Use of resttemplate and Eureka
动态内存与智能指针
随机推荐
[understanding of opportunity-54]: plain book-1-the origin of things [original chapter 1]: the road is simple.
How to deal with the DDoS attack on the game server and how to defend it?
TDA75610-I2C-模拟功放I2C地址的确定
【golang】使用select {}
body中基本标签
Analysys analysis: focus on users, improve the user experience of mobile banking, and help the growth of user value
About df['a column name'] [serial number]
承办首届算力大会,济南胜在何处?
How to choose professional, safe and high-performance remote control software
Openpyxl merge cells
【Unity项目实践】合成大西瓜
[unity project practice] synthetic watermelon
golang run时报undefined错误【已解决】
[WesternCTF2018]shrine
Test / development programmers rely on technology to survive the midlife crisis? Improve your own value
【GoLang】网络连接 net.Dial
如何选择专业、安全、高性能的远程控制软件
[hcip] experiment of republishing and routing strategy
[hcip] OSPF experiment under mGRE environment, including multi process bidirectional republication and OSPF special area
Cloud native application comprehensive exercise