本文将介绍如何在Spring Boot应用中实现基于请求参数MD5的缓存键,以及其他常见的缓存方式。通过实际代码示例,展示如何应用这些技术优化系统性能。
1. 引入必要的依赖
首先,在Spring Boot项目中引入缓存相关的依赖。修改pom.xml文件,添加如下依赖:
<dependencies>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Starter Cache -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- Optional: Spring Boot Starter Data Redis for Redis Cache -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
2. 配置缓存
在application.properties中进行缓存配置。这里以内存缓存(如ConcurrentMapCacheManager)为例:
spring.cache.type=simple
如果使用Redis作为缓存,可以进行如下配置:
spring.cache.type=redis
spring.redis.host=localhost
spring.redis.port=6379
3. 启用缓存支持
在Spring Boot应用的主类或配置类上添加@EnableCaching注解:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class CacheApplication {
public static void main(String[] args) {
SpringApplication.run(CacheApplication.class, args);
}
}
4. 计算请求参数的MD5作为缓存键
创建一个自定义缓存键生成器,计算请求参数的MD5值并作为缓存键:
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
@Component("customKeyGenerator")
public class CustomKeyGenerator implements KeyGenerator {
@Override
public Object generate(Object target, Method method, Object... params) {
try {
// 将请求参数按一定顺序组合成字符串
String paramStr = Arrays.stream(params)
.map(Object::toString)
.reduce((a, b) -> a + "&" + b)
.orElse("");
// 计算MD5值
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] hash = md.digest(paramStr.getBytes(StandardCharsets.UTF_8));
// 转换为32位的哈希值
StringBuilder hexString = new StringBuilder();
for (byte b : hash) {
hexString.append(String.format("%02x", b));
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
}
5. 应用缓存注解
在服务层应用缓存注解,使用自定义的缓存键生成器:
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class MyService {
@Cacheable(value = "myCache", keyGenerator = "customKeyGenerator")
public String getData(String param1, String param2) {
// 模拟耗时操作
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Data from " + param1 + " and " + param2;
}
}
6. 其他缓存方式
除了使用请求参数的MD5作为缓存键外,还可以采用其他缓存方式,如直接使用请求参数、组合多个参数等。示例如下:
6.1 直接使用请求参数作为缓存键
@Cacheable(value = "myCache", key = "#param1 + '-' + #param2")
public String getDataUsingParams(String param1, String param2) {
// 模拟耗时操作
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Data from " + param1 + " and " + param2;
}
6.2 组合多个参数作为缓存键
@Cacheable(value = "myCache", key = "T(String).valueOf(#param1).concat('-').concat(T(String).valueOf(#param2))")
public String getDataUsingCombinedParams(String param1, String param2) {
// 模拟耗时操作
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Data from " + param1 + " and " + param2;
}





![[前端]解决Iframe即使设置高度100%,但还是显示滚动条scrollbar的问题](https://i-blog.csdnimg.cn/direct/b21b3972d6be491eb4633423a5217f45.png)













