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

网站系统有哪些全国建筑行业资质查询平台官网

网站系统有哪些,全国建筑行业资质查询平台官网,iframe 一直网站底部,wordpress中文正式版本文将介绍如何在Spring Boot应用中实现基于请求参数MD5的缓存键,以及其他常见的缓存方式。通过实际代码示例,展示如何应用这些技术优化系统性能。 1. 引入必要的依赖 首先,在Spring Boot项目中引入缓存相关的依赖。修改pom.xml文件&#x…

本文将介绍如何在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.yayakq.cn/news/813713/

相关文章:

  • 哈尔滨建设厅官方网站洛阳做公司网站
  • 贵州新站优化工程模板多少钱一张
  • 网站开发最新流程梁山做网站
  • 个人网站找谁建设好百度一下全知道
  • 微信网站开发报价表谷歌自建站和优化
  • 网站木马诊断导购个人网站怎么做的
  • 陕西省建设执业资格注册中心网站建设银行网站怎么不可登入
  • 做网站没资源wordpress网站go.php跳转
  • 学做凉菜冷菜的网站微信小程序怎么关闭防沉迷
  • 网站建设简单模板搭建了什么样的平台
  • 企业网站优化官网做企业网站的多少钱
  • 做网站域名怎么选有利于seo怎么看公司是不是外包
  • 网站出租目录做菠菜 有什么坏处网站和系统的哪个容易做
  • 搭建发卡网站要多少钱新乡网站开发的公司
  • 浙江常升建设有限公司网站德国室内设计联盟
  • 平凉城乡建设局网站seo深度优化服务
  • 成都网站制作工业外观设计
  • 南宁网站建设nnxun公司一定建设网站吗
  • 网站建设之网页制作语言基础阿里云 建网站
  • 网站建设费用报价单想做个网站都需要什么
  • 迅睿cms建站公司注册核名
  • 找网站公司做网站的陷阱万网有跟企业做网站吗
  • 做app的网站有哪些功能吗php网站开发工具
  • zencart网站管理 1.5线上平台建设计划书怎么写
  • 制作公司网站的规划上海嘉定区网站建设公司
  • wordpress 搜索乱码青岛网站关键字优化
  • 网站制作公司怎么运营做网站sqlserver排序
  • 江门网站建设易搜互联佛山网站建设服务器
  • 怎么做网站注册推广300m空间够用吗 wordpress
  • 建设银行官方网站购房贷款利率如何让网站快速收录