首页 > 其他分享 >spring中cache注解中加上表达式会变慢!

spring中cache注解中加上表达式会变慢!

时间:2022-12-11 11:01:33浏览次数:60  
标签:String 变慢 spring cache long result avgt ns


在老外的两篇博文中,​​https://plumbr.io/blog/performance-blog/more-about-spring-cache-performance​​​ ,​​https://plumbr.io/blog/performance-blog/spring-cache-profiling​

分别讲解了,在@cache注解中,使用参数速度变慢(通过JMH)基准测试工具测试。

不使用参数:

@Cacheable("time")
public long annotationBased(String dummy) {
return System.currentTimeMillis();
}

public long manual(String dummy) {
Cache.ValueWrapper valueWrapper = cache.get(dummy);
long result;
if (valueWrapper == null) {
result = System.currentTimeMillis();
cache.put(dummy, result);
} else {
result = (long) valueWrapper.get();
}
return result;
}
JMH结果:


Benchmark Mode Cnt Score Error Units

CacheBenchmark.annotationBased avgt 5 245.960 ± 27.749 ns/op

CacheBenchmark.manual avgt 5 16.696 ± 0.496 ns/op

CacheBenchmark.nocache avgt 5 44.586 ± 9.091 ns/op
可以看到,avgt是平均执行每次操作的耗费时间长短,手工的快;


再看注解中有参数的:


@Cacheable(value = "time", key = "#p0.concat(#p1)")
public long annotationWithSpel(String dummy1, String dummy2) {
return System.currentTimeMillis();
}
@Cacheable(value = "time")
public long annotationBased(String dummy1, String dummy2) {
return System.currentTimeMillis();
}

JMH测试结果:

Benchmark                       Mode  Cnt     Score    Error  Units
CacheBenchmark.annotationBased avgt 5 271.975 ± 11.586 ns/op
CacheBenchmark.spel avgt 5 1196.744 ± 93.765 ns/op
CacheBenchmark.manual avgt 5 16.325 ± 0.856 ns/op
CacheBenchmark.nocache avgt 5 40.142 ± 4.012 ns/op

标签:String,变慢,spring,cache,long,result,avgt,ns
From: https://blog.51cto.com/u_14230175/5928136

相关文章

  • spring boot 2.x 不占用端口方式启动
    当我们通过springboot仅运行一些定时任务时,就可以不用占用web启动端口,这时候就需要springboot不占用web端口的方式;springboot2.x之后(代码方式)@SpringBootApplicati......
  • 基于Java+Springboot+Vue+elememt甜品屋蛋糕商城系统设计和实现
    目录​​一、开发背景和技术:​​​​1.1开发背景  ​​​​1.2B/S体系结构​​​​1.3Java语言简介​​​​1.4SpringBoot框架​​​​1.5MySQL简介​​​​二、系统......
  • springboot 整合使用redis发布订阅功能
    在Java中有3种流程控制结构:顺序结构、选择结构、循环结构。顺序结构顺序结构是指程序从上向下依次执行每条语句的结构,中间没有任何的判断和跳转。选择结构选择结构是根据条......
  • Spring之基础知识
    1、ApplicationContextVSBeanFactoryl 二者来自的jar包不同;BeanFactory来自spring.beans.jar;ApplicationnContext来自spring.context.jar下。l BeanFactory......
  • 解决SpringCloudConfig中文乱码问题
    问题来自于配置服务端使用的编码格式为ISO-8859-1导致  这里处理后的结果添加两个类CustomizedOriginTrackedPropertiesLoader和CustomizedPropertiesPropertySource......
  • SpringSecurity 学习记录
    SpringSecurity简介SpringSecurity,这是一种基于SpringAOP和Servlet过滤器的安全框架。它提供全面的安全性解决方案,同时在Web请求级和方法调用级处理身份确认和授......
  • 【02期】你能说说Spring框架中Bean的生命周期吗?
    首先简单说一下(以下为一个回答的参考模板)1、实例化一个Bean--也就是我们常说的new;2、按照Spring上下文对实例化的Bean进行配置--也就是IOC注入;3、如果这个Bean已经实现了Bea......
  • spring mvc环境之Aop切面的xml配置和注解(AspectJ)(十)
     Spring的AOP引入步骤:1.引入jar,pom.xml加入spring-aop坐标(一般引入数据库事务的时候已经引用了).2.创建目标接口和目标对象(bean类,service或dao层).3.创建切面类(类......
  • Spring,SpringMVC,SpringBoot,SpringCloud有什么区别和联系?
    简单介绍Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。Spring使你能够编写更干净、更可管理、并且更易于测试的代码。SpringMVC是Spring的一个模块,一......
  • 【spring框架】@componentscan, @import, @configuration区别
    @configuration:这个注解用来代替spring容器的xml配置文件。具体就是配置文件中的标签。被@configuration标注的类,他里边所有的被@bean标注的方法都会被执行,这些方法返回的......