|
|
|
@ -5,15 +5,22 @@ import com.xht.springboot.Dao.WebHomeMapper;
|
|
|
|
|
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.RedisAndDbOpsUtils;
|
|
|
|
|
import org.checkerframework.checker.units.qual.A;
|
|
|
|
|
import org.redisson.RedissonMultiLock;
|
|
|
|
|
import org.redisson.api.RLock;
|
|
|
|
|
import org.redisson.api.RedissonClient;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.cache.annotation.CacheEvict;
|
|
|
|
|
import org.springframework.cache.annotation.CachePut;
|
|
|
|
|
import org.springframework.cache.annotation.Cacheable;
|
|
|
|
|
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 {
|
|
|
|
@ -21,49 +28,82 @@ public class WebHomeService {
|
|
|
|
|
WebHomeMapper webHomeMapper;
|
|
|
|
|
@Autowired
|
|
|
|
|
RedisTemplate redisTemplate;
|
|
|
|
|
@Autowired
|
|
|
|
|
BloomFilterUtils bloomFilterUtils;
|
|
|
|
|
@Autowired
|
|
|
|
|
RedissonClient redissonClient;
|
|
|
|
|
@Autowired
|
|
|
|
|
RedisAndDbOpsUtils redisAndDbOpsUtils;
|
|
|
|
|
|
|
|
|
|
String webHomeDataCacheName = "userTxt&Cmt::";
|
|
|
|
|
String webHomeDataKey = "all";
|
|
|
|
|
String userRepliesCacheName = "userRepliesOfCmt::";
|
|
|
|
|
String userRepliesOfCmtCacheName = "userRepliesOfCmt::";
|
|
|
|
|
String userTxtCacheName = "userTxt::";
|
|
|
|
|
String userCmtCacheName = "userCmt::";
|
|
|
|
|
String userReplyCacheName = "userReply::";
|
|
|
|
|
String userFindTxtCacheName = "userFindTxt::";
|
|
|
|
|
String webHomeDataLockName = "webHomeDataLock";
|
|
|
|
|
String userRepliesOfCmtLockName = "userRepliesOfCmtLock";
|
|
|
|
|
String userTxtLockName = "userTxtLock";
|
|
|
|
|
String userFindTxtLockName = "userFindTxtLock";
|
|
|
|
|
String userCmtLockName = "userCmtLock";
|
|
|
|
|
String userReplyLockName = "userReplyLock";
|
|
|
|
|
|
|
|
|
|
//获得所有文章除了评论的回复
|
|
|
|
|
public List<UserText> getWebHomeData(){
|
|
|
|
|
List<UserText> userTexts = (List<UserText>) redisTemplate.opsForValue().get(webHomeDataCacheName+webHomeDataKey);
|
|
|
|
|
if(userTexts!=null)
|
|
|
|
|
return userTexts;
|
|
|
|
|
else{
|
|
|
|
|
synchronized (WebHomeService.class){
|
|
|
|
|
userTexts = (List<UserText>) redisTemplate.opsForValue().get(webHomeDataCacheName+webHomeDataKey);
|
|
|
|
|
if(userTexts!=null)
|
|
|
|
|
return userTexts;
|
|
|
|
|
else {
|
|
|
|
|
userTexts = webHomeMapper.findAllTextAndComments();
|
|
|
|
|
if(userTexts!=null)
|
|
|
|
|
redisTemplate.opsForValue().set(webHomeDataCacheName+webHomeDataKey,userTexts);
|
|
|
|
|
FunctionsForDB<List<UserText>> functionsForDB = new FunctionsForDB<List<UserText>>() {
|
|
|
|
|
@Override
|
|
|
|
|
public List<UserText> queryFunctionForDB(String token) {
|
|
|
|
|
List<UserText> userTexts = webHomeMapper.findAllTextAndComments();
|
|
|
|
|
return userTexts;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return redisAndDbOpsUtils.queryNoFilter(webHomeDataCacheName,webHomeDataKey,webHomeDataLockName,functionsForDB,"");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public List<UserReply> getUserReplies(int commentid){
|
|
|
|
|
FunctionsForDB<List<UserReply>> functionsForDB = new FunctionsForDB<List<UserReply>>() {
|
|
|
|
|
@Override
|
|
|
|
|
public List<UserReply> queryFunctionForDB(String token) {
|
|
|
|
|
List<UserReply> userReplies = webHomeMapper.findRepliesByCommentId(Integer.parseInt(token));
|
|
|
|
|
return userReplies;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!bloomFilterUtils.queryKey(userRepliesOfCmtCacheName+commentid))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
public List<UserReply> getUserReplies(int commentid){
|
|
|
|
|
List<UserReply> userReplies = (List<UserReply>) redisTemplate.opsForValue().get(userRepliesCacheName+commentid);
|
|
|
|
|
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(userRepliesCacheName+commentid);
|
|
|
|
|
userReplies = (List<UserReply>) redisTemplate.opsForValue().get(userRepliesOfCmtCacheName+commentid);
|
|
|
|
|
if(userReplies!=null)
|
|
|
|
|
return userReplies;
|
|
|
|
|
else {
|
|
|
|
|
userReplies = webHomeMapper.findRepliesByCommentId(commentid);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RLock lock = redissonClient.getLock(userRepliesOfCmtLockName);
|
|
|
|
|
RedissonMultiLock redissonMultiLock = new RedissonMultiLock(lock);
|
|
|
|
|
|
|
|
|
|
redissonMultiLock.lock();
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
if(userReplies!=null)
|
|
|
|
|
redisTemplate.opsForValue().set(userRepliesCacheName+commentid,userReplies);
|
|
|
|
|
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<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;
|
|
|
|
@ -126,8 +192,24 @@ public class WebHomeService {
|
|
|
|
|
else{
|
|
|
|
|
userTexts = webHomeMapper.findOfTextContains(token);//查询文本中是否包含
|
|
|
|
|
userTexts.addAll(webHomeMapper.findOfTitleContains(token));//查询标题中是否包含
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|