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

浏览器网站设置在哪里seo页面代码优化

浏览器网站设置在哪里,seo页面代码优化,品牌建设的意义和重要性,推广公司怎么做基于Spring Boot的多级缓存架构实现 以下是一个基于Spring Boot的多级缓存架构实现示例 多级缓存架构实现方案 1. 依赖配置&#xff08;pom.xml&#xff09; <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-star…

基于Spring Boot的多级缓存架构实现

以下是一个基于Spring Boot的多级缓存架构实现示例


多级缓存架构实现方案

1. 依赖配置(pom.xml)
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency><groupId>com.github.ben-manes.caffeine</groupId><artifactId>caffeine</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2. 缓存配置类
@Configuration
@EnableCaching
public class MultiLevelCacheConfig {// 一级缓存(本地缓存)@Beanpublic CacheManager caffeineCacheManager() {CaffeineCacheManager manager = new CaffeineCacheManager();manager.setCaffeine(Caffeine.newBuilder().maximumSize(1000).expireAfterWrite(30, TimeUnit.SECONDS)); // 本地缓存比Redis缓存更短return manager;}// 二级缓存(Redis缓存)@Beanpublic CacheManager redisCacheManager(RedisConnectionFactory factory) {RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(5)).serializeValuesWith(SerializationPair.fromSerializer(RedisSerializer.json()));return RedisCacheManager.builder(factory).cacheDefaults(config).transactionAware().build();}// 组合缓存管理器@Bean@Primarypublic CacheManager multiLevelCacheManager(@Qualifier("caffeineCacheManager") CacheManager level1,@Qualifier("redisCacheManager") CacheManager level2) {return new MultiLevelCacheManager(level1, level2);}
}
3. 多级缓存管理器实现
public class MultiLevelCacheManager implements CacheManager {private final CacheManager level1; // 本地缓存private final CacheManager level2; // Redis缓存private final ConcurrentMap<String, Cache> cacheMap = new ConcurrentHashMap<>();public MultiLevelCacheManager(CacheManager level1, CacheManager level2) {this.level1 = level1;this.level2 = level2;}@Overridepublic Cache getCache(String name) {return cacheMap.computeIfAbsent(name, key -> new MultiLevelCache(level1.getCache(name), level2.getCache(name)));}@Overridepublic Collection<String> getCacheNames() {return Stream.concat(level1.getCacheNames().stream(),level2.getCacheNames().stream()).collect(Collectors.toSet());}// 多级缓存实现static class MultiLevelCache implements Cache {private final Cache level1;private final Cache level2;private final String name;public MultiLevelCache(Cache level1, Cache level2) {this.level1 = level1 != null ? level1 : new NoOpCache();this.level2 = level2 != null ? level2 : new NoOpCache();this.name = level1.getName() + ":" + level2.getName();}@Overridepublic String getName() {return name;}@Overridepublic Object getNativeCache() {return this;}@Overridepublic ValueWrapper get(Object key) {// 先查一级缓存ValueWrapper value = level1.get(key);if (value == null) {// 二级缓存查询value = level2.get(key);if (value != null) {// 回填一级缓存level1.put(key, value.get());}}return value;}@Overridepublic <T> T get(Object key, Class<T> type) {// 实现逻辑类似get方法T value = level1.get(key, type);if (value == null) {value = level2.get(key, type);if (value != null) {level1.put(key, value);}}return value;}@Overridepublic void put(Object key, Object value) {// 同时写入两级缓存level1.put(key, value);level2.put(key, value);}@Overridepublic void evict(Object key) {// 同时清除两级缓存level1.evict(key);level2.evict(key);}@Overridepublic void clear() {level1.clear();level2.clear();}}// 空缓存实现(容错)static class NoOpCache implements Cache {// 实现基础的Cache方法,返回空值}
}
4. 业务层使用
@Service
public class ProductService {@Cacheable(value = "products", key = "#id")public Product getProduct(Long id) {// 数据库查询逻辑return productRepository.findById(id).orElseThrow(() -> new NotFoundException("Product not found"));}@CacheEvict(value = "products", key = "#product.id")public void updateProduct(Product product) {productRepository.save(product);}
}
5. 配置说明(application.yml)
spring:cache:multi-level:level1:ttl: 30s    # 本地缓存时间max-size: 1000level2:ttl: 5m     # Redis缓存时间redis:host: redis-cluster.prodport: 6379timeout: 2000ms

关键实现要点

  1. 缓存层级策略
Client Level1 Level2 DB 查询缓存 命中返回 查询二级缓存 返回结果 返回结果 查询数据库 返回数据 回填数据 返回数据 alt [二级缓存未命中] alt [未命中] Client Level1 Level2 DB
  1. 一致性保障
  • 写操作:同时清除两级缓存(@CacheEvict​)
  • 读操作:二级缓存命中后自动回填一级缓存
  • TTL策略:一级缓存TTL(30s) < 二级缓存TTL(5m)
  1. 性能优化
  • 本地缓存:使用Caffeine高性能缓存库
  • 异步回填:可扩展为异步加载(需自定义CacheLoader)
  • 批量操作:支持@Cacheable的批量查询优化

扩展建议

  1. 缓存预热
@PostConstruct
public void preloadHotData() {// 加载热点数据到缓存hotProducts.forEach(product -> cacheManager.getCache("products").put(product.getId(), product));
}
  1. 监控集成
@Bean
public MeterRegistryCustomizer<MeterRegistry> cacheMetrics() {return registry -> {CaffeineCacheManager caffeine = context.getBean(CaffeineCacheManager.class);RedisCacheManager redis = context.getBean(RedisCacheManager.class);// 注册Caffeine监控CacheMetrics.monitor(registry, caffeine, "level1");// 注册Redis监控CacheMetrics.monitor(registry, redis, "level2");};
}
  1. 防雪崩策略
// 在MultiLevelCache.get方法中添加
ValueWrapper get(Object key) {try {// 添加分布式锁检查if (lockManager.tryLock(key)) {// 实际查询逻辑}} finally {lockManager.unlock(key);}
}

http://www.dtcms.com/wzjs/171036.html

相关文章:

  • 中信建设公司领导班子口碑seo推广公司
  • 织梦建设网站需要什么软件女教师遭网课入侵直播录屏曝
  • 网站建设 合优网络谷歌浏览器怎么下载
  • 汕头seo推广优化重庆seo霸屏
  • 怎么做个人网站的企业宣传软文
  • 网站后台功能怎么找百度客服
  • 做网站签了合同后不想做了今天上海最新新闻事件
  • 东莞市电池网站建设软文广告发稿
  • 广告联盟有哪些鹤壁seo公司
  • 陇南市建设局网站网站优化推广
  • 湖南电子科技网站建设新东方在线教育平台官网
  • 网站开发前台徐州网站建设方案优化
  • ps怎么做网站首页界面网站查询站长工具
  • 做网站用的代码百度推广怎么收费
  • 在线自助网站按照程序网站下载免费软件
  • 公司企业建设网站郑州网站seo
  • 建设工程施工安全网站天津seo推广服务
  • 网站建设项目报价seo权威入门教程
  • 武汉工程公司有哪些网站关键词优化软件效果
  • 做兼职的网站贴吧济南seo网站关键词排名
  • 淄博企业高端网站建设seo是什么工作内容
  • dw怎么做网站教程合肥优化营商环境
  • 商务网站大全百度图片识别在线使用
  • 用什么工具建设网站关键词出价计算公式
  • 网站建设的认识安徽网站seo公司
  • 郑州的电子商城网站建设seminar
  • 如何用国外网站做头条网络推广平台软件app
  • 驻马店阿里巴巴做网站seo网站建设优化
  • 绍兴做网站建设邯郸今日头条最新消息
  • 网站整体地图怎么做在线识别图片找原图