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

汉中北京网站建设中国小康建设网是骗子网站吗?

汉中北京网站建设,中国小康建设网是骗子网站吗?,wordpress 当前主题,做网站需要Spring Boot 缓存最佳实践#xff1a;从基础到生产的完整指南 引言 在现代分布式系统中#xff0c;缓存是提升系统性能的银弹。Spring Boot 通过 spring-boot-starter-cache​ 模块提供了开箱即用的缓存抽象#xff0c;但如何根据业务需求实现灵活、可靠的缓存方案#xf… Spring Boot 缓存最佳实践从基础到生产的完整指南 引言 在现代分布式系统中缓存是提升系统性能的银弹。Spring Boot 通过 spring-boot-starter-cache​ 模块提供了开箱即用的缓存抽象但如何根据业务需求实现灵活、可靠的缓存方案本文将带您从零开始逐步构建符合生产要求的缓存系统。 一、基础篇5分钟快速接入 1.1 最小化配置 pom.xml 依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-cache/artifactId /dependencyNacos 配置application.yml spring:cache:type: simple # 默认内存缓存启动类注解 SpringBootApplication EnableCaching public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);} }业务层使用 Service public class ProductService {Cacheable(products)public Product getProduct(Long id) {// 数据库查询逻辑} }二、进阶篇多缓存引擎支持 2.1 缓存类型切换 配置选项对比 类型依赖适用场景特点​simple​内置开发测试环境无过期策略​caffeine​com.github.ben-manes.caffeine高性能本地缓存支持多种过期策略​redis​spring-boot-starter-data-redis分布式生产环境支持持久化、集群 Nacos 配置示例 spring:cache:type: redis # 切换缓存引擎# Redis 连接配置redis:host: redis.prod.clusterport: 6379password: ${REDIS_PASSWORD}三、生产级特性实现 3.1 方法级 TTL 控制 实现方式1语法约定 语法约定 Cacheable(热点数据#600) // 600秒过期 public HotData getHotData(String key) {// 业务逻辑 }TTL 解析实现 public class CacheConfig {Beanpublic CacheManagerCustomizerRedisCacheManager redisCacheCustomizer() {return manager - manager.setCacheDecorator((name, config) - {String[] parts name.split(#);if (parts.length 1) {Duration ttl Duration.ofSeconds(Long.parseLong(parts[1]));return new RedisCacheWrapper(parts[0], config.entryTtl(ttl));}return new RedisCacheWrapper(name, config);});} }实现方式2自定义注解AOP切面 定义自定义注解 Target(ElementType.METHOD) Retention(RetentionPolicy.RUNTIME) public interface CacheCustomTtl {long value(); // 缓存时间秒long jitter() default 10; // 抖动范围秒//....自定义其他逻辑 }aop切面逻辑 Aspect Component public class CacheTtlAspect {Around(annotation(cacheCustomTtl))public Object applyCustomTtl(ProceedingJoinPoint joinPoint, CacheCustomTtl cacheCustomTtl) throws Throwable {// 获取原始缓存配置Method method ((MethodSignature) joinPoint.getSignature()).getMethod();Cacheable cacheable method.getAnnotation(Cacheable.class);String[] cacheNames cacheable.value();// 生成带自定义时间的缓存名称例如: user#3600String newCacheName cacheNames[0] # cacheCustomTtl.value();String[] modifiedCacheNames {newCacheName};// 动态修改缓存名称Cacheable modifiedCacheable new CacheableWrapper(cacheable, modifiedCacheNames);((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(Cacheable.class).value();// 通过反射调用原方法需使用动态代理或工具类return joinPoint.proceed();}// 包装类用于动态修改注解属性private static class CacheableWrapper implements Cacheable {private final Cacheable delegate;private final String[] cacheNames;public CacheableWrapper(Cacheable delegate, String[] cacheNames) {this.delegate delegate;this.cacheNames cacheNames;}Overridepublic String[] value() { return cacheNames; }// 其他方法委托给原注解...} }3.2 随机抖动Jitter 防雪崩配置 spring:cache:jitter-range: 60s # 最大抖动时间范围抖动值生成逻辑 private Duration applyJitter(Duration ttl) {long jitter ThreadLocalRandom.current().nextLong(spring.cache.jitter-range.getSeconds() 1);return ttl.plusSeconds(jitter); }四、高级优化方案 4.1 多级缓存架构 #mermaid-svg-f749E03keHZeTstg {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-f749E03keHZeTstg .error-icon{fill:#552222;}#mermaid-svg-f749E03keHZeTstg .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-f749E03keHZeTstg .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-f749E03keHZeTstg .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-f749E03keHZeTstg .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-f749E03keHZeTstg .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-f749E03keHZeTstg .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-f749E03keHZeTstg .marker{fill:#333333;stroke:#333333;}#mermaid-svg-f749E03keHZeTstg .marker.cross{stroke:#333333;}#mermaid-svg-f749E03keHZeTstg svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-f749E03keHZeTstg .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-f749E03keHZeTstg .cluster-label text{fill:#333;}#mermaid-svg-f749E03keHZeTstg .cluster-label span{color:#333;}#mermaid-svg-f749E03keHZeTstg .label text,#mermaid-svg-f749E03keHZeTstg span{fill:#333;color:#333;}#mermaid-svg-f749E03keHZeTstg .node rect,#mermaid-svg-f749E03keHZeTstg .node circle,#mermaid-svg-f749E03keHZeTstg .node ellipse,#mermaid-svg-f749E03keHZeTstg .node polygon,#mermaid-svg-f749E03keHZeTstg .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-f749E03keHZeTstg .node .label{text-align:center;}#mermaid-svg-f749E03keHZeTstg .node.clickable{cursor:pointer;}#mermaid-svg-f749E03keHZeTstg .arrowheadPath{fill:#333333;}#mermaid-svg-f749E03keHZeTstg .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-f749E03keHZeTstg .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-f749E03keHZeTstg .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-f749E03keHZeTstg .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-f749E03keHZeTstg .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-f749E03keHZeTstg .cluster text{fill:#333;}#mermaid-svg-f749E03keHZeTstg .cluster span{color:#333;}#mermaid-svg-f749E03keHZeTstg div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-f749E03keHZeTstg :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 命中 未命中 未命中 业务层 本地缓存 Redis集群 数据库 实现要点 使用 Caffeine 作为一级缓存Redis 作为二级缓存自定义 CacheManager 实现分级策略 基于Spring Boot的多级缓存架构实现 4.2 监控与治理 Spring Boot Actuator 集成 management:endpoints:web:exposure:include: caches,health,metrics关键监控指标 ​cache.gets​缓存查询次数​cache.puts​缓存写入次数​cache.removals​缓存清除次数​cache.evictions​缓存淘汰次数 五、最佳实践总结 5.1 配置推荐 # 生产环境推荐配置 spring:cache:type: redisjitter-range: 30skey-separator: ::redis:lettuce:pool:max-active: 20max-idle: 10min-idle: 55.2 避坑指南 键设计原则 使用业务语义明确的键命名如 user:profile:{userId}​避免使用可变对象作为键 缓存穿透防护 Cacheable(value users, unless #result null) public User getUser(Long id) {// 返回null时自动跳过缓存 }版本兼容策略 CachePut(value products#3600, key #product.id) public Product updateProduct(Product product) {// 更新后自动刷新缓存 }最后 根据业务场景灵活选择适合的缓存策略从简单的内存缓存到复杂的分布式缓存体系Spring Boot 的缓存抽象层始终提供一致的使用体验。记住没有完美的缓存方案只有最适合业务场景的缓存策略。 ‍
http://www.hkea.cn/news/14321541/

相关文章:

  • 深圳网站建设总部大连大久建设集团有限公司
  • 俄罗斯免费网站推广网站外链建设记住5种外链方式不可用
  • 个人做网站如何赚钱建设学校网站的意义
  • 南京网站建设培训如何做一个免费的网站
  • 网站上怎么做企业推广wordpress网易云插件
  • 福建省建设局实名制网站东莞市公共资源交易中心
  • 厦门企业官方网站建设网站 js 广告代码大全
  • php网站如何编辑高端网站建设联系方式
  • 网站设计的主要步骤怎么开公众号
  • 响应式网站模板企业wordpress cms社交
  • 家具设计师培训班长沙网络优化推广公司
  • 做研究的网站怎么下载有风险的软件
  • 企业网站建设计划书html什么意思
  • 营销网站建设的目的手机版网站开发用什么语言
  • 企业网站建设立项请示wordpress登录工具
  • 网站怎么被搜到首页室内设计公司免费网站
  • 国外移动网站设计往网站上传照片怎么做
  • 用电脑做服务器的建一个网站淘宝的网络营销模式
  • 做dnf辅助网站什么网站可以做机票行程单
  • 郑州 做网站青海服装网站建设公司
  • 做照片书的网站网站美化教程下载
  • 高清的宝安网站推广wordpress制作网站步骤
  • 网站开发计划甘特图四川平台网站建设哪里有
  • 凡科电脑版登录首页关键词排名优化公司哪家好
  • 网站什么时候恢复彩色襄阳seo研究中心
  • 手机触屏网站模板360街景地图最新版
  • wordpress推送到公众号seo的培训网站哪里好
  • 阿里云网站方案建设书模板大庆做网站比较好的公司
  • 凡科手机网站设置问题模板网站视频
  • 简述商务网站建设的步骤代驾平台