当前位置:网站首页>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;
}
}
边栏推荐
- [observation] ranked first in SaaS of pure public cloud in three years, and yonsuite's "flywheel effect"
- The information security and Standardization Commission issued the draft for comments on the management guide for app personal information processing activities
- LeetCode 112:路径总和
- Super technology network security risk assessment service, comprehensively understand the security risks faced by the network system
- 科研环境对人的影响是很大的
- Alphafold revealed the universe of protein structure - from nearly 1million structures to more than 200million structures
- It is found that the data of decimal type in the database can be obtained through resultset.getdouble, but this attribute cannot be obtained through GetObject.
- SiC功率半导体产业高峰论坛成功举办
- 10 major network security incidents in the past 10 years
- Embedded sharing collection 23
猜你喜欢

Nacos installation guide on win system

【7.21-26】代码源 - 【体育节】【丹钓战】【最大权值划分】

The brutal rule of blackmail software continues, and attacks increase by 105%

C language 300 lines of code to achieve mine sweeping (deployable + markable + changeable difficulty level)

What are source code, inverse code and complement code

HCIA configuration instance (ENSP)

LeetCode 113:路径总和 II

What is the ISO assessment? How to do the waiting insurance scheme

【HCIP】MGRE环境下OSPF实验,含多进程双向重发布及OSPF特殊区域

MySQL execution order
随机推荐
numpy.where() 用法和np.argsort()的用法
ELS stop at all
剑指offer专项突击版第13天
Data security is a competitive advantage. How can companies give priority to information security and compliance
Alphafold revealed the universe of protein structure - from nearly 1million structures to more than 200million structures
[web technology] 1395 esbuild bundler HMR
els 新的方块落下
With the explosive growth of digital identity in 2022, global organizations are facing greater network security
围绕新市民金融聚焦差异化产品设计、智能技术提效及素养教育
Merkel Studio - harmonyos implementation list to do
Nacos installation guide on win system
Event express | Apache Doris Performance Optimization Practice Series live broadcast course is open at the beginning. You are cordially invited to participate!
Read the recent trends of okaleido tiger and tap the value and potential behind it
规划数学期末模拟考试一
Analysys analysis: focus on users, improve the user experience of mobile banking, and help the growth of user value
【Web技术】1395- Esbuild Bundler HMR
【搜索】—— 迭代加深/双向DFS/IDA*
What are the common cyber threats faced by manufacturers and how do they protect themselves
internship:用于类型判断的工具类编写
TypeError: can only concatenate str (not “int“) to str