🍰 🌯 Caffeine 是基于 JAVA 8 的高性能缓存库。参考 Google Guava 的API对缓存框架重写,基于LRU算法实现,支持多种缓存过期策略。 Spring Boot 1.x版本中的默认本地缓存是Guava Cache。在 Spring5 (spring boot 2.x) 后,Spring 官方放弃了 Guava Cache 作为缓存机制,而是使用性能更优秀的 Caffeine 作为默认缓存组件
xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.7.0</version>
</dependency>
在SpingBoot启动类标上@EnableCaching注解
application.yml 配置参数
yml
spring:
# 配置缓存,初始缓存容量为10,最大容量为200,过期时间(这里配置写入后过期时间为3秒)
cache:
type: caffeine
caffeine:
spec: initialCapacity=10,maximumSize=200,expireAfterWrite=3s
这个问题是由于Nginx上传默认限制大小为1mb,如果包含过多的图片就会超出,需要手动修改nginx的配置
在linux服务上找到nginx的安装位置,在安装目录下面找到conf文件,下载到本地打开编辑,找到http{}位置,修改最大body大小
http { include /etc/nginx/mime.types; default_type application/octet-stream; client_max_body_size 20M; #此处 }
修改完成后执行重启nginx命令
./nginx -s reload
项目中经常会遇到各种需要以树形结构展示的功能,如菜单树、分类树、部门树,Hutool的TreeUtil主要是用来快速构造树形结构,以及获取所有叶子节点等操作。🍉
xml
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.22</version>
</dependency>
java
/**
* 获取构造树
**/
@ApiOperation(value = "获取构造树", notes = "获取构造树")
@GetMapping(value = "/getConstructTree")
public Result<?> getConstructTree() {
List<Tree<String>> treeList = sdSchoolClassTypeDataCategoryService.constructTree();
return Result.OK(treeList);
}
HttpSevletRequest 的getRemoteAddr 方法不能获取到客户端的ip地址,显示的是127.0.0.1,这是代理服务器的部署地址
因为经过代理以后,由于在客户端和服务之间增加了中间层,因此服务器无法直接拿到客户端的IP,服务器端应用也无法直接通过转发请求的地址返回给客户端,但是在转发请求的HTTP头信息中,增加了X-FORWARDED-FOR信息。用以跟踪原有的客户端IP地址和原来客户端请求的服务器地址。
java
String requestIp = "";
String xff = request.getHeader("x-forwarded-for");
if (xff != null) {
int index = xff.indexOf(',');
if (index != -1) {
xff = xff.substring(0, index);
}
requestIp = xff.trim();
} else {
requestIp = request.getRemoteAddr();
}
🎯先上结论,Mapstruct的性能远远高于BeanUtils,这应该是大佬使用Mapstruct的主要原因,下面是我的测试结果,可以看出随着属性个数的增加,BeanUtils的耗时也在增加,并且BeanUtils的耗时跟属性个数成正比,而Mapstruct的耗时却一直是1秒,所以从对比数据可以看出Mapstruct是非常优秀的,其性能远远超过BeanUtils。
xml
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.5.0.Final</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.0.Final</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>