|
@@ -42,7 +42,6 @@ import java.io.BufferedReader;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStreamReader;
|
|
|
import java.util.*;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
@@ -100,7 +99,7 @@ public class BookServiceImpl implements BookService {
|
|
|
}
|
|
|
|
|
|
public List<BookRes> getRecommendFromCache(QueryVO queryVO) {
|
|
|
- String key = "recommend_" + queryVO.getSex() + "_" + queryVO.getPage() + "_" + queryVO.getSize();
|
|
|
+ String key = getKey("recommend", queryVO.getSex(), queryVO.getPage(), queryVO.getSize());
|
|
|
Object o = redisUtil.get(key);
|
|
|
if (o == null) {
|
|
|
return setRecommendCache(key, queryVO);
|
|
@@ -128,7 +127,7 @@ public class BookServiceImpl implements BookService {
|
|
|
|
|
|
private synchronized ArrayList<BlockRes> setBlockByPageIdCache(Integer pageId) {
|
|
|
//缓存
|
|
|
- String key = pageId.toString();
|
|
|
+ String key = getKey("block", pageId.toString());
|
|
|
Object o = redisUtil.get(key);
|
|
|
if (o != null) {
|
|
|
return (ArrayList<BlockRes>) o;
|
|
@@ -278,17 +277,16 @@ public class BookServiceImpl implements BookService {
|
|
|
}
|
|
|
|
|
|
//缓存
|
|
|
- String key = "chaptersByBookId" + bookId;
|
|
|
+ String key = getKey("chapters", bookId);
|
|
|
Object o = redisUtil.get(key);
|
|
|
if (o != null) {
|
|
|
return (List<Chapter>) o;
|
|
|
} else {
|
|
|
- return setChaptersByBookIdCache(bookId);
|
|
|
+ return setChaptersByBookIdCache(key, bookId);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private synchronized List<Chapter> setChaptersByBookIdCache(Long bookId) {
|
|
|
- String key = "chaptersByBookId" + bookId;
|
|
|
+ private synchronized List<Chapter> setChaptersByBookIdCache(String key, Long bookId) {
|
|
|
Object o = redisUtil.get(key);
|
|
|
if (o != null) {
|
|
|
return (List<Chapter>) o;
|
|
@@ -335,7 +333,7 @@ public class BookServiceImpl implements BookService {
|
|
|
|
|
|
@Override
|
|
|
public List<SearchKeyword> getSearchKeywordBySex(String sex) {
|
|
|
- String key = "getSearchKeywordBySex" + sex;
|
|
|
+ String key = getKey("searchKeyword", sex);
|
|
|
Object o = redisUtil.get(key);
|
|
|
if (o != null) {
|
|
|
return (List<SearchKeyword>) o;
|
|
@@ -378,7 +376,7 @@ public class BookServiceImpl implements BookService {
|
|
|
|
|
|
@Override
|
|
|
public List<BookRes> smartRecommand(QueryVO queryVO) {
|
|
|
- String key = "smartRecommand_" + queryVO.getSex();
|
|
|
+ String key = getKey("smartRecommand", queryVO.getSex());
|
|
|
Object o = redisUtil.get(key);
|
|
|
if (o == null) {
|
|
|
List<BookRes> bookRes = setSmartRecommandCache(queryVO, key);
|
|
@@ -460,7 +458,7 @@ public class BookServiceImpl implements BookService {
|
|
|
// 更新免费看点到 user
|
|
|
int i = userMapper.updateUserKandian(user, user.getKandian(), freeCount - book.getPrice(), DateUtils.getNow());
|
|
|
checkUpdate(i);
|
|
|
- updateUserByIdCache(user.getId());
|
|
|
+ redisUtil.remove(getUserKey(user.getId()));
|
|
|
|
|
|
} else if (user.getKandian() + freeCount > book.getPrice()) { //免费+充值足够
|
|
|
Integer left = user.getKandian() + freeCount;
|
|
@@ -525,7 +523,7 @@ public class BookServiceImpl implements BookService {
|
|
|
}
|
|
|
|
|
|
private BookContent getBookContent(BookContentVO bookContentVO) {
|
|
|
- String key = "" + bookContentVO.getBookId() + bookContentVO.getContentId();
|
|
|
+ String key = getKey("bookContent", bookContentVO.getBookId(), bookContentVO.getContentId());
|
|
|
Object o = redisUtil.get(key);
|
|
|
if (o != null) {
|
|
|
return (BookContent) o;
|
|
@@ -870,7 +868,7 @@ public class BookServiceImpl implements BookService {
|
|
|
* @return
|
|
|
*/
|
|
|
public int getDefaultFreeChapterNum() {
|
|
|
- String key = "defaultFreeChapterNum";
|
|
|
+ String key = getKey("defaultFreeChapterNum");
|
|
|
Object o = redisUtil.get(key);
|
|
|
if (o == null) {
|
|
|
int book_free_chapter_num = setBook_free_chapter_num(key);
|
|
@@ -891,17 +889,16 @@ public class BookServiceImpl implements BookService {
|
|
|
}
|
|
|
|
|
|
public Book getBookById(Long id) {
|
|
|
- String key = "book" + id;
|
|
|
+ String key = getKey("book", id);
|
|
|
Object o = redisUtil.get(key);
|
|
|
if (o == null) {
|
|
|
- return updateBookByIdToRedis(id);
|
|
|
+ return updateBookByIdToRedis(key, id);
|
|
|
} else {
|
|
|
return (Book) o;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public synchronized Book updateBookByIdToRedis(Long id) {
|
|
|
- String key = "book" + id;
|
|
|
+ public synchronized Book updateBookByIdToRedis(String key, Long id) {
|
|
|
Object o = redisUtil.get(key);
|
|
|
if (o != null) {
|
|
|
return (Book) o;
|
|
@@ -912,7 +909,7 @@ public class BookServiceImpl implements BookService {
|
|
|
}
|
|
|
|
|
|
public User getUserById(Long id) {
|
|
|
- String key = "user" + id;
|
|
|
+ String key = getUserKey(id);
|
|
|
Object o = redisUtil.get(key);
|
|
|
if (o == null) {
|
|
|
return updateUserByIdCache(id);
|
|
@@ -921,8 +918,12 @@ public class BookServiceImpl implements BookService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private String getUserKey(Long id) {
|
|
|
+ return getKey("user", id);
|
|
|
+ }
|
|
|
+
|
|
|
public synchronized User updateUserByIdCache(Long id) {
|
|
|
- String key = "user" + id;
|
|
|
+ String key = getUserKey(id);
|
|
|
Object o = redisUtil.get(key);
|
|
|
if (o != null) {
|
|
|
return (User) o;
|
|
@@ -933,7 +934,7 @@ public class BookServiceImpl implements BookService {
|
|
|
}
|
|
|
|
|
|
public String getCategoryName(Integer id) {
|
|
|
- String key = "category";
|
|
|
+ String key = getKey("category");
|
|
|
Object o = redisUtil.hashGet(key, id);
|
|
|
if (o == null) {
|
|
|
String name = setCategoryCache(id, key);
|
|
@@ -956,16 +957,12 @@ public class BookServiceImpl implements BookService {
|
|
|
name = x.getName();
|
|
|
}
|
|
|
}
|
|
|
- redisTemplate.expire(key, redisUtil.getRandomTime(), TimeUnit.MINUTES);
|
|
|
+ redisUtil.expire(key);
|
|
|
return name;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private RedisTemplate redisTemplate;
|
|
|
-
|
|
|
public String getEditBookContentById(Long bookId, Long chapterId) {
|
|
|
- String key = "edit_book" + bookId + chapterId;
|
|
|
+ String key = getKey("edit_book", bookId, chapterId);
|
|
|
Object o = redisUtil.get(key);
|
|
|
if (o == null) {
|
|
|
return null;
|
|
@@ -974,8 +971,16 @@ public class BookServiceImpl implements BookService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public String getKey(Object... keys) {
|
|
|
+ StringBuilder sb = new StringBuilder("t_");
|
|
|
+ for (Object key : keys) {
|
|
|
+ sb.append(key).append("_");
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
public synchronized void updateEditBookById(Long bookId, Long chapterId) {
|
|
|
- String key = "edit_book" + bookId + chapterId;
|
|
|
+ String key = getKey("edit_book", bookId, chapterId);
|
|
|
EditBook obj = editBookMapper.selectOneByExampleSelective(
|
|
|
EditBookExample.newAndCreateCriteria()
|
|
|
.andBookIdEqualTo(bookId)
|
|
@@ -991,7 +996,7 @@ 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 = "edit_book" + x.getBookId() + x.getChapterId();
|
|
|
+ String key = getKey("edit_book", x.getBookId(), x.getChapterId());
|
|
|
redisUtil.set(key, x.getContent());
|
|
|
});
|
|
|
}
|