mirror of
https://github.com/201206030/novel-plus.git
synced 2025-07-30 15:08:46 +00:00
perf: 优化缓存模块
提升可读性 & 减小内存占用
This commit is contained in:
parent
7e27456a65
commit
c24c68ecaf
@ -9,7 +9,7 @@ import com.java2nb.novel.service.FriendLinkService;
|
|||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
@ -31,7 +31,7 @@ public class FriendLinkController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private FriendLinkService friendLinkService;
|
private FriendLinkService friendLinkService;
|
||||||
@Autowired
|
@Autowired
|
||||||
private RedisTemplate<Object, Object> redisTemplate;
|
private StringRedisTemplate redisTemplate;
|
||||||
|
|
||||||
@GetMapping()
|
@GetMapping()
|
||||||
@RequiresPermissions("novel:friendLink:friendLink")
|
@RequiresPermissions("novel:friendLink:friendLink")
|
||||||
|
@ -9,7 +9,7 @@ import com.java2nb.novel.service.NewsService;
|
|||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
@ -32,7 +32,7 @@ public class NewsController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private NewsService newsService;
|
private NewsService newsService;
|
||||||
@Autowired
|
@Autowired
|
||||||
private RedisTemplate<Object, Object> redisTemplate;
|
private StringRedisTemplate redisTemplate;
|
||||||
|
|
||||||
@GetMapping()
|
@GetMapping()
|
||||||
@RequiresPermissions("novel:news:news")
|
@RequiresPermissions("novel:news:news")
|
||||||
|
@ -8,7 +8,7 @@ public interface CacheKey {
|
|||||||
/**
|
/**
|
||||||
* 首页小说设置
|
* 首页小说设置
|
||||||
* */
|
* */
|
||||||
String INDEX_BOOK_SETTINGS_KEY = "indexBookSettingsKey";
|
String INDEX_BOOK_SETTINGS_KEY = "indexBookSettingsKey:v2";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 首页新闻
|
* 首页新闻
|
||||||
|
@ -1,55 +1,61 @@
|
|||||||
package com.java2nb.novel.core.cache;
|
package com.java2nb.novel.core.cache;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author 11797
|
* @author 11797
|
||||||
*/
|
*/
|
||||||
public interface CacheService {
|
public interface CacheService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据key获取缓存的String类型数据
|
* 根据key获取缓存的String类型数据
|
||||||
*/
|
*/
|
||||||
String get(String key);
|
String get(String key);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置String类型的缓存
|
* 设置String类型的缓存
|
||||||
*/
|
*/
|
||||||
void set(String key, String value);
|
void set(String key, String value);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置一个有过期时间的String类型的缓存,单位秒
|
* 设置一个有过期时间的String类型的缓存,单位秒
|
||||||
*/
|
*/
|
||||||
void set(String key, String value, long timeout);
|
void set(String key, String value, long timeout);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据key获取缓存的Object类型数据
|
* 根据key获取缓存的Object类型数据
|
||||||
*/
|
*/
|
||||||
Object getObject(String key);
|
<T> T getObject(String key, Class<T> clazz);
|
||||||
|
|
||||||
/**
|
<T> List<T> getList(String key, Class<T> clazz);
|
||||||
* 设置Object类型的缓存
|
|
||||||
*/
|
/**
|
||||||
void setObject(String key, Object value);
|
* 设置Object类型的缓存
|
||||||
|
*/
|
||||||
/**
|
void setObject(String key, Object value);
|
||||||
* 设置一个有过期时间的Object类型的缓存,单位秒
|
|
||||||
*/
|
/**
|
||||||
|
* 设置一个有过期时间的Object类型的缓存,单位秒
|
||||||
|
*/
|
||||||
void setObject(String key, Object value, long timeout);
|
void setObject(String key, Object value, long timeout);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据key删除缓存的数据
|
* 根据key删除缓存的数据
|
||||||
*/
|
*/
|
||||||
void del(String key);
|
void del(String key);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断是否存在一个key
|
* 判断是否存在一个key
|
||||||
* */
|
*/
|
||||||
boolean contains(String key);
|
boolean contains(String key);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置key过期时间
|
* 设置key过期时间
|
||||||
* */
|
*/
|
||||||
void expire(String key, long timeout);
|
void expire(String key, long timeout);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,23 +1,35 @@
|
|||||||
package com.java2nb.novel.core.cache.impl;
|
package com.java2nb.novel.core.cache.impl;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.java2nb.novel.core.cache.CacheService;
|
import com.java2nb.novel.core.cache.CacheService;
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xxy
|
* @author xxy
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@Service
|
@Service
|
||||||
public class RedisServiceImpl implements CacheService {
|
public class RedisServiceImpl implements CacheService {
|
||||||
|
|
||||||
private final StringRedisTemplate stringRedisTemplate;
|
private final StringRedisTemplate stringRedisTemplate;
|
||||||
|
|
||||||
private final RedisTemplate<Object, Object> redisTemplate;
|
private ObjectMapper objectMapper;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
objectMapper = new ObjectMapper();
|
||||||
|
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -37,34 +49,66 @@ public class RedisServiceImpl implements CacheService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object getObject(String key) {
|
public <T> T getObject(String key, Class<T> clazz) {
|
||||||
return redisTemplate.opsForValue().get(key);
|
String result = get(key);
|
||||||
|
if (result != null) {
|
||||||
|
try {
|
||||||
|
return objectMapper.readValue(result, clazz);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> List<T> getList(String key, Class<T> clazz) {
|
||||||
|
String result = get(key);
|
||||||
|
if (result != null) {
|
||||||
|
try {
|
||||||
|
return objectMapper.readValue(result,
|
||||||
|
objectMapper.getTypeFactory().constructCollectionType(List.class, clazz));
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setObject(String key, Object value) {
|
public void setObject(String key, Object value) {
|
||||||
redisTemplate.opsForValue().set(key, value);
|
if (value != null) {
|
||||||
|
try {
|
||||||
|
set(key, objectMapper.writeValueAsString(value));
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setObject(String key, Object value, long timeout) {
|
public void setObject(String key, Object value, long timeout) {
|
||||||
redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
|
if (value != null) {
|
||||||
|
try {
|
||||||
|
set(key, objectMapper.writeValueAsString(value), timeout);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void del(String key) {
|
public void del(String key) {
|
||||||
redisTemplate.delete(key);
|
|
||||||
stringRedisTemplate.delete(key);
|
stringRedisTemplate.delete(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean contains(String key) {
|
public boolean contains(String key) {
|
||||||
return redisTemplate.hasKey(key) || stringRedisTemplate.hasKey(key);
|
return stringRedisTemplate.hasKey(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void expire(String key, long timeout) {
|
public void expire(String key, long timeout) {
|
||||||
redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
|
|
||||||
stringRedisTemplate.expire(key, timeout, TimeUnit.SECONDS);
|
stringRedisTemplate.expire(key, timeout, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,17 +72,15 @@ public class CrawlController {
|
|||||||
if(url.startsWith("https://")||url.startsWith("http://")){
|
if(url.startsWith("https://")||url.startsWith("http://")){
|
||||||
String refreshCache="1";
|
String refreshCache="1";
|
||||||
if(!refreshCache.equals(isRefresh)) {
|
if(!refreshCache.equals(isRefresh)) {
|
||||||
Object cache = cacheService.getObject(CacheKey.BOOK_TEST_PARSE + url);
|
html = cacheService.get(CacheKey.BOOK_TEST_PARSE + url);
|
||||||
if (cache == null) {
|
if (html == null) {
|
||||||
isRefresh="1";
|
isRefresh="1";
|
||||||
}else {
|
|
||||||
html = (String) cache;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(refreshCache.equals(isRefresh)){
|
if(refreshCache.equals(isRefresh)){
|
||||||
html = HttpUtil.getByHttpClientWithChrome(url);
|
html = HttpUtil.getByHttpClientWithChrome(url);
|
||||||
if (html != null) {
|
if (html != null) {
|
||||||
cacheService.setObject(CacheKey.BOOK_TEST_PARSE + url, html, 60 * 10);
|
cacheService.set(CacheKey.BOOK_TEST_PARSE + url, html, 60 * 10);
|
||||||
}else{
|
}else{
|
||||||
resultMap.put("msg","html is null");
|
resultMap.put("msg","html is null");
|
||||||
return RestResult.ok(resultMap);
|
return RestResult.ok(resultMap);
|
||||||
|
@ -12,7 +12,6 @@ import io.github.xxyopen.util.IdWorker;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
|
||||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ public class BookController extends BaseController {
|
|||||||
* 查询首页小说设置列表数据
|
* 查询首页小说设置列表数据
|
||||||
*/
|
*/
|
||||||
@GetMapping("listBookSetting")
|
@GetMapping("listBookSetting")
|
||||||
public RestResult<Map<Byte, List<BookSettingVO>>> listBookSetting() {
|
public RestResult<Map<String, List<BookSettingVO>>> listBookSetting() {
|
||||||
return RestResult.ok(bookService.listBookSettingVO());
|
return RestResult.ok(bookService.listBookSettingVO());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,7 +82,7 @@ public class PageController extends BaseController {
|
|||||||
@RequestMapping(path = {"/", "/index", "/index.html"})
|
@RequestMapping(path = {"/", "/index", "/index.html"})
|
||||||
public String index(Model model) {
|
public String index(Model model) {
|
||||||
//加载小说首页小说基本信息线程
|
//加载小说首页小说基本信息线程
|
||||||
CompletableFuture<Map<Byte, List<BookSettingVO>>> bookCompletableFuture = CompletableFuture.supplyAsync(
|
CompletableFuture<Map<String, List<BookSettingVO>>> bookCompletableFuture = CompletableFuture.supplyAsync(
|
||||||
bookService::listBookSettingVO, threadPoolExecutor);
|
bookService::listBookSettingVO, threadPoolExecutor);
|
||||||
//加载首页新闻线程
|
//加载首页新闻线程
|
||||||
CompletableFuture<List<News>> newsCompletableFuture = CompletableFuture.supplyAsync(newsService::listIndexNews,
|
CompletableFuture<List<News>> newsCompletableFuture = CompletableFuture.supplyAsync(newsService::listIndexNews,
|
||||||
|
@ -16,9 +16,10 @@ public interface BookService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询首页小说设置列表数据
|
* 查询首页小说设置列表数据
|
||||||
|
*
|
||||||
* @return
|
* @return
|
||||||
* */
|
*/
|
||||||
Map<Byte, List<BookSettingVO>> listBookSettingVO();
|
Map<String, List<BookSettingVO>> listBookSettingVO();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询首页点击榜单数据
|
* 查询首页点击榜单数据
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package com.java2nb.novel.service.impl;
|
package com.java2nb.novel.service.impl;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import com.github.pagehelper.PageHelper;
|
import com.github.pagehelper.PageHelper;
|
||||||
import com.java2nb.novel.core.cache.CacheKey;
|
import com.java2nb.novel.core.cache.CacheKey;
|
||||||
import com.java2nb.novel.core.cache.CacheService;
|
import com.java2nb.novel.core.cache.CacheService;
|
||||||
@ -25,7 +24,6 @@ import io.github.xxyopen.web.util.BeanUtil;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
import org.apache.http.client.utils.DateUtils;
|
import org.apache.http.client.utils.DateUtils;
|
||||||
import org.mybatis.dynamic.sql.SortSpecification;
|
import org.mybatis.dynamic.sql.SortSpecification;
|
||||||
import org.mybatis.dynamic.sql.render.RenderingStrategies;
|
import org.mybatis.dynamic.sql.render.RenderingStrategies;
|
||||||
@ -48,7 +46,6 @@ import java.util.concurrent.ThreadPoolExecutor;
|
|||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static com.java2nb.novel.mapper.BookCategoryDynamicSqlSupport.bookCategory;
|
import static com.java2nb.novel.mapper.BookCategoryDynamicSqlSupport.bookCategory;
|
||||||
import static com.java2nb.novel.mapper.BookCategoryDynamicSqlSupport.sort;
|
|
||||||
import static com.java2nb.novel.mapper.BookCommentDynamicSqlSupport.bookComment;
|
import static com.java2nb.novel.mapper.BookCommentDynamicSqlSupport.bookComment;
|
||||||
import static com.java2nb.novel.mapper.BookContentDynamicSqlSupport.bookContent;
|
import static com.java2nb.novel.mapper.BookContentDynamicSqlSupport.bookContent;
|
||||||
import static com.java2nb.novel.mapper.BookContentDynamicSqlSupport.content;
|
import static com.java2nb.novel.mapper.BookContentDynamicSqlSupport.content;
|
||||||
@ -108,19 +105,19 @@ public class BookServiceImpl implements BookService {
|
|||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
@Override
|
@Override
|
||||||
public Map<Byte, List<BookSettingVO>> listBookSettingVO() {
|
public Map<String, List<BookSettingVO>> listBookSettingVO() {
|
||||||
String result = cacheService.get(CacheKey.INDEX_BOOK_SETTINGS_KEY);
|
List<BookSettingVO> list = cacheService.getList(CacheKey.INDEX_BOOK_SETTINGS_KEY, BookSettingVO.class);
|
||||||
if (result == null || result.length() < Constants.OBJECT_JSON_CACHE_EXIST_LENGTH) {
|
if (list == null || list.isEmpty()) {
|
||||||
List<BookSettingVO> list = bookSettingMapper.listVO();
|
list = bookSettingMapper.listVO();
|
||||||
if (list.size() == 0) {
|
if (list.isEmpty()) {
|
||||||
//如果首页小说没有被设置,则初始化首页小说设置
|
//如果首页小说没有被设置,则初始化首页小说设置
|
||||||
list = initIndexBookSetting();
|
list = initIndexBookSetting();
|
||||||
}
|
}
|
||||||
result = new ObjectMapper().writeValueAsString(
|
cacheService.setObject(CacheKey.INDEX_BOOK_SETTINGS_KEY, list, 3600 * 24);
|
||||||
list.stream().collect(Collectors.groupingBy(BookSettingVO::getType)));
|
|
||||||
cacheService.set(CacheKey.INDEX_BOOK_SETTINGS_KEY, result, 3600 * 24);
|
|
||||||
}
|
}
|
||||||
return new ObjectMapper().readValue(result, Map.class);
|
return list.stream().collect(
|
||||||
|
Collectors.groupingBy(book -> book.getType().toString())
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -170,11 +167,10 @@ public class BookServiceImpl implements BookService {
|
|||||||
return new ArrayList<>(0);
|
return new ArrayList<>(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Book> listClickRank() {
|
public List<Book> listClickRank() {
|
||||||
List<Book> result = (List<Book>) cacheService.getObject(CacheKey.INDEX_CLICK_BANK_BOOK_KEY);
|
List<Book> result = cacheService.getList(CacheKey.INDEX_CLICK_BANK_BOOK_KEY, Book.class);
|
||||||
if (result == null || result.size() == 0) {
|
if (result == null || result.isEmpty()) {
|
||||||
result = listRank((byte) 0, 10);
|
result = listRank((byte) 0, 10);
|
||||||
cacheService.setObject(CacheKey.INDEX_CLICK_BANK_BOOK_KEY, result, 5000);
|
cacheService.setObject(CacheKey.INDEX_CLICK_BANK_BOOK_KEY, result, 5000);
|
||||||
}
|
}
|
||||||
@ -183,8 +179,8 @@ public class BookServiceImpl implements BookService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Book> listNewRank() {
|
public List<Book> listNewRank() {
|
||||||
List<Book> result = (List<Book>) cacheService.getObject(CacheKey.INDEX_NEW_BOOK_KEY);
|
List<Book> result = cacheService.getList(CacheKey.INDEX_NEW_BOOK_KEY, Book.class);
|
||||||
if (result == null || result.size() == 0) {
|
if (result == null || result.isEmpty()) {
|
||||||
result = listRank((byte) 1, 10);
|
result = listRank((byte) 1, 10);
|
||||||
cacheService.setObject(CacheKey.INDEX_NEW_BOOK_KEY, result, 3600);
|
cacheService.setObject(CacheKey.INDEX_NEW_BOOK_KEY, result, 3600);
|
||||||
}
|
}
|
||||||
@ -193,8 +189,8 @@ public class BookServiceImpl implements BookService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<BookVO> listUpdateRank() {
|
public List<BookVO> listUpdateRank() {
|
||||||
List<BookVO> result = (List<BookVO>) cacheService.getObject(CacheKey.INDEX_UPDATE_BOOK_KEY);
|
List<BookVO> result = cacheService.getList(CacheKey.INDEX_UPDATE_BOOK_KEY, BookVO.class);
|
||||||
if (result == null || result.size() == 0) {
|
if (result == null || result.isEmpty()) {
|
||||||
List<Book> bookPOList = listRank((byte) 2, 23);
|
List<Book> bookPOList = listRank((byte) 2, 23);
|
||||||
result = BeanUtil.copyList(bookPOList, BookVO.class);
|
result = BeanUtil.copyList(bookPOList, BookVO.class);
|
||||||
cacheService.setObject(CacheKey.INDEX_UPDATE_BOOK_KEY, result, 60 * 10);
|
cacheService.setObject(CacheKey.INDEX_UPDATE_BOOK_KEY, result, 60 * 10);
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package com.java2nb.novel.service.impl;
|
package com.java2nb.novel.service.impl;
|
||||||
|
|
||||||
import io.github.xxyopen.web.util.BeanUtil;
|
|
||||||
import com.java2nb.novel.service.FriendLinkService;
|
import com.java2nb.novel.service.FriendLinkService;
|
||||||
import com.java2nb.novel.core.cache.CacheKey;
|
import com.java2nb.novel.core.cache.CacheKey;
|
||||||
import com.java2nb.novel.core.cache.CacheService;
|
import com.java2nb.novel.core.cache.CacheService;
|
||||||
@ -31,16 +30,16 @@ public class FriendLinkServiceImpl implements FriendLinkService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<FriendLink> listIndexLink() {
|
public List<FriendLink> listIndexLink() {
|
||||||
List<FriendLink> result = (List<FriendLink>) cacheService.getObject(CacheKey.INDEX_LINK_KEY);
|
List<FriendLink> result = cacheService.getList(CacheKey.INDEX_LINK_KEY, FriendLink.class);
|
||||||
if(result == null || result.size() == 0) {
|
if (result == null || result.isEmpty()) {
|
||||||
SelectStatementProvider selectStatement = select(linkName,linkUrl)
|
SelectStatementProvider selectStatement = select(linkName, linkUrl)
|
||||||
.from(friendLink)
|
.from(friendLink)
|
||||||
.where(isOpen,isEqualTo((byte)1))
|
.where(isOpen, isEqualTo((byte) 1))
|
||||||
.orderBy(sort)
|
.orderBy(sort)
|
||||||
.build()
|
.build()
|
||||||
.render(RenderingStrategies.MYBATIS3);
|
.render(RenderingStrategies.MYBATIS3);
|
||||||
result = friendLinkMapper.selectMany(selectStatement);
|
result = friendLinkMapper.selectMany(selectStatement);
|
||||||
cacheService.setObject(CacheKey.INDEX_LINK_KEY,result,60 * 60 * 24);
|
cacheService.setObject(CacheKey.INDEX_LINK_KEY, result, 60 * 60 * 24);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import com.java2nb.novel.service.LikeService;
|
|||||||
import jakarta.annotation.PostConstruct;
|
import jakarta.annotation.PostConstruct;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.data.redis.core.script.DefaultRedisScript;
|
import org.springframework.data.redis.core.script.DefaultRedisScript;
|
||||||
import org.springframework.scripting.support.StaticScriptSource;
|
import org.springframework.scripting.support.StaticScriptSource;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@ -20,7 +20,7 @@ import java.util.Collections;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class LikeServiceImpl implements LikeService {
|
public class LikeServiceImpl implements LikeService {
|
||||||
|
|
||||||
private final RedisTemplate<Object, Object> redisTemplate;
|
private final StringRedisTemplate redisTemplate;
|
||||||
|
|
||||||
private DefaultRedisScript<Long> toggleLikeScript;
|
private DefaultRedisScript<Long> toggleLikeScript;
|
||||||
|
|
||||||
@ -33,19 +33,19 @@ public class LikeServiceImpl implements LikeService {
|
|||||||
public void init() {
|
public void init() {
|
||||||
// Lua 脚本保证原子性操作
|
// Lua 脚本保证原子性操作
|
||||||
String script = """
|
String script = """
|
||||||
local key = KEYS[1]
|
local key = KEYS[1]
|
||||||
local userId = ARGV[1]
|
local userId = ARGV[1]
|
||||||
|
|
||||||
local isLiked = redis.call('SISMEMBER', key, userId)
|
local isLiked = redis.call('SISMEMBER', key, userId)
|
||||||
|
|
||||||
if isLiked == 1 then
|
if isLiked == 1 then
|
||||||
redis.call('SREM', key, userId)
|
redis.call('SREM', key, userId)
|
||||||
else
|
else
|
||||||
redis.call('SADD', key, userId)
|
redis.call('SADD', key, userId)
|
||||||
end
|
end
|
||||||
|
|
||||||
return redis.call('SCARD', key)
|
return redis.call('SCARD', key)
|
||||||
""";
|
""";
|
||||||
|
|
||||||
toggleLikeScript = new DefaultRedisScript<>();
|
toggleLikeScript = new DefaultRedisScript<>();
|
||||||
toggleLikeScript.setScriptSource(new StaticScriptSource(script));
|
toggleLikeScript.setScriptSource(new StaticScriptSource(script));
|
||||||
@ -89,6 +89,6 @@ public class LikeServiceImpl implements LikeService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Long executeToggle(String key, Long userId) {
|
private Long executeToggle(String key, Long userId) {
|
||||||
return redisTemplate.execute(toggleLikeScript, Collections.singletonList(key), userId);
|
return redisTemplate.execute(toggleLikeScript, Collections.singletonList(key), String.valueOf(userId));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,16 +36,16 @@ public class NewsServiceImpl implements NewsService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<News> listIndexNews() {
|
public List<News> listIndexNews() {
|
||||||
List<News> result = (List<News>) cacheService.getObject(CacheKey.INDEX_NEWS_KEY);
|
List<News> result = cacheService.getList(CacheKey.INDEX_NEWS_KEY, News.class);
|
||||||
if(result == null || result.size() == 0) {
|
if (result == null || result.isEmpty()) {
|
||||||
SelectStatementProvider selectStatement = select(id, catName, catId, title,createTime)
|
SelectStatementProvider selectStatement = select(id, catName, catId, title, createTime)
|
||||||
.from(news)
|
.from(news)
|
||||||
.orderBy(createTime.descending())
|
.orderBy(createTime.descending())
|
||||||
.limit(2)
|
.limit(2)
|
||||||
.build()
|
.build()
|
||||||
.render(RenderingStrategies.MYBATIS3);
|
.render(RenderingStrategies.MYBATIS3);
|
||||||
result = newsMapper.selectMany(selectStatement);
|
result = newsMapper.selectMany(selectStatement);
|
||||||
cacheService.setObject(CacheKey.INDEX_NEWS_KEY,result,60 * 60 * 12);
|
cacheService.setObject(CacheKey.INDEX_NEWS_KEY, result, 60 * 60 * 12);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -53,25 +53,25 @@ public class NewsServiceImpl implements NewsService {
|
|||||||
@Override
|
@Override
|
||||||
public News queryNewsInfo(Long newsId) {
|
public News queryNewsInfo(Long newsId) {
|
||||||
SelectStatementProvider selectStatement = select(news.allColumns())
|
SelectStatementProvider selectStatement = select(news.allColumns())
|
||||||
.from(news)
|
.from(news)
|
||||||
.where(id,isEqualTo(newsId))
|
.where(id, isEqualTo(newsId))
|
||||||
.build()
|
.build()
|
||||||
.render(RenderingStrategies.MYBATIS3);
|
.render(RenderingStrategies.MYBATIS3);
|
||||||
return newsMapper.selectMany(selectStatement).get(0);
|
return newsMapper.selectMany(selectStatement).get(0);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PageBean<News> listByPage(int page, int pageSize) {
|
public PageBean<News> listByPage(int page, int pageSize) {
|
||||||
PageHelper.startPage(page,pageSize);
|
PageHelper.startPage(page, pageSize);
|
||||||
SelectStatementProvider selectStatement = select(id, catName, catId, title,createTime)
|
SelectStatementProvider selectStatement = select(id, catName, catId, title, createTime)
|
||||||
.from(news)
|
.from(news)
|
||||||
.orderBy(createTime.descending())
|
.orderBy(createTime.descending())
|
||||||
.build()
|
.build()
|
||||||
.render(RenderingStrategies.MYBATIS3);
|
.render(RenderingStrategies.MYBATIS3);
|
||||||
List<News> news = newsMapper.selectMany(selectStatement);
|
List<News> news = newsMapper.selectMany(selectStatement);
|
||||||
PageBean<News> pageBean = PageBuilder.build(news);
|
PageBean<News> pageBean = PageBuilder.build(news);
|
||||||
pageBean.setList(BeanUtil.copyList(news,NewsVO.class));
|
pageBean.setList(BeanUtil.copyList(news, NewsVO.class));
|
||||||
return pageBean;
|
return pageBean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user