缓存配置 - xxl-job配置 - 前端AGV地图png转base64

This commit is contained in:
aikai 2025-01-13 16:23:11 +08:00
parent edbea240f8
commit 6bf1f7da7f
4 changed files with 17 additions and 85 deletions

View File

@ -2,6 +2,8 @@ package cn.iocoder.yudao.module.system;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
/**
* 项目的启动类
@ -13,6 +15,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
* @author 芋道源码
*/
@SpringBootApplication
@EnableCaching
public class SystemServerApplication {
public static void main(String[] args) {

View File

@ -1,65 +0,0 @@
package cn.iocoder.yudao.module.system.config;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @ClassName XxlJobConfig
* @Description: xxl-job依赖配置
* @author
* @date 2022年12月07日 08:37
* @version 1.0
*/
@Configuration //是否开启xxl-job定时任务注释掉 //@Configuration 则不开启定时任务
@Data
@Slf4j
public class XxlJobConfig {
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
@Value("${xxl.job.accessToken}")
private String accessToken;
@Value("${xxl.job.executor.appname}")
private String appname;
@Value("${xxl.job.executor.address}")
private String address;
@Value("${xxl.job.executor.ip}")
private String ip;
@Value("${xxl.job.executor.port}")
private int port;
@Value("${xxl.job.executor.logpath}")
private String logPath;
@Value("${xxl.job.executor.logretentiondays}")
private int logRetentionDays;
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
XxlJobHelper.log(">>>>>>>>>>> xxl-job config init.>>>>>>>>>>>");
System.out.println("=============== xxl-job config init.===============");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
xxlJobSpringExecutor.setAppname(appname);
xxlJobSpringExecutor.setAddress(address);
xxlJobSpringExecutor.setIp(ip);
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}
}

View File

@ -10,9 +10,10 @@ import cn.iocoder.yudao.module.system.dal.dataobject.positionmap.PositionMapDO;
import cn.iocoder.yudao.module.system.dal.mysql.positionmap.PositionMapMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
@ -30,7 +31,6 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@ -44,6 +44,7 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
*/
@Service
@Validated
@Slf4j
public class PositionMapServiceImpl extends ServiceImpl<PositionMapMapper, PositionMapDO> implements PositionMapService {
@Resource
@ -258,16 +259,13 @@ public class PositionMapServiceImpl extends ServiceImpl<PositionMapMapper, Posit
@Override
@Cacheable(value = "pngBase64", key = "#floor + '_' + #area")
public String downloadPngBase64(Integer floor, String area) {
String key = "pngBase64" + "_" + floor + "_" + area;
Boolean b = stringRedisTemplate.hasKey(key);
if (b) {
return stringRedisTemplate.opsForValue().get(key);
}
// 根据楼层和区域查找对应的PNG文件路径
String basePath = UPLOAD_DIR + floor + "/" + area + "/";
File directory = new File(basePath);
if (!directory.exists() || !directory.isDirectory()) {
log.error("Directory not found for floor: {}, area: {}", floor, area);
throw exception(AGV_MAP_NOT_FOUND);
}
@ -279,6 +277,7 @@ public class PositionMapServiceImpl extends ServiceImpl<PositionMapMapper, Posit
}
if (pngFile == null || !pngFile.exists()) {
log.error("PNG file not found for floor: {}, area: {}", floor, area);
throw exception(AGV_MAP_NOT_FOUND);
}
String encodedString = null;
@ -287,13 +286,14 @@ public class PositionMapServiceImpl extends ServiceImpl<PositionMapMapper, Posit
byte[] fileContent = Files.readAllBytes(pngFile.toPath());
// 将文件内容转换为Base64字符串
encodedString = Base64.getEncoder().encodeToString(fileContent);
// 添加MIME类型前缀
encodedString = "data:image/png;base64," + encodedString;
} catch (IOException e) {
e.printStackTrace();
log.error("Failed to convert PNG to Base64 for floor: {}, area: {}", floor, area, e);
throw exception(AGV_IMAGE_CONVERSION_TO_BASE64_FAILED);
}
// -- 缓存30分钟
stringRedisTemplate.opsForValue().set(key, encodedString, 30, TimeUnit.MINUTES);
return encodedString;
}
}

View File

@ -14,7 +14,8 @@ spring:
config: # 【注册中心】配置项
namespace: dev # 命名空间。这里使用 dev 开发环境
group: DEFAULT_GROUP # 使用的 Nacos 配置分组,默认为 DEFAULT_GROUP
cache:
type: redis
--- #################### 数据库相关配置 ####################
spring:
# 数据源配置项
@ -103,16 +104,9 @@ spring:
xxl:
job:
enabled: true # 是否开启调度中心,默认为 true 开启
admin:
addresses: http://127.0.0.1:8080/xxl-job-admin
accessToken: default_token
executor:
appname: xxl-job-executor
address:
ip:
port: 9999
logpath: /data/applogs/xxl-job/jobhandler
logretentiondays: 30
addresses: http://127.0.0.1:9999/xxl-job-admin # 调度中心部署跟地址
--- #################### 服务保障相关配置 ####################