Compared to traditional code memoization implementation 与传统代码记忆化实现相比:
- Fast Mode 快速模式: 2-4x slower 慢2-4倍
- Safe Mode 安全模式: 5-20x slower 慢5-20倍
Note: The actual performance difference depends on the complexity of method parameters. More complex parameter types will result in larger performance gaps.
注意:实际性能差异取决于方法参数的复杂度。参数类型越复杂,性能差距越大。
A Spring-based method caching solution similar to Python's @cache decorator, providing easy-to-use method result caching capabilities with support for both normal and fast caching modes.
- Simple annotation-based caching (
@MethodCache) - Support for both safe and fast caching modes
- Automatic cache key generation based on method parameters
- Spring AOP integration
- Thread-safe implementation using
ConcurrentHashMap - Support for internal method calls (self-invocation) through Spring AOP
- Enable AOP in your Spring Boot application:
@SpringBootTest
@EnableAspectJAutoProxy
public class YourApplication {
// ...
}- Add the
@MethodCacheannotation to methods you want to cache:
@Component
public class Calculator {
@MethodCache
public int expensiveCalculation(int n) {
// Some time-consuming calculation
return result;
}
}The @MethodCache annotation supports two caching modes:
- Safe Mode (default): Uses deep comparison of method parameters to ensure cache correctness
- Fast Mode: Uses string representation of parameters for faster cache lookup
To enable fast cache mode:
@MethodCache(fastCache = true)
public int quickCalculation(int n) {
// Method implementation
}To support caching for internal method calls (self-invocation), you need to:
- Autowire the component into itself using
@Lazy:
@Component
public class Calculator {
@Autowired
@Lazy
private Calculator self;
@MethodCache
public int calculate(int n) {
// Method implementation
return self.calculate(n - 1) + self.calculate(n - 2);
}
}Based on the test cases:
- Normal Method Call: No caching, full computation every time
- @MethodCache (Safe Mode): Caches results with parameter deep comparison
- @MethodCache (Fast Mode): Fastest caching with string-based parameter comparison
The fast cache mode can provide significant performance improvements when dealing with complex parameters or frequent cache lookups.
The caching mechanism is implemented using:
- Spring AOP for method interception
- Concurrent hash maps for thread-safe caching
- Custom annotation processing
- Lazy proxy injection for self-invocation support
- The cache is stored in memory and will be cleared when the application restarts
- Fast cache mode trades some safety for performance - use it when you're sure the string representation of parameters is sufficient for cache key generation
- For internal method calls, always use the autowired reference (self) instead of this to ensure caching works properly
Here's a complete example showing different caching modes:
@Component
public class Example {
@Autowired
@Lazy
private Example self;
// Regular caching with safe parameter comparison
@MethodCache
public int normalCache(String param1, int param2) {
// Expensive operation
return result;
}
// Fast caching for better performance
@MethodCache(fastCache = true)
public int fastCache(String param1, int param2) {
// Expensive operation
return result;
}
// Internal method call with caching
@MethodCache
public int recursiveCalculation(int n) {
if (n <= 1) return n;
return self.recursiveCalculation(n - 1) + self.recursiveCalculation(n - 2);
}
}一个基于Spring的方法缓存解决方案,类似于Python的@cache装饰器,提供易用的方法结果缓存功能,支持普通和快速缓存模式。
- 基于注解的简单缓存实现(
@MethodCache) - 支持安全和快速两种缓存模式
- 基于方法参数的自动缓存键生成
- Spring AOP集成
- 使用
ConcurrentHashMap实现线程安全 - 通过Spring AOP支持内部方法调用(自调用)
- 在Spring Boot应用中启用AOP:
@SpringBootTest
@EnableAspectJAutoProxy
public class YourApplication {
// ...
}- 在需要缓存的方法上添加
@MethodCache注解:
@Component
public class Calculator {
@MethodCache
public int expensiveCalculation(int n) {
// 一些耗时的计算
return result;
}
}@MethodCache注解支持两种缓存模式:
- 安全模式(默认):使用方法参数的深度比较确保缓存正确性
- 快速模式:使用参数的字符串表示进行更快的缓存查找
启用快速缓存模式:
@MethodCache(fastCache = true)
public int quickCalculation(int n) {
// 方法实现
}要支持内部方法调用(自调用)的缓存,你需要:
- 使用
@Lazy注解将组件自动注入到自身:
@Component
public class Calculator {
@Autowired
@Lazy
private Calculator self;
@MethodCache
public int calculate(int n) {
// 方法实现
return self.calculate(n - 1) + self.calculate(n - 2);
}
}基于测试用例:
- 普通方法调用:无缓存,每次都进行完整计算
- @MethodCache(安全模式):使用参数深度比较进行缓存
- @MethodCache(快速模式):使用基于字符串的参数比较进行最快速的缓存
在处理复杂参数或频繁缓存查找时,快速缓存模式可以提供显著的性能改进。
缓存机制使用以下技术实现:
- Spring AOP用于方法拦截
- 并发哈希映射实现线程安全
- 自定义注解处理
- 延迟代理注入支持自调用
- 缓存存储在内存中,应用重启时会被清除
- 快速缓存模式以牺牲一定安全性为代价换取性能 - 仅在确定参数的字符串表示足够作为缓存键时使用
- 对于内部方法调用,始终使用自动注入的引用(self)而不是this,以确保缓存正常工作
这里是一个展示不同缓存模式的完整示例:
@Component
public class Example {
@Autowired
@Lazy
private Example self;
// 使用安全参数比较的常规缓存
@MethodCache
public int normalCache(String param1, int param2) {
// 耗时操作
return result;
}
// 使用快速缓存提升性能
@MethodCache(fastCache = true)
public int fastCache(String param1, int param2) {
// 耗时操作
return result;
}
// 带缓存的内部方法调用
@MethodCache
public int recursiveCalculation(int n) {
if (n <= 1) return n;
return self.recursiveCalculation(n - 1) + self.recursiveCalculation(n - 2);
}
}