当前位置:网站首页>Personal blog item: processing of reading number +1 after viewing article details
Personal blog item: processing of reading number +1 after viewing article details
2022-06-29 05:37:00 【Don't want to grow 1.9 meters tall】
Requirements describe :
Click to view the article , Go to the article details page , Number of readings required +1, If you add a field update to the database table +1,
Existing problems :
(1) The update operation will add a write lock to the database table , It really affects performance ; In case of multiple visits , Will cause the article to be unable to view ;
(2) And if you will view articles and readings +1 As a whole business , The number of readings executed after querying the article details +1 If there is a problem during the operation of , It will lead to the interruption of the whole process , Unable to view articles normally ;
resolvent :
Use Thread pool technology , Put the operation of updating the reading number into the thread pool for execution , It will not affect the work of the main thread to view the details of the article
Concrete realization :
- Write thread pool configuration class ThreadPoolConfig
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
@Configuration
@EnableAsync// Open thread pool
public class ThreadPoolConfig {
@Bean("taskExecutor")
public Executor asyncServiceExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// Set the number of core threads
executor.setCorePoolSize(5);
// Set the maximum number of threads
executor.setMaxPoolSize(20);
// Configure queue size
executor.setQueueCapacity(Integer.MAX_VALUE);
// Set thread active time ( second )
executor.setKeepAliveSeconds(60);
// Set the default thread name
executor.setThreadNamePrefix("MyBlogThread");
// Wait for all tasks to finish before closing the thread pool
executor.setWaitForTasksToCompleteOnShutdown(true);
// Perform initialization
executor.initialize();
return executor;
}
}
- To write service layer ThreadService
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.guoxin.blog.dao.mapper.ArticleMapper;
import com.guoxin.blog.dao.pojo.Article;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
public class ThreadService {
// Perform operations in the thread pool , Does not affect the main thread
@Async("taskExecutor")
public void updateArticleViewCount(ArticleMapper articleMapper, Article article) {
int viewCounts = article.getViewCounts();
Article articleUpdate = new Article();
articleUpdate.setViewCounts(viewCounts + 1);
LambdaUpdateWrapper<Article> updateWrapper = new LambdaUpdateWrapper();
updateWrapper.eq(Article::getId,article.getId());
// For thread safety in multithreaded environments
//update article set view_count=100 where view_count=99 and id=11;
updateWrapper.eq(Article::getViewCounts,viewCounts);
articleMapper.update(articleUpdate,updateWrapper);
}
}
边栏推荐
- [high concurrency] deeply analyze the callable interface
- QT precautions and RCC download address
- How to choose congestion model and anemia model
- HTTP Caching Protocol practice
- Summary of redis basic knowledge points
- 2022-2028 global and Chinese industrial digital electronic blasting detonator Market Status and future development trend
- Leetcode notes on question brushing (XIV) -- related topics of binary tree attributes in binary tree chapter
- Résultats D - exam de Qinhuangdao au cours des 20 dernières années
- 【IoT】公众号“简一商业”更名为“产品人卫朋”说明
- Design risc-v processor from scratch -- data adventure of five stage pipeline
猜你喜欢

How to use thread stack location

Distributed transaction Seata

Tcapulusdb Jun · industry news collection (V)

2022 recommended high-speed rail industry research report investment strategy industry development prospect market analysis (the attachment is a link to the online disk, and the report is continuously

《软件体系结构》期末复习总结

轻松入门自然语言处理系列 专题7 基于FastText的文本分类

HTTP Caching Protocol practice

Analysis report on the investment market situation of the development planning prospect of the recommended chip industry research industry in 2022 (the attachment is a link to the network disk, and th

Slot

5,10-di (4-aminophenyl) - 15,20-diphenylporphyrin (cis-dadph2) /5,15-di (4-aminophenyl) - 10,20-diphenylporphyrin (trans-dadph2) / (tri-apph2) supplied by Qiyue
随机推荐
2022 recommended cloud computing industry research report investment strategy industry development prospect market analysis (the attachment is a link to the online disk, and the report is continuously
机器人强化学习——Transferring End-to-End Visuomotor Control from Simulation to RealWorld (CoRL 2017)
What has urbanization brought to our mental health and behavior?
DataX connection MySQL cannot find driver
Introduction to Photoshop (the first case)
2022 recommended REITs Industry Research Report investment strategy industry development prospect market analysis (the attachment is a link to the online disk, and the report is continuously updated)
QT precautions and RCC download address
Review of MySQL knowledge points
Microsoft Pinyin IME personal preferences
AttributeError: module ‘torch. nn. Parameter 'has no attribute' uninitializedparameter 'solution
Research Report on recommended specialized, special and new industries in 2022 industry development prospect and market investment analysis (the attachment is a link to the online disk, and the report
笔记本访问台式机的共享磁盘
使用VS创建静态链接库.lib并使用
Hantai oscilloscope software | Hantai oscilloscope upper computer software ns-scope, add measurement data arbitrarily
Structure training camp module II operation
Redis notes (II) operating commands for keys in redis
Annual inventory review of Alibaba cloud's observable practices in 2021
How to change the password of mysql8 created by docker
Love that can't be met -- what is the intimate relationship maintained by video chat
[IOT] description of renaming the official account "Jianyi commerce" to "product renweipeng"