当前位置: 首页 > news >正文

使用Spring Boot开发应用:基于请求参数MD5的缓存键及其他缓存方式

本文将介绍如何在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 {@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;
}

http://www.lryc.cn/news/410786.html

相关文章:

  • typescript中interface常见3种用法
  • windows10 安装CUDA教程
  • 计算机毕业设计选题推荐-某炼油厂盲板管理系统-Java/Python项目实战
  • PSO求解函数最小值的MATLAB例程|MATLAB源代码
  • scrapy 爬取旅游景点相关数据(一)
  • 构建铁塔基站安全防护网:视频AI智能监控技术引领智慧化转型
  • Java中的分布式缓存:Ehcache与Hazelcast
  • 前端开发工程师的薪资,主要取决于哪3个方面?
  • springboot美食网站—计算机毕业设计源码11574
  • WordPress建站:如何使用ChemiCloud搭建外贸独立站
  • 在 Vim 编辑器中,如果某个单词被意外地高亮显示,使用:noh可以取消高亮显示
  • 一条命令安装mysql,php
  • 配置maven环境
  • 飞书打卡 快捷指令
  • LeYOLO,一种用于目标检测的新型可扩展且高效的CNN架构
  • docker安装phpMyAdmin
  • 举例详细学习和分析后端业务逻辑代码开发思路
  • 面试经典算法150题系列-数组/字符串操作之轮转数组
  • 苹果手机怎么录屏?一键操作,轻松掌握录屏技巧
  • [Vue3] - 3 数据响应式
  • 【话题】“八股文”在实际工作中是助力、阻力还是空谈?
  • Windows 10 安装 WSL、安装 Go 以及配置环境变量的详细教程
  • 论文阅读:基于生物神经元的模拟游戏世界感知与学习
  • 理解最先进模型的起点GPT-2 源码 配置的解释
  • C++11 可变参数模板
  • 项目实战——外挂开发(30小时精通C++和外挂实战)
  • 【人工智能专栏】Constructive损失解析
  • PHP经销商订货管理系统小程序源码
  • 【网络世界】HTTPS协议
  • 根据空域图信息构造飞机航线图以及飞行轨迹模拟matlab仿真