接口缓存优化

This commit is contained in:
mashuai 2024-10-28 18:36:27 +08:00
parent 93f01d79bc
commit db1d297b6d
10 changed files with 215 additions and 30 deletions

View File

@ -111,4 +111,14 @@ public class Constants {
public static final String SM4_IV = "1234567890abcdef1234567890abcdef";
/**
* 边带设备接入申请RedisKey
*/
public static final String TB_BD_RECORD_REDIS_KEY = "tb_bd_record";
/**
* 预警告警页面RedisKey
*/
public static final String TB_DEV_ATTRIBUTE_REDIS_KEY = "Tb_dev_Attribute";
}

View File

@ -3,6 +3,7 @@ package com.bonus.base.controller;
import com.bonus.base.domain.TbBdDeviceRecord;
import com.bonus.base.domain.TbBdRecord;
import com.bonus.base.service.TbBdRecordService;
import com.bonus.base.vo.PageResultVo;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
@ -48,9 +49,8 @@ public class TbBdRecordController extends BaseController {
@GetMapping("/list")
public AjaxResult queryByPage(TbBdRecord tbBdRecord) {
startPage();
List<TbBdRecord> list = tbBdRecordService.getAll(tbBdRecord);
return AjaxResult.success(getDataTable(list));
PageResultVo<TbBdRecord> data = tbBdRecordService.getAll(tbBdRecord);
return AjaxResult.success(data);
}

View File

@ -4,6 +4,7 @@ import com.alibaba.nacos.common.utils.CollectionUtils;
import com.bonus.base.domain.TbDevAttribute;
import com.bonus.base.domain.TbDevDataRecord;
import com.bonus.base.service.TbDevAttributeService;
import com.bonus.base.vo.PageResultVo;
import com.bonus.base.vo.TbDevAttributeVo;
import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.controller.BaseController;
@ -13,6 +14,7 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;
/**
@ -39,9 +41,8 @@ public class TbDevAttributeController extends BaseController {
@GetMapping("/list")
public AjaxResult queryByPage(TbDevAttribute record) {
startPage();
List<TbDevAttributeVo> list = tbDevAttributeService.queryAll(record);
return AjaxResult.success(getDataTable(list));
PageResultVo<TbDevAttributeVo> data = tbDevAttributeService.queryAll(record);
return AjaxResult.success(data);
}
/**
@ -81,7 +82,8 @@ public class TbDevAttributeController extends BaseController {
*/
@PostMapping("/export")
public void export(HttpServletResponse response, TbDevAttribute record) {
List<TbDevAttributeVo> list = tbDevAttributeService.queryAll(record);
PageResultVo<TbDevAttributeVo> pageResult = tbDevAttributeService.queryAll(record);
List<TbDevAttributeVo> list = pageResult != null ? pageResult.getRows() : Collections.emptyList();
if (CollectionUtils.isNotEmpty(list)) {
for (TbDevAttributeVo attribute : list) {
if (attribute.getWarnName() == null) {

View File

@ -156,4 +156,14 @@ public class TbBdRecord implements Serializable {
private List<TbBdDeviceRecord> recordList;
private static final long serialVersionUID = 1L;
/**
* 分页参数条数
*/
private int pageNum;
/**
* 分页参数页码
*/
private int pageSize;
}

View File

@ -109,5 +109,15 @@ public class TbDevAttribute implements Serializable {
@ApiModelProperty(value="结束时间")
private String endTime;
/**
* 分页参数条数
*/
private int pageNum;
/**
* 分页参数页码
*/
private int pageSize;
private static final long serialVersionUID = 1L;
}

View File

@ -2,6 +2,7 @@ package com.bonus.base.service;
import com.bonus.base.domain.TbBdDeviceRecord;
import com.bonus.base.domain.TbBdRecord;
import com.bonus.base.vo.PageResultVo;
import com.bonus.common.core.web.domain.AjaxResult;
import java.util.List;
@ -53,7 +54,7 @@ public interface TbBdRecordService {
* @param record
* @return
*/
List<TbBdRecord> getAll(TbBdRecord record);
PageResultVo<TbBdRecord> getAll(TbBdRecord record);
/**
* 审核

View File

@ -3,6 +3,7 @@ package com.bonus.base.service;
import java.util.List;
import com.bonus.base.domain.TbDevAttribute;
import com.bonus.base.domain.TbDevDataRecord;
import com.bonus.base.vo.PageResultVo;
import com.bonus.base.vo.TbDevAttributeVo;
import com.bonus.common.core.web.domain.AjaxResult;
@ -25,7 +26,7 @@ public interface TbDevAttributeService{
int updateBatch(List<TbDevAttribute> list);
List<TbDevAttributeVo> queryAll(TbDevAttribute record);
PageResultVo<TbDevAttributeVo> queryAll(TbDevAttribute record);
/**
* 预警告警异常记录处理

View File

@ -2,13 +2,18 @@ package com.bonus.base.service.impl;
import cn.hutool.core.util.PhoneUtil;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.bonus.base.config.Constants;
import com.bonus.base.config.ExceptionEnum;
import com.bonus.base.domain.TbBdDeviceRecord;
import com.bonus.base.mapper.TbBdDeviceRecordMapper;
import com.bonus.base.vo.PageResultVo;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.utils.encryption.Sm4Utils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.redis.service.RedisService;
import com.bonus.common.security.utils.SecurityUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
@ -17,12 +22,15 @@ import com.bonus.base.domain.TbBdRecord;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import com.bonus.base.mapper.TbBdRecordMapper;
import com.bonus.base.service.TbBdRecordService;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
/**
*@PackagePath: com.bonus.base.service.impl
*@author : 阮世耀
@ -39,6 +47,9 @@ public class TbBdRecordServiceImpl implements TbBdRecordService{
@Autowired
private TbBdDeviceRecordMapper tbBdDeviceRecordMapper;
@Resource
private RedisService redisService;
@Override
@Transactional(rollbackFor = Exception.class)
public AjaxResult deleteByPrimaryKey(Long id) {
@ -174,16 +185,36 @@ public class TbBdRecordServiceImpl implements TbBdRecordService{
}
@Override
public List<TbBdRecord> getAll(TbBdRecord record){
List<TbBdRecord> list = tbBdRecordMapper.getAll(record);
if (list != null && list.size() > 0) {
List<TbBdRecord> recordList = list.stream().filter(tbBdRecord -> tbBdRecord.getDepartName() != null).collect(Collectors.toList());
for (TbBdRecord tbBdRecord : recordList) {
//对手机号进行解密处理
tbBdRecord.setRelPhone(Sm4Utils.decode(tbBdRecord.getRelPhone()));
}
public PageResultVo<TbBdRecord> getAll(TbBdRecord record){
List<TbBdRecord> list = redisService.getCacheObject(Constants.TB_BD_RECORD_REDIS_KEY);
if (list == null || list.isEmpty()) {
// 从数据库获取数据并缓存
list = tbBdRecordMapper.getAll(record);
redisService.setCacheObject(Constants.TB_BD_RECORD_REDIS_KEY, list, 10L, TimeUnit.MINUTES);
}
return list;
// 处理手机号解密
if (list != null && list.size() > 0) {
list.stream()
.filter(tbBdRecord -> tbBdRecord.getDepartName() != null)
.forEach(tbBdRecord -> tbBdRecord.setRelPhone(Sm4Utils.decode(tbBdRecord.getRelPhone())));
}
// 手动计算分页信息
int pageNum = record.getPageNum() == 0 ? 1 : record.getPageNum();
int pageSize = record.getPageSize() == 0 ? 10 : record.getPageSize();
int total = list.size();
int totalPages = (int) Math.ceil((double) total / pageSize);
// 手动分页获取当前页的数据
int startIndex = (pageNum - 1) * pageSize;
int endIndex = Math.min(startIndex + pageSize, total);
List<TbBdRecord> currentPageData = list.subList(startIndex, endIndex);
// 构建返回结果对象
PageResultVo<TbBdRecord> pageResult = new PageResultVo<>();
pageResult.setTotal(total);
pageResult.setTotalPageCount(totalPages);
pageResult.setRows(currentPageData);
pageResult.setPageNum(pageNum);
pageResult.setPageSize(pageSize);
return pageResult;
}
/**

View File

@ -1,17 +1,26 @@
package com.bonus.base.service.impl;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.bonus.base.config.Constants;
import com.bonus.base.domain.TbBdRecord;
import com.bonus.base.domain.TbDevDataRecord;
import com.bonus.base.vo.PageResultVo;
import com.bonus.base.vo.TbDevAttributeVo;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.redis.service.RedisService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.concurrent.TimeUnit;
import com.bonus.base.mapper.TbDevAttributeMapper;
import com.bonus.base.domain.TbDevAttribute;
import com.bonus.base.service.TbDevAttributeService;
import javax.annotation.Resource;
/**
*@PackagePath: com.bonus.base.service.impl
*@author : 阮世耀
@ -25,6 +34,9 @@ public class TbDevAttributeServiceImpl implements TbDevAttributeService{
@Autowired
private TbDevAttributeMapper tbDevAttributeMapper;
@Resource
private RedisService redisService;
@Override
public int deleteByPrimaryKey(Long id) {
return tbDevAttributeMapper.deleteByPrimaryKey(id);
@ -61,19 +73,37 @@ public class TbDevAttributeServiceImpl implements TbDevAttributeService{
}
@Override
public List<TbDevAttributeVo> queryAll(TbDevAttribute record){
List<TbDevAttributeVo> list = tbDevAttributeMapper.queryAll(record);
if (CollectionUtils.isNotEmpty(list)) {
for (TbDevAttributeVo attribute : list) {
attribute.setWarnName(attribute.getDevName() + attribute.getJcName() + "检测异常");
if ("0".equals(attribute.getStatus())) {
attribute.setStatus("未处置");
} else if ("1".equals(attribute.getStatus())) {
attribute.setStatus("已处置");
}
}
public PageResultVo<TbDevAttributeVo> queryAll(TbDevAttribute record) {
//先从缓存中查询
List<TbDevAttributeVo> list = redisService.getCacheObject(Constants.TB_DEV_ATTRIBUTE_REDIS_KEY);
if (list == null || list.isEmpty()) {
// 从数据库获取数据并缓存
list = tbDevAttributeMapper.queryAll(record);
redisService.setCacheObject(Constants.TB_DEV_ATTRIBUTE_REDIS_KEY, list, 10L, TimeUnit.MINUTES);
}
return list;
if (CollectionUtils.isNotEmpty(list)) {
list.forEach(attribute -> {
attribute.setWarnName(attribute.getDevName() + attribute.getJcName() + "检测异常");
attribute.setStatus("0".equals(attribute.getStatus()) ? "未处置" : "1".equals(attribute.getStatus()) ? "已处置" : attribute.getStatus());
});
}
// 手动计算分页信息
int pageNum = record.getPageNum() == 0 ? 1 : record.getPageNum();
int pageSize = record.getPageSize() == 0 ? 10 : record.getPageSize();
int total = list.size();
int totalPages = (int) Math.ceil((double) total / pageSize);
// 手动分页获取当前页的数据
int startIndex = (pageNum - 1) * pageSize;
int endIndex = Math.min(startIndex + pageSize, total);
List<TbDevAttributeVo> currentPageData = list.subList(startIndex, endIndex);
// 构建返回结果对象
PageResultVo<TbDevAttributeVo> pageResult = new PageResultVo<>();
pageResult.setTotal(total);
pageResult.setTotalPageCount(totalPages);
pageResult.setRows(currentPageData);
pageResult.setPageNum(pageNum);
pageResult.setPageSize(pageSize);
return pageResult;
}
/**

View File

@ -0,0 +1,90 @@
package com.bonus.base.vo;
import java.io.Serializable;
import java.util.List;
/**
* @Author ma_sh
* @create 2024/10/28 15:54
*/
public class PageResultVo<T> implements Serializable {
private static final long serialVersionUID = -3915666721968467471L;
private int pageNum;
private int pageSize;
private long total;
private int totalPageCount;
private List<T> rows;
public PageResultVo() {
}
public PageResultVo(List<T> rows, int total) {
this.rows = rows;
this.total = (long)total;
}
public PageResultVo(int pageNum, int pageSize, long total, int totalPageCount, List<T> rows) {
this.pageNum = pageNum;
this.pageSize = pageSize;
this.total = total;
this.totalPageCount = totalPageCount;
this.rows = rows;
}
public boolean getFirstPage() {
return this.getPageNum() == 1;
}
public long getPrevPage() {
return (long)Math.max(1, this.getPageNum() - 1);
}
public long getNextPage() {
return (long)(this.getPageNum() + 1);
}
public boolean getLastPage() {
return this.getPageNum() >= this.getTotalPageCount();
}
public int getPageNum() {
return this.pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getPageSize() {
return this.pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public long getTotal() {
return this.total;
}
public void setTotal(long total) {
this.total = total;
}
public int getTotalPageCount() {
return this.totalPageCount;
}
public void setTotalPageCount(int totalPageCount) {
this.totalPageCount = totalPageCount;
}
public List<T> getRows() {
return this.rows;
}
public void setRows(List<T> rows) {
this.rows = rows;
}
}