为什么 google 使用 AspectJ 缓存很慢而 SpringCaching 更快

Why google cache with AspectJ is slow while SpringCaching is faster

我正在阅读有关缓存机制的内容,并使用 SpringCaching with AspectJ 创建了一个 EhCache 示例项目,并使用 SpringCaching with AspectJ 对 google 缓存进行了基准测试。我发现的东西让我有点惊讶。

注意:- Spring 缓存 我的意思是,方法使用@Cacheable/@Caching 注释进行注释。

SpringAspectJ 的缓存速度更快,但另一方面 googleAspectJ 的缓存执行速度较慢。以下是他们处理请求所用时间的平均值:-

GoogleCacheWithoutAspect 
1.262323232 ms

GoogleCacheWithAspect
5.205010101 ms

SpringCachingWithoutAspect
3.08548 ms

SpringCachingWithAspect
2.77782 ms

因为我是新手,所以我也想确认一下,为什么要将 AspectJ 与 Spring 缓存一起使用,AspectJ 如何使缓存更快。

我也遵循下面的post,但仍然不确定,AspectJ 是否真的提高了缓存性能。 http://architects.dzone.com/articles/cacheable-overhead-spring-0

still not sure, does AspectJ really improves the caching performance

article 中的数字清楚地表明,与 Spring AOP 相比,AspectJ 在运行时开销方面要优越得多(并且在视觉水平上甚至优于手动缓存)。这是因为:

  • Spring 使用动态代理(即子classes 或在运行时动态创建的接口实现)并且总是引入调用链,因为每个调用都通过代理路由以实现 AOP 拦截行为。 Java 动态代理(用于接口)和 CGLIB 代理(用于 classes)也是如此。
  • AspectJ 不需要任何代理,因为它通过字节码检测将优化代码直接编织到原始 classes 中。这是在编译时完成的,或者可选地在 class 加载期间每次 JVM 启动时完成一次。这样既快速又高效。