From 0866c7f967fd6bc6c8217e71f6697beb15db010e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E9=87=91=E6=88=90?= <2693239786@qq.com> Date: Sat, 7 Oct 2023 20:10:33 +0800 Subject: [PATCH] update --- .../5feb9f75e1bc678660918562fd0ce2745899be29 | 23 -- .../6455addaf8475c1ee66ddf019aba9934cde58b33 | 6 - .../.idea/sonarlint/issuestore/index.pb | 22 +- .../5feb9f75e1bc678660918562fd0ce2745899be29 | 0 .../6455addaf8475c1ee66ddf019aba9934cde58b33 | 0 .../sonarlint/securityhotspotstore/index.pb | 22 +- .../springboot/Service/WebHomeService.java | 201 +++++++++++++++--- .../xht/springboot/Utils/FunctionsForDB.java | 6 + .../springboot/Utils/RedisAndDbOpsUtils.java | 68 ++++++ 9 files changed, 270 insertions(+), 78 deletions(-) delete mode 100644 src/medicine/.idea/sonarlint/issuestore/5/f/5feb9f75e1bc678660918562fd0ce2745899be29 delete mode 100644 src/medicine/.idea/sonarlint/issuestore/6/4/6455addaf8475c1ee66ddf019aba9934cde58b33 delete mode 100644 src/medicine/.idea/sonarlint/securityhotspotstore/5/f/5feb9f75e1bc678660918562fd0ce2745899be29 delete mode 100644 src/medicine/.idea/sonarlint/securityhotspotstore/6/4/6455addaf8475c1ee66ddf019aba9934cde58b33 create mode 100644 src/medicine/springboot/src/main/java/com/xht/springboot/Utils/FunctionsForDB.java create mode 100644 src/medicine/springboot/src/main/java/com/xht/springboot/Utils/RedisAndDbOpsUtils.java diff --git a/src/medicine/.idea/sonarlint/issuestore/5/f/5feb9f75e1bc678660918562fd0ce2745899be29 b/src/medicine/.idea/sonarlint/issuestore/5/f/5feb9f75e1bc678660918562fd0ce2745899be29 deleted file mode 100644 index 21a9adbf..00000000 --- a/src/medicine/.idea/sonarlint/issuestore/5/f/5feb9f75e1bc678660918562fd0ce2745899be29 +++ /dev/null @@ -1,23 +0,0 @@ - -J -java:S3740"/Provide the parametrized type for this generic.( -t java:S120"ZRename this package name to match the regular expression '^[a-z_]+(\.[a-z_][a-z0-9_]*)*$'.( -S -java:S2589"7Remove this expression which always evaluates to "true"( -S java:S106N"9Replace this use of System.out or System.err by a logger.( -S java:S106O"9Replace this use of System.out or System.err by a logger.(˼ -N java:S106]"9Replace this use of System.out or System.err by a logger.(ވ -N java:S106k"9Replace this use of System.out or System.err by a logger.( -T java:S106"9Replace this use of System.out or System.err by a logger.( -N java:S106"9Replace this use of System.out or System.err by a logger.(ƴ7 -T java:S106"9Replace this use of System.out or System.err by a logger.(㘘 -O java:S106"9Replace this use of System.out or System.err by a logger.(¼ -N java:S106"9Replace this use of System.out or System.err by a logger.(ܝ -T java:S106"9Replace this use of System.out or System.err by a logger.(Я -O java:S106"9Replace this use of System.out or System.err by a logger.( -O java:S106"9Replace this use of System.out or System.err by a logger.(ɂ -g -java:S1128 "LRemove this unused import 'org.springframework.cache.annotation.CacheEvict'.( -e -java:S1128 -"JRemove this unused import 'org.springframework.cache.annotation.CachePut'.(≠ \ No newline at end of file diff --git a/src/medicine/.idea/sonarlint/issuestore/6/4/6455addaf8475c1ee66ddf019aba9934cde58b33 b/src/medicine/.idea/sonarlint/issuestore/6/4/6455addaf8475c1ee66ddf019aba9934cde58b33 deleted file mode 100644 index 11cc87e8..00000000 --- a/src/medicine/.idea/sonarlint/issuestore/6/4/6455addaf8475c1ee66ddf019aba9934cde58b33 +++ /dev/null @@ -1,6 +0,0 @@ - -{ java:S120"ZRename this package name to match the regular expression '^[a-z_]+(\.[a-z_][a-z0-9_]*)*$'.(8ϥ1 -^ -java:S1128"ARemove this unused import 'org.apache.ibatis.annotations.Select'.(ߘ8ڠХ1 -g -java:S1128"ERemove this unused import 'org.springframework.stereotype.Component'.(Ѹ8ѷХ1 \ 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 4f78404f..b1e393c9 100644 --- a/src/medicine/.idea/sonarlint/issuestore/index.pb +++ b/src/medicine/.idea/sonarlint/issuestore/index.pb @@ -1,8 +1,6 @@ z Jspringboot/src/main/java/com/xht/springboot/pythonspider/PythonSpider.java,8\2\82d065e13dbb46b69838db763af0105eec47337f -{ -Kspringboot/src/main/java/com/xht/springboot/Dao/LoginAndRegisterMapper.java,6\4\6455addaf8475c1ee66ddf019aba9934cde58b33 m =train_cancer/test_information/train_model_and_load_picture.py,b\c\bc14878c6e46b0ed0df34c90b21fc25660f0e1f1 W @@ -33,13 +31,23 @@ q Aspringboot/src/main/java/com/xht/springboot/Entity/UserReply.java,0\5\051eaf551b928a8ccf651611c070ebb2b8155264 l getWebHomeData(){ - List userTexts = (List) redisTemplate.opsForValue().get(webHomeDataCacheName+webHomeDataKey); - if(userTexts!=null) - return userTexts; - else{ - synchronized (WebHomeService.class){ - userTexts = (List) redisTemplate.opsForValue().get(webHomeDataCacheName+webHomeDataKey); - if(userTexts!=null) - return userTexts; - else { - userTexts = webHomeMapper.findAllTextAndComments(); - if(userTexts!=null) - redisTemplate.opsForValue().set(webHomeDataCacheName+webHomeDataKey,userTexts); - return userTexts; - } + FunctionsForDB> functionsForDB = new FunctionsForDB>() { + @Override + public List queryFunctionForDB(String token) { + List userTexts = webHomeMapper.findAllTextAndComments(); + return userTexts; } - } - } + }; + return redisAndDbOpsUtils.queryNoFilter(webHomeDataCacheName,webHomeDataKey,webHomeDataLockName,functionsForDB,""); + } public List getUserReplies(int commentid){ - List userReplies = (List) redisTemplate.opsForValue().get(userRepliesCacheName+commentid); + FunctionsForDB> functionsForDB = new FunctionsForDB>() { + @Override + public List queryFunctionForDB(String token) { + List userReplies = webHomeMapper.findRepliesByCommentId(Integer.parseInt(token)); + 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(userRepliesCacheName+commentid); + userReplies = (List) redisTemplate.opsForValue().get(userRepliesOfCmtCacheName+commentid); if(userReplies!=null) return userReplies; else { - userReplies = webHomeMapper.findRepliesByCommentId(commentid); - if(userReplies!=null) - redisTemplate.opsForValue().set(userRepliesCacheName+commentid,userReplies); + + + 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; } } @@ -78,7 +118,7 @@ public class WebHomeService { System.out.println("insert text OK "+rows); System.out.println("textid "+userText.getTextid()); - redisTemplate.opsForValue().set(userTxtCacheName+userText.getTextid(),userText);//写入缓存 + bloomFilterUtils.putIntoBloomFilter(userTxtCacheName+userText.getTextid()); return true; }else{ return false; @@ -92,7 +132,18 @@ public class WebHomeService { if(rows >= 1){ System.out.println("delete text OK "+rows); - redisTemplate.opsForValue().getAndDelete(userTxtCacheName+userText.getTextid());//删除缓存中数据 + 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(); + } + return true; }else{ return false; @@ -105,7 +156,19 @@ public class WebHomeService { rows = webHomeMapper.updateUserText(userText); if(rows >= 1){ System.out.println("update text OK "+rows); - redisTemplate.opsForValue().getAndDelete(userTxtCacheName+userText.getTextid());//删除缓存中数据 + + 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(); + } return true; }else{ @@ -115,6 +178,9 @@ public class WebHomeService { @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; @@ -126,8 +192,24 @@ public class WebHomeService { else{ userTexts = webHomeMapper.findOfTextContains(token);//查询文本中是否包含 userTexts.addAll(webHomeMapper.findOfTitleContains(token));//查询标题中是否包含 - if(userTexts!=null) - redisTemplate.opsForValue().set(userFindTxtCacheName+token,userTexts); + + RLock lock = redissonClient.getLock(userFindTxtLockName); + RedissonMultiLock redissonMultiLock = new RedissonMultiLock(lock); + + 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; } } @@ -144,7 +226,7 @@ public class WebHomeService { System.out.println("insert comment OK "+rows); System.out.println("commentid "+userComment.getCommentid()); - redisTemplate.opsForValue().set(userCmtCacheName+userComment.getCommentid(),userComment); + bloomFilterUtils.putIntoBloomFilter(userCmtCacheName+userComment.getCommentid()); return true; }else{ return false; @@ -156,7 +238,19 @@ public class WebHomeService { rows = webHomeMapper.deleteUserComment(userComment); if(rows >= 1){ System.out.println("delete comment OK "+rows); - redisTemplate.opsForValue().getAndDelete(userCmtCacheName+userComment.getCommentid()); + + 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(); + } + return true; }else{ return false; @@ -168,7 +262,19 @@ public class WebHomeService { rows = webHomeMapper.updateUserComment(userComment); if(rows >= 1){ System.out.println("update comment OK "+rows); - redisTemplate.opsForValue().getAndDelete(userCmtCacheName+userComment.getCommentid()); + + 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(); + } + return true; }else{ return false; @@ -187,7 +293,8 @@ public class WebHomeService { System.out.println("insert reply OK "+rows); System.out.println("replyid "+userReply.getReplyid()); - redisTemplate.opsForValue().set(userReplyCacheName+userReply.getReplyid(),userReply); + bloomFilterUtils.putIntoBloomFilter(userReplyCacheName+userReply.getReplyid()); + return true; }else{ return false; @@ -199,7 +306,19 @@ public class WebHomeService { rows = webHomeMapper.deleteUserReply(userReply); if(rows >= 1){ System.out.println("delete reply OK "+rows); - redisTemplate.opsForValue().getAndDelete(userReplyCacheName+userReply.getReplyid()); + + 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(); + } + return true; }else{ return false; @@ -211,7 +330,19 @@ public class WebHomeService { rows = webHomeMapper.updateUserReply(userReply); if(rows >= 1){ System.out.println("update reply OK "+rows); - redisTemplate.opsForValue().getAndDelete(userReplyCacheName+userReply.getReplyid()); + + 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(); + } + return true; }else{ return false; 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 new file mode 100644 index 00000000..6ac07ef1 --- /dev/null +++ b/src/medicine/springboot/src/main/java/com/xht/springboot/Utils/FunctionsForDB.java @@ -0,0 +1,6 @@ +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 new file mode 100644 index 00000000..e3adb29e --- /dev/null +++ b/src/medicine/springboot/src/main/java/com/xht/springboot/Utils/RedisAndDbOpsUtils.java @@ -0,0 +1,68 @@ +package com.xht.springboot.Utils; + +import com.xht.springboot.Entity.UserText; +import com.xht.springboot.Service.WebHomeService; +import org.redisson.RedissonMultiLock; +import org.redisson.api.RLock; +import org.redisson.api.RedissonClient; +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 +public class RedisAndDbOpsUtils { + @Autowired + BloomFilterUtils bloomFilterUtils; + @Autowired + RedisTemplate redisTemplate; + @Autowired + RedissonClient redissonClient; + + public T queryNoFilter(String cacheName,String key,String lockName,FunctionsForDB queryFunctionsForDB,String token){ + if(!bloomFilterUtils.queryKey(cacheName+key)) + return null; + + return baseQuery(cacheName,key,lockName,queryFunctionsForDB,token); + } + + public T queryHaveFilter(String cacheName,String key,String lockName,FunctionsForDB queryFunctionsForDB,String token){ + return baseQuery(cacheName,key,lockName,queryFunctionsForDB,token); + } + + public T baseQuery(String cacheName,String key,String lockName,FunctionsForDB queryFunctionsForDB,String token){ + T result = (T) redisTemplate.opsForValue().get(cacheName+key); + if(result!=null) + return result; + else{ + synchronized (this){ + result = (T) redisTemplate.opsForValue().get(cacheName+key); + if(result!=null) + return result; + else { + result = (T) queryFunctionsForDB.queryFunctionForDB(token); + + RLock lock = redissonClient.getLock(lockName); + RedissonMultiLock redissonMultiLock = new RedissonMultiLock(lock); + + redissonMultiLock.lock(); + try { + if(result!=null) + redisTemplate.opsForValue().set(cacheName+key,result); + else + redisTemplate.opsForValue().set(cacheName+key,null,30, TimeUnit.SECONDS); + }catch (Exception e){ + e.printStackTrace(); + }finally { + redissonMultiLock.unlock(); + } + + return result; + } + } + } + } + +}