wap网站前台,seo推广seo技术培训,网站由哪些部分组成部分组成部分,dw内部网站链接怎么做本文将介绍如何在Spring Boot应用中实现基于请求参数MD5的缓存键#xff0c;以及其他常见的缓存方式。通过实际代码示例#xff0c;展示如何应用这些技术优化系统性能。
1. 引入必要的依赖
首先#xff0c;在Spring Boot项目中引入缓存相关的依赖。修改pom.xml文件#x…本文将介绍如何在Spring Boot应用中实现基于请求参数MD5的缓存键以及其他常见的缓存方式。通过实际代码示例展示如何应用这些技术优化系统性能。
1. 引入必要的依赖
首先在Spring Boot项目中引入缓存相关的依赖。修改pom.xml文件添加如下依赖
dependencies!-- Spring Boot Starter Web --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!-- Spring Boot Starter Cache --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-cache/artifactId/dependency!-- Optional: Spring Boot Starter Data Redis for Redis Cache --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependency
/dependencies2. 配置缓存
在application.properties中进行缓存配置。这里以内存缓存如ConcurrentMapCacheManager为例
spring.cache.typesimple如果使用Redis作为缓存可以进行如下配置
spring.cache.typeredis
spring.redis.hostlocalhost
spring.redis.port63793. 启用缓存支持
在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 {Overridepublic 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;
}