From aad457d225620f18bdf8bb155ab9187e3a75dee5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E9=87=91=E6=88=90?= <2693239786@qq.com> Date: Sun, 8 Oct 2023 16:13:48 +0800 Subject: [PATCH] update --- .../edeacc9d294f3b307d4238fb3f381c6838218bb6 | 7 - .../.idea/sonarlint/issuestore/index.pb | 16 +- .../edeacc9d294f3b307d4238fb3f381c6838218bb6 | 0 .../sonarlint/securityhotspotstore/index.pb | 16 +- .../Service/PersonalHomeService.java | 140 +++---- .../springboot/Service/WebHomeService.java | 349 ++++++------------ .../FuncInterFaces/DeleteFunctionForDB.java | 5 + .../FuncInterFaces/InsertFunctionForDB.java | 5 + .../FuncInterFaces/QueryFunctionForDB.java | 7 + .../FuncInterFaces/UpdateFunctionForDB.java | 5 + .../xht/springboot/Utils/FunctionsForDB.java | 6 - .../springboot/Utils/RedisAndDbOpsUtils.java | 81 +++- 12 files changed, 271 insertions(+), 366 deletions(-) delete mode 100644 src/medicine/.idea/sonarlint/issuestore/e/d/edeacc9d294f3b307d4238fb3f381c6838218bb6 delete mode 100644 src/medicine/.idea/sonarlint/securityhotspotstore/e/d/edeacc9d294f3b307d4238fb3f381c6838218bb6 create mode 100644 src/medicine/springboot/src/main/java/com/xht/springboot/Utils/FuncInterFaces/DeleteFunctionForDB.java create mode 100644 src/medicine/springboot/src/main/java/com/xht/springboot/Utils/FuncInterFaces/InsertFunctionForDB.java create mode 100644 src/medicine/springboot/src/main/java/com/xht/springboot/Utils/FuncInterFaces/QueryFunctionForDB.java create mode 100644 src/medicine/springboot/src/main/java/com/xht/springboot/Utils/FuncInterFaces/UpdateFunctionForDB.java delete mode 100644 src/medicine/springboot/src/main/java/com/xht/springboot/Utils/FunctionsForDB.java diff --git a/src/medicine/.idea/sonarlint/issuestore/e/d/edeacc9d294f3b307d4238fb3f381c6838218bb6 b/src/medicine/.idea/sonarlint/issuestore/e/d/edeacc9d294f3b307d4238fb3f381c6838218bb6 deleted file mode 100644 index d4a1342d..00000000 --- a/src/medicine/.idea/sonarlint/issuestore/e/d/edeacc9d294f3b307d4238fb3f381c6838218bb6 +++ /dev/null @@ -1,7 +0,0 @@ - -t java:S120"ZRename this package name to match the regular expression '^[a-z_]+(\.[a-z_][a-z0-9_]*)*$'.( -a -java:S1128 "KRemove this unused import 'org.springframework.cache.annotation.Cacheable'.(ݺؓ -i -java:S1128 -"NRemove this unused import 'org.springframework.data.redis.core.RedisTemplate'.( \ No newline at end of file diff --git a/src/medicine/.idea/sonarlint/issuestore/index.pb b/src/medicine/.idea/sonarlint/issuestore/index.pb index b1e393c9..623c02d4 100644 --- a/src/medicine/.idea/sonarlint/issuestore/index.pb +++ b/src/medicine/.idea/sonarlint/issuestore/index.pb @@ -41,13 +41,19 @@ B springboot/pom.xml,e\4\e46aee6c7c512a7b48980a296fca0acf004a1907 w Gspringboot/src/main/java/com/xht/springboot/Config/RedissionConfig.java,e\7\e7fb8e0140bfaca4a8371f31a806802c8dad2ee2 - -Ospringboot/src/main/java/com/xht/springboot/Control/PersonalHomeController.java,b\e\be5bf7d32cedc052850e2cc0c99f2cf7357cac12 w Gspringboot/src/main/java/com/xht/springboot/Utils/BloomFilterUtils.java,6\6\661af37e1535a382309f94140434b4de4d42f13f s Cspringboot/src/main/java/com/xht/springboot/Config/RedisConfig.java,0\3\03febb48c6f14848416295ea77d17704b452f798 -z -Jspringboot/src/main/java/com/xht/springboot/Control/WebHomeController.java,e\d\edeacc9d294f3b307d4238fb3f381c6838218bb6 m -=springboot/src/main/java/com/xht/springboot/Utils/MyLock.java,d\6\d6d02a5bfa15e38571f6a2c3200cbaaebce3a1ae \ No newline at end of file +=springboot/src/main/java/com/xht/springboot/Utils/MyLock.java,d\6\d6d02a5bfa15e38571f6a2c3200cbaaebce3a1ae +{ +Kspringboot/src/main/java/com/xht/springboot/Dao/LoginAndRegisterMapper.java,6\4\6455addaf8475c1ee66ddf019aba9934cde58b33 + +Yspringboot/src/main/java/com/xht/springboot/Utils/FuncInterFaces/InsertFunctionForDB.java,7\d\7d70f34e659e4d46090652b3abb110013fe4a496 + +Yspringboot/src/main/java/com/xht/springboot/Utils/FuncInterFaces/DeleteFunctionForDB.java,0\b\0b251f1bfaf2cd8f80058d8dc2aa5d92389ae5dd + +Yspringboot/src/main/java/com/xht/springboot/Utils/FuncInterFaces/UpdateFunctionForDB.java,a\2\a208e939918d2caeb2c9b38f01b7b2b9a9c1c423 + +Xspringboot/src/main/java/com/xht/springboot/Utils/FuncInterFaces/QueryFunctionForDB.java,5\b\5be6cb1ac1481aa1480b23c960b9102ff121b2c3 \ No newline at end of file diff --git a/src/medicine/.idea/sonarlint/securityhotspotstore/e/d/edeacc9d294f3b307d4238fb3f381c6838218bb6 b/src/medicine/.idea/sonarlint/securityhotspotstore/e/d/edeacc9d294f3b307d4238fb3f381c6838218bb6 deleted file mode 100644 index e69de29b..00000000 diff --git a/src/medicine/.idea/sonarlint/securityhotspotstore/index.pb b/src/medicine/.idea/sonarlint/securityhotspotstore/index.pb index b1e393c9..623c02d4 100644 --- a/src/medicine/.idea/sonarlint/securityhotspotstore/index.pb +++ b/src/medicine/.idea/sonarlint/securityhotspotstore/index.pb @@ -41,13 +41,19 @@ B springboot/pom.xml,e\4\e46aee6c7c512a7b48980a296fca0acf004a1907 w Gspringboot/src/main/java/com/xht/springboot/Config/RedissionConfig.java,e\7\e7fb8e0140bfaca4a8371f31a806802c8dad2ee2 - -Ospringboot/src/main/java/com/xht/springboot/Control/PersonalHomeController.java,b\e\be5bf7d32cedc052850e2cc0c99f2cf7357cac12 w Gspringboot/src/main/java/com/xht/springboot/Utils/BloomFilterUtils.java,6\6\661af37e1535a382309f94140434b4de4d42f13f s Cspringboot/src/main/java/com/xht/springboot/Config/RedisConfig.java,0\3\03febb48c6f14848416295ea77d17704b452f798 -z -Jspringboot/src/main/java/com/xht/springboot/Control/WebHomeController.java,e\d\edeacc9d294f3b307d4238fb3f381c6838218bb6 m -=springboot/src/main/java/com/xht/springboot/Utils/MyLock.java,d\6\d6d02a5bfa15e38571f6a2c3200cbaaebce3a1ae \ No newline at end of file +=springboot/src/main/java/com/xht/springboot/Utils/MyLock.java,d\6\d6d02a5bfa15e38571f6a2c3200cbaaebce3a1ae +{ +Kspringboot/src/main/java/com/xht/springboot/Dao/LoginAndRegisterMapper.java,6\4\6455addaf8475c1ee66ddf019aba9934cde58b33 + +Yspringboot/src/main/java/com/xht/springboot/Utils/FuncInterFaces/InsertFunctionForDB.java,7\d\7d70f34e659e4d46090652b3abb110013fe4a496 + +Yspringboot/src/main/java/com/xht/springboot/Utils/FuncInterFaces/DeleteFunctionForDB.java,0\b\0b251f1bfaf2cd8f80058d8dc2aa5d92389ae5dd + +Yspringboot/src/main/java/com/xht/springboot/Utils/FuncInterFaces/UpdateFunctionForDB.java,a\2\a208e939918d2caeb2c9b38f01b7b2b9a9c1c423 + +Xspringboot/src/main/java/com/xht/springboot/Utils/FuncInterFaces/QueryFunctionForDB.java,5\b\5be6cb1ac1481aa1480b23c960b9102ff121b2c3 \ No newline at end of file diff --git a/src/medicine/springboot/src/main/java/com/xht/springboot/Service/PersonalHomeService.java b/src/medicine/springboot/src/main/java/com/xht/springboot/Service/PersonalHomeService.java index 91624e9f..f5e35673 100644 --- a/src/medicine/springboot/src/main/java/com/xht/springboot/Service/PersonalHomeService.java +++ b/src/medicine/springboot/src/main/java/com/xht/springboot/Service/PersonalHomeService.java @@ -5,6 +5,10 @@ import com.xht.springboot.Dao.PersonalHomeMapper; import com.xht.springboot.Entity.UserHome; import com.xht.springboot.Entity.UserText; import com.xht.springboot.Utils.BloomFilterUtils; +import com.xht.springboot.Utils.FuncInterFaces.InsertFunctionForDB; +import com.xht.springboot.Utils.FuncInterFaces.QueryFunctionForDB; +import com.xht.springboot.Utils.FuncInterFaces.UpdateFunctionForDB; +import com.xht.springboot.Utils.RedisAndDbOpsUtils; import org.redisson.RedissonMultiLock; import org.redisson.api.RLock; import org.redisson.api.RedissonClient; @@ -26,6 +30,10 @@ public class PersonalHomeService { BloomFilterUtils bloomFilterUtils; @Autowired RedissonClient redissonClient; + @Autowired + RedisAndDbOpsUtils redisAndDbOpsUtils; + + String userHomeDataCacheName = "userData::"; String userTxtByUidCacheName = "userTxtByUid::"; String userHomeDataLockHome = "userHomeDataLock"; @@ -34,119 +42,57 @@ public class PersonalHomeService { //获得用户主页数据 public UserHome getUserHomeData(int uid){ - if(!bloomFilterUtils.queryKey(userHomeDataCacheName+uid))//布隆过滤器先过滤 - return null; - - UserHome userHome = (UserHome) redisTemplate.opsForValue().get(userHomeDataCacheName + uid); - if(userHome!=null) - return userHome; - else{ - synchronized (PersonalHomeService.class){//双检加锁 - userHome = (UserHome) redisTemplate.opsForValue().get(userHomeDataCacheName + uid); - if(userHome!=null) - return userHome; - else{ - userHome = personalHomeMapper.findById(uid); - - RLock lock = redissonClient.getLock(userHomeDataLockHome); - RedissonMultiLock redissonMultiLock = new RedissonMultiLock(lock); - - redissonMultiLock.lock();//写缓存加锁 - try { - - if(userHome!=null) - redisTemplate.opsForValue().set(userHomeDataCacheName+uid,userHome); - else{//写回空值 - redisTemplate.opsForValue().set(userHomeDataCacheName+uid,null,30, TimeUnit.SECONDS); - } - - }catch (Exception e){ - e.printStackTrace(); - }finally { - redissonMultiLock.unlock(); - } - - return userHome; - } + QueryFunctionForDB queryFunctionForDB = new QueryFunctionForDB() { + @Override + public UserHome queryForDB(String token) { + UserHome userHome = personalHomeMapper.findById(Integer.parseInt(token)); + return userHome; } - } + }; + + return redisAndDbOpsUtils.queryHaveFilter(userHomeDataCacheName,uid+"",userHomeDataLockHome,queryFunctionForDB,uid+""); } public boolean postUserHomeData(UserHome userHome){//先更新数据库后删除缓存 - if(personalHomeMapper.postUserHomeData(userHome)!=0){ - RLock lock = redissonClient.getLock(userHomeDataLockHome); - RedissonMultiLock multiLock = new RedissonMultiLock(lock); - - multiLock.lock();//写缓存加锁 - try { - redisTemplate.delete(userHomeDataCacheName+userHome.getUid()); - }catch (Exception exception){ - exception.printStackTrace(); - }finally { - multiLock.unlock(); + InsertFunctionForDB insertFunctionForDB = new InsertFunctionForDB() { + @Override + public boolean insertForDB(UserHome data) { + int rows = personalHomeMapper.postUserHomeData(data); + System.out.println("insert userHomeData "+rows); + return rows >= 1; } + }; - bloomFilterUtils.putIntoBloomFilter(userHomeDataCacheName+userHome.getUid());//写入布隆过滤器 - return true; - } - return false; + return redisAndDbOpsUtils.insertHaveFilter(userHomeDataCacheName,userHome.getUid()+"",userHome,insertFunctionForDB); } public boolean updateUserHomeData(UserHome userHome){ - if(personalHomeMapper.updateUserHomeData(userHome)!=0){ - RLock lock = redissonClient.getLock(userHomeDataLockHome); - RedissonMultiLock redissonMultiLock = new RedissonMultiLock(lock); - - redissonMultiLock.lock(); - try { - redisTemplate.delete(userHomeDataCacheName+userHome.getUid()); - }catch (Exception exception){ - exception.printStackTrace(); - }finally { - redissonMultiLock.unlock(); + UpdateFunctionForDB updateFunctionForDB = new UpdateFunctionForDB() { + @Override + public boolean updateForDB(UserHome data) { + int rows = personalHomeMapper.updateUserHomeData(data); + System.out.println("update userHomeData "+rows); + return rows >= 1; } - return true; - } - return false; + }; + + return redisAndDbOpsUtils.baseUpdate(userHomeDataCacheName,userHome.getUid()+"",userHomeDataLockHome,userHome,updateFunctionForDB); } public List getUserTexts(int uid){ - if(!bloomFilterUtils.queryKey(userTxtByUidCacheName+uid)) - return null; - - List userTexts = (List) redisTemplate.opsForValue().get(userTxtByUidCacheName + uid); - if(userTexts!=null) - return userTexts; - else{ - synchronized (PersonalHomeService.class){ - userTexts = (List) redisTemplate.opsForValue().get(userTxtByUidCacheName + uid); - if(userTexts!=null) - return userTexts; - else{ - userTexts = personalHomeMapper.findTexts(uid); - - RLock lock = redissonClient.getLock(userTxtByUidLockHome); - RedissonMultiLock redissonMultiLock = new RedissonMultiLock(lock); - - redissonMultiLock.lock(); - try { - - if(userTexts!=null) - redisTemplate.opsForValue().set(userTxtByUidCacheName+uid,userTexts); - else - redisTemplate.opsForValue().set(userTxtByUidCacheName+uid,null,30,TimeUnit.SECONDS); - - }catch (Exception e){ - e.printStackTrace(); - }finally { - redissonMultiLock.unlock(); - } - + QueryFunctionForDB> queryFunctionForDB = new QueryFunctionForDB>() { + @Override + public List queryForDB(String token) { + List userTexts = personalHomeMapper.findTexts(Integer.parseInt(token)); + if(userTexts.size()==0) + return null; + else return userTexts; - } } - } + }; + + return redisAndDbOpsUtils.queryNoFilter(userTxtByUidCacheName,uid+"",userTxtByUidLockHome,queryFunctionForDB,uid+""); } } diff --git a/src/medicine/springboot/src/main/java/com/xht/springboot/Service/WebHomeService.java b/src/medicine/springboot/src/main/java/com/xht/springboot/Service/WebHomeService.java index d7c63cbb..2e8a43a6 100644 --- a/src/medicine/springboot/src/main/java/com/xht/springboot/Service/WebHomeService.java +++ b/src/medicine/springboot/src/main/java/com/xht/springboot/Service/WebHomeService.java @@ -6,9 +6,11 @@ import com.xht.springboot.Entity.UserComment; import com.xht.springboot.Entity.UserReply; import com.xht.springboot.Entity.UserText; import com.xht.springboot.Utils.BloomFilterUtils; -import com.xht.springboot.Utils.FunctionsForDB; +import com.xht.springboot.Utils.FuncInterFaces.DeleteFunctionForDB; +import com.xht.springboot.Utils.FuncInterFaces.InsertFunctionForDB; +import com.xht.springboot.Utils.FuncInterFaces.QueryFunctionForDB; +import com.xht.springboot.Utils.FuncInterFaces.UpdateFunctionForDB; import com.xht.springboot.Utils.RedisAndDbOpsUtils; -import org.checkerframework.checker.units.qual.A; import org.redisson.RedissonMultiLock; import org.redisson.api.RLock; import org.redisson.api.RedissonClient; @@ -18,22 +20,20 @@ import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.ArrayList; import java.util.List; -import java.util.concurrent.TimeUnit; @Service public class WebHomeService { @Autowired WebHomeMapper webHomeMapper; @Autowired - RedisTemplate redisTemplate; - @Autowired - BloomFilterUtils bloomFilterUtils; + RedisAndDbOpsUtils redisAndDbOpsUtils; @Autowired RedissonClient redissonClient; @Autowired - RedisAndDbOpsUtils redisAndDbOpsUtils; + RedisTemplate redisTemplate; + @Autowired + BloomFilterUtils bloomFilterUtils; String webHomeDataCacheName = "userTxt&Cmt::"; String webHomeDataKey = "all"; @@ -51,234 +51,140 @@ public class WebHomeService { //获得所有文章除了评论的回复 public List getWebHomeData(){ - FunctionsForDB> functionsForDB = new FunctionsForDB>() { + QueryFunctionForDB> queryFunctionForDB = new QueryFunctionForDB>() { @Override - public List queryFunctionForDB(String token) { - List userTexts = webHomeMapper.findAllTextAndComments(); - return userTexts; + public List queryForDB(String token) { + List userTexts = webHomeMapper.findAllTextAndComments();//没有返回空集合 + if(userTexts.size()==0) + return null; + else + return userTexts; } }; - return redisAndDbOpsUtils.queryNoFilter(webHomeDataCacheName,webHomeDataKey,webHomeDataLockName,functionsForDB,""); + return redisAndDbOpsUtils.queryNoFilter(webHomeDataCacheName,webHomeDataKey,webHomeDataLockName,queryFunctionForDB,""); } public List getUserReplies(int commentid){ - FunctionsForDB> functionsForDB = new FunctionsForDB>() { + QueryFunctionForDB> queryFunctionForDB = new QueryFunctionForDB>() { @Override - public List queryFunctionForDB(String token) { + public List queryForDB(String token) { List userReplies = webHomeMapper.findRepliesByCommentId(Integer.parseInt(token)); - return userReplies; + if(userReplies.size()==0) + return null; + else + return userReplies; } }; - - - if(!bloomFilterUtils.queryKey(userRepliesOfCmtCacheName+commentid)) - return null; - - List userReplies = (List) redisTemplate.opsForValue().get(userRepliesOfCmtCacheName+commentid); - if(userReplies!=null) - return userReplies; - else{ - synchronized (WebHomeService.class){ - userReplies = (List) redisTemplate.opsForValue().get(userRepliesOfCmtCacheName+commentid); - if(userReplies!=null) - return userReplies; - else { - - - RLock lock = redissonClient.getLock(userRepliesOfCmtLockName); - RedissonMultiLock redissonMultiLock = new RedissonMultiLock(lock); - - redissonMultiLock.lock(); - try { - if(userReplies!=null) - redisTemplate.opsForValue().set(userRepliesOfCmtCacheName+commentid,userReplies); - else - redisTemplate.opsForValue().set(userRepliesOfCmtCacheName+commentid,null,30,TimeUnit.SECONDS); - - }catch (Exception e){ - e.printStackTrace(); - }finally { - redissonMultiLock.unlock(); - } - - return userReplies; - } - } - } + return redisAndDbOpsUtils.queryNoFilter(userRepliesOfCmtCacheName,commentid+"",userRepliesOfCmtLockName,queryFunctionForDB,commentid+""); } public boolean postUserText(UserText userText){ - int rows=0; - rows = webHomeMapper.insertUserText(userText); - if(rows >= 1){ - System.out.println("insert text OK "+rows); - System.out.println("textid "+userText.getTextid()); - - bloomFilterUtils.putIntoBloomFilter(userTxtCacheName+userText.getTextid()); - return true; - }else{ - return false; - } + InsertFunctionForDB insertFunctionForDB = new InsertFunctionForDB() { + @Override + public boolean insertForDB(UserText data) { + int rows = webHomeMapper.insertUserText(data); + System.out.println("insert userText "+rows); + return rows >= 1; + } + }; + + return redisAndDbOpsUtils.insertHaveFilter(userTxtCacheName,userText.getTextid()+"" ,userText,insertFunctionForDB); } public boolean deleteUserText(UserText userText){ - int rows=0; - rows = webHomeMapper.deleteUserText(userText); - if(rows >= 1){ - System.out.println("delete text OK "+rows); - - RLock lock = redissonClient.getLock(userTxtLockName); - RedissonMultiLock redissonMultiLock = new RedissonMultiLock(lock); - - redissonMultiLock.lock(); - try { - redisTemplate.delete(userTxtCacheName+userText.getTextid());//删除缓存中数据 - }catch (Exception e){ - e.printStackTrace(); - }finally { - redissonMultiLock.unlock(); + DeleteFunctionForDB deleteFunctionForDB = new DeleteFunctionForDB() { + @Override + public boolean deleteForDB(UserText data) { + int rows = webHomeMapper.deleteUserText(data); + System.out.println("delete userText "+rows); + return rows >= 1; } + }; - return true; - }else{ - return false; - } + return redisAndDbOpsUtils.baseDelete(userTxtCacheName,userText.getTextid()+"",userTxtLockName,userText,deleteFunctionForDB); } public boolean updateUserText(UserText userText){ - int rows=0; - rows = webHomeMapper.updateUserText(userText); - if(rows >= 1){ - System.out.println("update text OK "+rows); - - RLock lock = redissonClient.getLock(userTxtLockName); - RedissonMultiLock redissonMultiLock = new RedissonMultiLock(lock); - - redissonMultiLock.lock(); - try { - redisTemplate.delete(userTxtCacheName+userText.getTextid());//删除缓存中数据 - - }catch (Exception e){ - e.printStackTrace(); - }finally { - redissonMultiLock.unlock(); + UpdateFunctionForDB userTextUpdateFunctionForDB = new UpdateFunctionForDB() { + @Override + public boolean updateForDB(UserText data) { + int rows = webHomeMapper.updateUserText(data); + System.out.println("update userText "+rows); + return rows >= 1; } + }; - return true; - }else{ - return false; - } + return redisAndDbOpsUtils.baseUpdate(userTxtCacheName,userText.getTextid()+"",userTxtLockName,userText,userTextUpdateFunctionForDB); } + @Transactional public List findUserText(String token){ - if(!bloomFilterUtils.queryKey(userFindTxtCacheName+token)) - return null; - - List userTexts = (List) redisTemplate.opsForValue().get(userFindTxtCacheName+token); - if(userTexts!=null) - return userTexts; - else{ - synchronized (WebHomeService.class){ - userTexts = (List) redisTemplate.opsForValue().get(userFindTxtCacheName+token); - if(userTexts!=null) + QueryFunctionForDB> queryFunctionForDB = new QueryFunctionForDB>() { + @Override + public List queryForDB(String token) { + List userTexts = webHomeMapper.findOfTextContains(token);//查询文本中是否包含 + userTexts.addAll(webHomeMapper.findOfTitleContains(token));//查询标题中是否包含 + if(userTexts.size()==0) + return userTexts; + else return userTexts; - else{ - userTexts = webHomeMapper.findOfTextContains(token);//查询文本中是否包含 - userTexts.addAll(webHomeMapper.findOfTitleContains(token));//查询标题中是否包含 + } + }; - RLock lock = redissonClient.getLock(userFindTxtLockName); - RedissonMultiLock redissonMultiLock = new RedissonMultiLock(lock); + return redisAndDbOpsUtils.queryNoFilter(userFindTxtCacheName,token,userFindTxtLockName,queryFunctionForDB,token); + } - redissonMultiLock.lock(); - try { - if(userTexts!=null) - redisTemplate.opsForValue().set(userFindTxtCacheName+token,userTexts); - else - redisTemplate.opsForValue().set(userFindTxtCacheName+token,null,30,TimeUnit.SECONDS); - }catch (Exception e){ - e.printStackTrace(); - }finally { - redissonMultiLock.unlock(); - } - return userTexts; - } - } - } - } public boolean postUserComment(UserComment userComment){ - int rows=0; - rows = webHomeMapper.insertUserComment(userComment); - if(rows >= 1){ - System.out.println("insert comment OK "+rows); - System.out.println("commentid "+userComment.getCommentid()); - - bloomFilterUtils.putIntoBloomFilter(userCmtCacheName+userComment.getCommentid()); - return true; - }else{ - return false; - } + InsertFunctionForDB insertFunctionForDB = new InsertFunctionForDB() { + @Override + public boolean insertForDB(UserComment data) { + int rows = webHomeMapper.insertUserComment(data); + System.out.println("insert userComment "+rows); + return rows >= 1; + } + }; + + return redisAndDbOpsUtils.insertHaveFilter(userCmtCacheName,userComment.getCommentid()+"",userComment,insertFunctionForDB); } public boolean deleteUserComment(UserComment userComment){ - int rows=0; - rows = webHomeMapper.deleteUserComment(userComment); - if(rows >= 1){ - System.out.println("delete comment OK "+rows); - - RLock lock = redissonClient.getLock(userCmtLockName); - RedissonMultiLock redissonMultiLock = new RedissonMultiLock(lock); - - redissonMultiLock.lock(); - try { - redisTemplate.delete(userCmtCacheName+userComment.getCommentid()); - }catch (Exception e){ - e.printStackTrace(); - }finally { - redissonMultiLock.unlock(); + DeleteFunctionForDB deleteFunctionForDB = new DeleteFunctionForDB() { + @Override + public boolean deleteForDB(UserComment data) { + int rows = webHomeMapper.deleteUserComment(data); + System.out.println("delete userComment "+rows); + return rows >= 1; } + }; - return true; - }else{ - return false; - } + return redisAndDbOpsUtils.baseDelete(userCmtCacheName,userComment.getCommentid()+"",userCmtLockName,userComment,deleteFunctionForDB); } public boolean updateUserComment(UserComment userComment){ - int rows=0; - rows = webHomeMapper.updateUserComment(userComment); - if(rows >= 1){ - System.out.println("update comment OK "+rows); - - RLock lock = redissonClient.getLock(userCmtLockName); - RedissonMultiLock redissonMultiLock = new RedissonMultiLock(lock); - - redissonMultiLock.lock(); - try { - redisTemplate.delete(userCmtCacheName+userComment.getCommentid()); - }catch (Exception e){ - e.printStackTrace(); - }finally { - redissonMultiLock.unlock(); + UpdateFunctionForDB updateFunctionForDB = new UpdateFunctionForDB() { + @Override + public boolean updateForDB(UserComment data) { + int rows = webHomeMapper.updateUserComment(data); + System.out.println("update userComment "+rows); + return rows >= 1; } + }; - return true; - }else{ - return false; - } + return redisAndDbOpsUtils.baseUpdate(userCmtCacheName,userComment.getCommentid()+"",userCmtLockName,userComment,updateFunctionForDB); } @@ -286,69 +192,48 @@ public class WebHomeService { + + public boolean postUserReply(UserReply userReply){ - int rows=0; - rows = webHomeMapper.insertUserReply(userReply); - if(rows >= 1){ - System.out.println("insert reply OK "+rows); - System.out.println("replyid "+userReply.getReplyid()); - - bloomFilterUtils.putIntoBloomFilter(userReplyCacheName+userReply.getReplyid()); - - return true; - }else{ - return false; - } + InsertFunctionForDB insertFunctionForDB = new InsertFunctionForDB() { + @Override + public boolean insertForDB(UserReply data) { + int rows = webHomeMapper.insertUserReply(data); + System.out.println("insert userReply "+rows); + return rows >= 1; + } + }; + + return redisAndDbOpsUtils.insertHaveFilter(userReplyCacheName,userReply.getReplyid()+"",userReply,insertFunctionForDB); } public boolean deleteUserReply(UserReply userReply){ - int rows=0; - rows = webHomeMapper.deleteUserReply(userReply); - if(rows >= 1){ - System.out.println("delete reply OK "+rows); - - RLock lock = redissonClient.getLock(userReplyLockName); - RedissonMultiLock redissonMultiLock = new RedissonMultiLock(lock); - - redissonMultiLock.lock(); - try { - redisTemplate.delete(userReplyCacheName+userReply.getReplyid()); - }catch (Exception e){ - e.printStackTrace(); - }finally { - redissonMultiLock.unlock(); + DeleteFunctionForDB userReplyDeleteFunctionForDB = new DeleteFunctionForDB() { + @Override + public boolean deleteForDB(UserReply data) { + int rows = webHomeMapper.deleteUserReply(data); + System.out.println("delete userReply "+rows); + return rows >= 1; } + }; - return true; - }else{ - return false; - } + return redisAndDbOpsUtils.baseDelete(userReplyCacheName,userReply.getReplyid()+"",userReplyLockName,userReply,userReplyDeleteFunctionForDB); } public boolean updateUserReply(UserReply userReply){ - int rows=0; - rows = webHomeMapper.updateUserReply(userReply); - if(rows >= 1){ - System.out.println("update reply OK "+rows); - - RLock lock = redissonClient.getLock(userReplyLockName); - RedissonMultiLock redissonMultiLock = new RedissonMultiLock(lock); - - redissonMultiLock.lock(); - try { - redisTemplate.delete(userReplyCacheName+userReply.getReplyid()); - }catch (Exception e){ - e.printStackTrace(); - }finally { - redissonMultiLock.unlock(); + UpdateFunctionForDB updateFunctionForDB = new UpdateFunctionForDB() { + @Override + public boolean updateForDB(UserReply data) { + int rows = webHomeMapper.updateUserReply(data); + System.out.println("update userReply "+rows); + return rows >= 1; } + }; - return true; - }else{ - return false; - } + return redisAndDbOpsUtils.baseUpdate(userReplyCacheName,userReply.getReplyid()+"",userReplyLockName,userReply,updateFunctionForDB); } + @Cacheable(cacheNames = "userReply",key = "#p0") public UserReply selectUserReply(int id){ return webHomeMapper.selectUserReply(id); diff --git a/src/medicine/springboot/src/main/java/com/xht/springboot/Utils/FuncInterFaces/DeleteFunctionForDB.java b/src/medicine/springboot/src/main/java/com/xht/springboot/Utils/FuncInterFaces/DeleteFunctionForDB.java new file mode 100644 index 00000000..a49b7b6d --- /dev/null +++ b/src/medicine/springboot/src/main/java/com/xht/springboot/Utils/FuncInterFaces/DeleteFunctionForDB.java @@ -0,0 +1,5 @@ +package com.xht.springboot.Utils.FuncInterFaces; + +public interface DeleteFunctionForDB { + public boolean deleteForDB(T data); +} diff --git a/src/medicine/springboot/src/main/java/com/xht/springboot/Utils/FuncInterFaces/InsertFunctionForDB.java b/src/medicine/springboot/src/main/java/com/xht/springboot/Utils/FuncInterFaces/InsertFunctionForDB.java new file mode 100644 index 00000000..6b5cdefd --- /dev/null +++ b/src/medicine/springboot/src/main/java/com/xht/springboot/Utils/FuncInterFaces/InsertFunctionForDB.java @@ -0,0 +1,5 @@ +package com.xht.springboot.Utils.FuncInterFaces; + +public interface InsertFunctionForDB { + public boolean insertForDB(T data); +} diff --git a/src/medicine/springboot/src/main/java/com/xht/springboot/Utils/FuncInterFaces/QueryFunctionForDB.java b/src/medicine/springboot/src/main/java/com/xht/springboot/Utils/FuncInterFaces/QueryFunctionForDB.java new file mode 100644 index 00000000..1dbad97d --- /dev/null +++ b/src/medicine/springboot/src/main/java/com/xht/springboot/Utils/FuncInterFaces/QueryFunctionForDB.java @@ -0,0 +1,7 @@ +package com.xht.springboot.Utils.FuncInterFaces; + +@FunctionalInterface +public interface QueryFunctionForDB { + public T queryForDB(String token); + +} diff --git a/src/medicine/springboot/src/main/java/com/xht/springboot/Utils/FuncInterFaces/UpdateFunctionForDB.java b/src/medicine/springboot/src/main/java/com/xht/springboot/Utils/FuncInterFaces/UpdateFunctionForDB.java new file mode 100644 index 00000000..c5ce61b5 --- /dev/null +++ b/src/medicine/springboot/src/main/java/com/xht/springboot/Utils/FuncInterFaces/UpdateFunctionForDB.java @@ -0,0 +1,5 @@ +package com.xht.springboot.Utils.FuncInterFaces; + +public interface UpdateFunctionForDB { + public boolean updateForDB(T data); +} diff --git a/src/medicine/springboot/src/main/java/com/xht/springboot/Utils/FunctionsForDB.java b/src/medicine/springboot/src/main/java/com/xht/springboot/Utils/FunctionsForDB.java deleted file mode 100644 index 6ac07ef1..00000000 --- a/src/medicine/springboot/src/main/java/com/xht/springboot/Utils/FunctionsForDB.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.xht.springboot.Utils; - -@FunctionalInterface -public interface FunctionsForDB { - public T queryFunctionForDB(String token); -} diff --git a/src/medicine/springboot/src/main/java/com/xht/springboot/Utils/RedisAndDbOpsUtils.java b/src/medicine/springboot/src/main/java/com/xht/springboot/Utils/RedisAndDbOpsUtils.java index e3adb29e..a220336c 100644 --- a/src/medicine/springboot/src/main/java/com/xht/springboot/Utils/RedisAndDbOpsUtils.java +++ b/src/medicine/springboot/src/main/java/com/xht/springboot/Utils/RedisAndDbOpsUtils.java @@ -1,7 +1,9 @@ package com.xht.springboot.Utils; -import com.xht.springboot.Entity.UserText; -import com.xht.springboot.Service.WebHomeService; +import com.xht.springboot.Utils.FuncInterFaces.DeleteFunctionForDB; +import com.xht.springboot.Utils.FuncInterFaces.InsertFunctionForDB; +import com.xht.springboot.Utils.FuncInterFaces.QueryFunctionForDB; +import com.xht.springboot.Utils.FuncInterFaces.UpdateFunctionForDB; import org.redisson.RedissonMultiLock; import org.redisson.api.RLock; import org.redisson.api.RedissonClient; @@ -9,7 +11,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; -import java.util.List; import java.util.concurrent.TimeUnit; @Component @@ -21,18 +22,18 @@ public class RedisAndDbOpsUtils { @Autowired RedissonClient redissonClient; - public T queryNoFilter(String cacheName,String key,String lockName,FunctionsForDB queryFunctionsForDB,String token){ - if(!bloomFilterUtils.queryKey(cacheName+key)) - return null; - + public T queryNoFilter(String cacheName, String key, String lockName, QueryFunctionForDB queryFunctionsForDB, String token){ return baseQuery(cacheName,key,lockName,queryFunctionsForDB,token); } - public T queryHaveFilter(String cacheName,String key,String lockName,FunctionsForDB queryFunctionsForDB,String token){ + public T queryHaveFilter(String cacheName,String key,String lockName,QueryFunctionForDB queryFunctionsForDB,String token){ + if(!bloomFilterUtils.queryKey(cacheName+key)) + return null; + return baseQuery(cacheName,key,lockName,queryFunctionsForDB,token); } - public T baseQuery(String cacheName,String key,String lockName,FunctionsForDB queryFunctionsForDB,String token){ + public T baseQuery(String cacheName,String key,String lockName,QueryFunctionForDB queryFunctionsForDB,String token){ T result = (T) redisTemplate.opsForValue().get(cacheName+key); if(result!=null) return result; @@ -40,19 +41,24 @@ public class RedisAndDbOpsUtils { synchronized (this){ result = (T) redisTemplate.opsForValue().get(cacheName+key); if(result!=null) - return result; + return (T) result; else { - result = (T) queryFunctionsForDB.queryFunctionForDB(token); + result = (T) queryFunctionsForDB.queryForDB(token); RLock lock = redissonClient.getLock(lockName); RedissonMultiLock redissonMultiLock = new RedissonMultiLock(lock); redissonMultiLock.lock(); try { - if(result!=null) + if(result!=null){ + System.out.println("have"); redisTemplate.opsForValue().set(cacheName+key,result); - else - redisTemplate.opsForValue().set(cacheName+key,null,30, TimeUnit.SECONDS); + } + else{ + System.out.println("null"); + redisTemplate.opsForValue().set(cacheName+key,null,30,TimeUnit.SECONDS); + } + }catch (Exception e){ e.printStackTrace(); }finally { @@ -65,4 +71,51 @@ public class RedisAndDbOpsUtils { } } + public boolean insertHaveFilter(String cacheName, String key, T data,InsertFunctionForDB insertFunctionForDB){ + if(insertFunctionForDB.insertForDB(data)){ + bloomFilterUtils.putIntoBloomFilter(cacheName+key); + return true; + }else + return false; + } + + public boolean baseDelete(String cacheName, String key, String lockName, T data, DeleteFunctionForDB deleteFunctionForDB){ + if(deleteFunctionForDB.deleteForDB(data)){ + RLock lock = redissonClient.getLock(lockName); + RedissonMultiLock redissonMultiLock = new RedissonMultiLock(lock); + + redissonMultiLock.lock(); + try { + redisTemplate.delete(cacheName+key);//删除缓存中数据 + }catch (Exception e){ + e.printStackTrace(); + }finally { + redissonMultiLock.unlock(); + } + + return true; + }else + return false; + } + + public boolean baseUpdate(String cacheName, String key, String lockName, T data, UpdateFunctionForDB updateFunctionForDB){ + if(updateFunctionForDB.updateForDB(data)){ + RLock lock = redissonClient.getLock(lockName); + RedissonMultiLock redissonMultiLock = new RedissonMultiLock(lock); + + redissonMultiLock.lock(); + try { + redisTemplate.delete(cacheName+key);//删除缓存中数据 + + }catch (Exception e){ + e.printStackTrace(); + }finally { + redissonMultiLock.unlock(); + } + + return true; + }else + return false; + } + }