xiangjincheng_branch
向金成 2 years ago
parent 0866c7f967
commit aad457d225

@ -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'.(–ª¡Çøÿÿÿÿ

@ -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
{
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

@ -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
{
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

@ -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<UserHome> queryFunctionForDB = new QueryFunctionForDB<UserHome>() {
@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<UserHome> insertFunctionForDB = new InsertFunctionForDB<UserHome>() {
@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<UserHome> updateFunctionForDB = new UpdateFunctionForDB<UserHome>() {
@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<UserText> getUserTexts(int uid){
if(!bloomFilterUtils.queryKey(userTxtByUidCacheName+uid))
return null;
List<UserText> userTexts = (List<UserText>) redisTemplate.opsForValue().get(userTxtByUidCacheName + uid);
if(userTexts!=null)
return userTexts;
else{
synchronized (PersonalHomeService.class){
userTexts = (List<UserText>) 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<List<UserText>> queryFunctionForDB = new QueryFunctionForDB<List<UserText>>() {
@Override
public List<UserText> queryForDB(String token) {
List<UserText> userTexts = personalHomeMapper.findTexts(Integer.parseInt(token));
if(userTexts.size()==0)
return null;
else
return userTexts;
}
}
}
};
return redisAndDbOpsUtils.queryNoFilter(userTxtByUidCacheName,uid+"",userTxtByUidLockHome,queryFunctionForDB,uid+"");
}
}

@ -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<UserText> getWebHomeData(){
FunctionsForDB<List<UserText>> functionsForDB = new FunctionsForDB<List<UserText>>() {
QueryFunctionForDB<List<UserText>> queryFunctionForDB = new QueryFunctionForDB<List<UserText>>() {
@Override
public List<UserText> queryFunctionForDB(String token) {
List<UserText> userTexts = webHomeMapper.findAllTextAndComments();
return userTexts;
public List<UserText> queryForDB(String token) {
List<UserText> 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<UserReply> getUserReplies(int commentid){
FunctionsForDB<List<UserReply>> functionsForDB = new FunctionsForDB<List<UserReply>>() {
QueryFunctionForDB<List<UserReply>> queryFunctionForDB = new QueryFunctionForDB<List<UserReply>>() {
@Override
public List<UserReply> queryFunctionForDB(String token) {
public List<UserReply> queryForDB(String token) {
List<UserReply> 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<UserReply> userReplies = (List<UserReply>) redisTemplate.opsForValue().get(userRepliesOfCmtCacheName+commentid);
if(userReplies!=null)
return userReplies;
else{
synchronized (WebHomeService.class){
userReplies = (List<UserReply>) 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<UserText> insertFunctionForDB = new InsertFunctionForDB<UserText>() {
@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<UserText> deleteFunctionForDB = new DeleteFunctionForDB<UserText>() {
@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<UserText> userTextUpdateFunctionForDB = new UpdateFunctionForDB<UserText>() {
@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<UserText> findUserText(String token){
if(!bloomFilterUtils.queryKey(userFindTxtCacheName+token))
return null;
List<UserText> userTexts = (List<UserText>) redisTemplate.opsForValue().get(userFindTxtCacheName+token);
if(userTexts!=null)
return userTexts;
else{
synchronized (WebHomeService.class){
userTexts = (List<UserText>) redisTemplate.opsForValue().get(userFindTxtCacheName+token);
if(userTexts!=null)
QueryFunctionForDB<List<UserText>> queryFunctionForDB = new QueryFunctionForDB<List<UserText>>() {
@Override
public List<UserText> queryForDB(String token) {
List<UserText> userTexts = webHomeMapper.findOfTextContains(token);//查询文本中是否包含
userTexts.addAll(webHomeMapper.findOfTitleContains(token));//查询标题中是否包含
if(userTexts.size()==0)
return userTexts;
else{
userTexts = webHomeMapper.findOfTextContains(token);//查询文本中是否包含
userTexts.addAll(webHomeMapper.findOfTitleContains(token));//查询标题中是否包含
else
return userTexts;
}
};
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<UserComment> insertFunctionForDB = new InsertFunctionForDB<UserComment>() {
@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<UserComment> deleteFunctionForDB = new DeleteFunctionForDB<UserComment>() {
@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<UserComment> updateFunctionForDB = new UpdateFunctionForDB<UserComment>() {
@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<UserReply> insertFunctionForDB = new InsertFunctionForDB<UserReply>() {
@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<UserReply> userReplyDeleteFunctionForDB = new DeleteFunctionForDB<UserReply>() {
@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<UserReply> updateFunctionForDB = new UpdateFunctionForDB<UserReply>() {
@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);

@ -0,0 +1,5 @@
package com.xht.springboot.Utils.FuncInterFaces;
public interface DeleteFunctionForDB <T>{
public boolean deleteForDB(T data);
}

@ -0,0 +1,5 @@
package com.xht.springboot.Utils.FuncInterFaces;
public interface InsertFunctionForDB <T>{
public boolean insertForDB(T data);
}

@ -0,0 +1,7 @@
package com.xht.springboot.Utils.FuncInterFaces;
@FunctionalInterface
public interface QueryFunctionForDB <T>{
public T queryForDB(String token);
}

@ -0,0 +1,5 @@
package com.xht.springboot.Utils.FuncInterFaces;
public interface UpdateFunctionForDB <T>{
public boolean updateForDB(T data);
}

@ -1,6 +0,0 @@
package com.xht.springboot.Utils;
@FunctionalInterface
public interface FunctionsForDB <T>{
public T queryFunctionForDB(String token);
}

@ -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> T queryNoFilter(String cacheName,String key,String lockName,FunctionsForDB queryFunctionsForDB,String token){
if(!bloomFilterUtils.queryKey(cacheName+key))
return null;
public <T> T queryNoFilter(String cacheName, String key, String lockName, QueryFunctionForDB queryFunctionsForDB, String token){
return baseQuery(cacheName,key,lockName,queryFunctionsForDB,token);
}
public <T> T queryHaveFilter(String cacheName,String key,String lockName,FunctionsForDB queryFunctionsForDB,String token){
public <T> 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> T baseQuery(String cacheName,String key,String lockName,FunctionsForDB queryFunctionsForDB,String token){
public <T> 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 <T> boolean insertHaveFilter(String cacheName, String key, T data,InsertFunctionForDB insertFunctionForDB){
if(insertFunctionForDB.insertForDB(data)){
bloomFilterUtils.putIntoBloomFilter(cacheName+key);
return true;
}else
return false;
}
public <T> 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 <T> 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;
}
}

Loading…
Cancel
Save