IntelliJ idea version 2017 Spring-boot2.0.4+mybatis+redis handles high concurrency, penetrating issues

Source: Internet
Author: User

I. When using the Reddis cache, if at the same time, 10,000 accesses, then there will be 10,000 times access to the database so it will be a huge pressure on the database, it is necessary to use the thread

1. Method body Lock (advantages, protection of the concurrent lock, the disadvantage of reducing memory efficiency)

1  /**2 * The most concise high concurrency processing, but the sacrifice efficiency is large3      *4      * @return5      */6      Public synchronizedList<student>selectAllStudent1 () {7 8         //String Serializer9Redisserializer Redisserializer =NewStringredisserializer ();Ten Redistemplate.setkeyserializer (redisserializer); One  A         /** - * In high concurrency conditions, there is a problem here, cache penetration problem -          */ the         //Query Keywords -List<student> studentlist = (list<student>) redistemplate.opsforvalue (). Get ("AllStudents"); -  -         if(NULL==studentlist) { +  -             //cache is empty, query database +Studentlist =studentmapper.selectallstudent (); A  at             //put the data in the database into Redis -Redistemplate.opsforvalue (). Set ("AllStudents", studentlist); -         } -  -         returnstudentlist; -}
View Code

2. Bicycle

1 /**2 * Double lock version for improved efficiency3      *4      * @return5      */6 @Override7      PublicList<student>selectallstudent () {8 9         //String SerializerTenRedisserializer Redisserializer =NewStringredisserializer (); One Redistemplate.setkeyserializer (redisserializer); A  -         /** - * In high concurrency conditions, there is a problem here, cache penetration problem the          */ -         //Query Keywords -List<student> studentlist = (list<student>) redistemplate.opsforvalue (). Get ("AllStudents"); -  +         if(NULL==studentlist) { -             //because the objects in spring are in singleton mode, the objects are locked directly +             synchronized( This) { A                 //get a bit from Redis atStudentlist = (list<student>) redistemplate.opsforvalue (). Get ("AllStudents"); -                 if(NULL==studentlist) { -SYSTEM.OUT.PRINTLN ("Querying Database *******************"); -                     //cache is empty, query database -Studentlist =studentmapper.selectallstudent (); -  in                     //put the data in the database into Redis -Redistemplate.opsforvalue (). Set ("AllStudents", studentlist); to}Else { +SYSTEM.OUT.PRINTLN ("Query cache *******************"); -                 } the             } *}Else { $SYSTEM.OUT.PRINTLN ("Query cache *******************");Panax Notoginseng         } -  the         returnstudentlist; +}
View Code

Two. Controller in addition to the thread pool for validation

1 /**2 * Redis test, perfect high concurrency3      *4      * @return5      */6@GetMapping ("/student/selectallstudent")7      PublicObject selectallstudent () {8 9         //thread, the thread that calls the underlying query all student methodsTenRunnable Runnable =NewRunnable () { One @Override A              Public voidrun () { - studentservice.selectallstudent (); -             } the         }; -  -  -         //Multithreading test penetration issues +Executorservice Executorservice = Executors.newfixedthreadpool (25); -  +          for(inti = 0; I < 10000; i++) { A Executorservice.submit (runnable); at         } -  -         returnstudentservice.selectallstudent (); -}
View Code

Three. Source Address

Https://github.com/liushaoye/02-transaction/tree/reddis

IntelliJ idea version 2017 Spring-boot2.0.4+mybatis+redis handles high concurrency, penetrating issues

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.