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