Procházet zdrojové kódy

修复章节更新

tianyunperfect před 3 roky
rodič
revize
a7ce01973a

+ 6 - 5
book-server/src/main/java/com/book/server/aspect/CacheAOP.java

@@ -1,6 +1,7 @@
 package com.book.server.aspect;
 
 import com.book.server.annotation.CacheFind;
+import com.book.server.common.util.CacheUtil;
 import com.book.server.utils.JsonUtils;
 import com.book.server.utils.RedisUtil;
 import lombok.extern.slf4j.Slf4j;
@@ -15,10 +16,13 @@ import org.springframework.stereotype.Component;
 import java.util.ArrayList;
 import java.util.Arrays;
 
+import static com.book.server.config.Const.CacheSplit;
+
 @Component
 @Aspect
 @Slf4j
 public class CacheAOP {
+
     @Autowired
     private RedisUtil redisUtil;
 
@@ -38,11 +42,8 @@ public class CacheAOP {
     public Object around(ProceedingJoinPoint joinPoint, CacheFind cacheFind) {
         Object result = null;
         try {
-            //如何获取动态的获取注解中的数据?
-            String prokey = cacheFind.key();
-            //动态的获取方法中的参数
-            String args = Arrays.toString(joinPoint.getArgs());
-            String key = prokey + "::" + args;
+            String key = CacheUtil.getKeyWithPre(cacheFind.key(), joinPoint.getArgs());
+
             Object o = redisUtil.get(key);
             MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
             Class returnClass = methodSignature.getReturnType();

+ 35 - 0
book-server/src/main/java/com/book/server/common/util/CacheUtil.java

@@ -0,0 +1,35 @@
+package com.book.server.common.util;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+
+import static com.book.server.config.Const.CacheSplit;
+
+public class CacheUtil {
+    public static String getKeyWithPre(String pre, Object[] keys) {
+        StringBuilder sb = new StringBuilder();
+        sb.append(pre).append(CacheSplit);
+        sb.append(getKey(keys));
+        return sb.toString();
+    }
+
+    public static String getKey(Object... keys) {
+        StringBuilder sb = new StringBuilder();
+        for (int i = 0; i < keys.length; i++) {
+            sb.append(keys[i]);
+            if (i < keys.length - 1) {
+                sb.append(CacheSplit);
+            }
+        }
+        return sb.toString();
+    }
+
+    public static void main(String[] args) {
+        ArrayList<String> strings = new ArrayList<>();
+        strings.add("1");
+        strings.add("1");
+        System.out.println(getKeyWithPre("test", strings.toArray()));
+        System.out.println(getKey("test", 1, 1));
+    }
+}

+ 6 - 0
book-server/src/main/java/com/book/server/config/Const.java

@@ -0,0 +1,6 @@
+package com.book.server.config;
+
+public class Const {
+    public static final String CacheSplit = "_";
+    public static final String EDIT_BOOK_PRE = "edit_book";
+}

+ 13 - 8
book-server/src/main/java/com/book/server/service/impl/BookServiceImpl.java

@@ -17,9 +17,11 @@ import com.book.dao.cpsshard.pojo.example.UserRecentlyReadExample;
 import com.book.server.common.entity.Result;
 import com.book.server.common.entity.ResultCode;
 import com.book.dao.utils.DateUtils;
+import com.book.server.common.util.CacheUtil;
 import com.book.server.common.util.JsonUtils;
 import com.book.dao.cps.pojo.example.*;
 import com.book.dao.VO.*;
+import com.book.server.config.Const;
 import com.book.server.service.BookService;
 import com.book.server.service.CacheService;
 import com.book.server.utils.HttpTool;
@@ -261,7 +263,8 @@ public class BookServiceImpl implements BookService {
         if (freeChapterNum == null) { //查看默认配置
             freeChapterNum = getDefaultFreeChapterNum();
         }
-        if (bookContentVO.getContentId() > freeChapterNum) { //收费
+        if (bookContentVO.getContentId() > freeChapterNum) {
+            //收费
             //是否已购买
             Consume consume = consumeMapper.selectOneByExample(ConsumeExample.newAndCreateCriteria()
                     .andUserIdEqualTo(bookContentVO.getUserId())
@@ -366,7 +369,7 @@ public class BookServiceImpl implements BookService {
     }
 
     private BookContent getBookContent(BookContentVO bookContentVO) {
-        String key = getKey("edit_book", bookContentVO.getBookId(), bookContentVO.getContentId());
+        String key = CacheUtil.getKey(Const.EDIT_BOOK_PRE, bookContentVO.getBookId(), bookContentVO.getContentId());
         Object o = redisUtil.get(key);
         if (o != null) {
             return JsonUtils.getObject(o.toString(), BookContent.class);
@@ -378,18 +381,19 @@ public class BookServiceImpl implements BookService {
             if (content != null) {
                 return getBookContentFromVO(bookContentVO.getBookId(), bookContentVO.getContentId(), bookContentVO.getContentName(), content);
             }
-
             if (cacheService.isUploadBook(bookContentVO.getBookId())) {
                 // 自定义的书籍
                 bookContent = getBookContentFromDB(bookContentVO.getBookId(), bookContentVO.getContentId(), bookContentVO.getContentName());
             } else {
                 bookContent = getBookContentFromRemote(bookContentVO.getBookId(), bookContentVO.getContentId());
             }
+            redisUtil.setWithTime(key, JsonUtils.toJsonStr(bookContent));
             return bookContent;
         }
     }
 
 
+
     private void checkUpdate(int i) {
         if (i <= 0) {
             throw new RuntimeException("请稍后重试");
@@ -702,17 +706,17 @@ public class BookServiceImpl implements BookService {
     }
 
     public String getEditBookContentById(Long bookId, Long chapterId) {
-        String key = getKey("edit_book", bookId, chapterId);
+        String key = CacheUtil.getKey(Const.EDIT_BOOK_PRE, bookId, chapterId);
         Object o = redisUtil.get(key);
         if (o == null) {
             return null;
         } else {
-            return (String) o;
+            return o.toString();
         }
     }
 
     public String getKey(Object... keys) {
-        StringBuilder sb = new StringBuilder("t_");
+        StringBuilder sb = new StringBuilder();
         for (Object key : keys) {
             sb.append(key).append("_");
         }
@@ -720,7 +724,7 @@ public class BookServiceImpl implements BookService {
     }
 
     public void updateEditBookByIdCache(Long bookId, Long chapterId) {
-        String key = getKey("edit_book", bookId, chapterId);
+        String key = CacheUtil.getKey(Const.EDIT_BOOK_PRE, bookId, chapterId);
         EditBook obj = editBookMapper.selectOneByExampleSelective(
                 EditBookExample.newAndCreateCriteria()
                         .andBookIdEqualTo(bookId)
@@ -736,11 +740,12 @@ public class BookServiceImpl implements BookService {
         List<EditBook> editBooks = editBookMapper.selectByExampleSelective(EditBookExample.newAndCreateCriteria().example(),
                 EditBook.Column.bookId, EditBook.Column.chapterId, EditBook.Column.content);
         editBooks.forEach(x -> {
-            String key = getKey("edit_book", x.getBookId(), x.getChapterId());
+            String key = CacheUtil.getKey(Const.EDIT_BOOK_PRE, x.getBookId(), x.getChapterId());
             redisUtil.set(key, JsonUtils.toJsonStr(x));
         });
     }
 
+
     private List<BookRes> getRandom(List<BookRes> bookRes) {
         if (bookRes.size() <= 4) {
             return bookRes;