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

配件查询网站制作网站公司好做吗

配件查询网站制作,网站公司好做吗,国外网站 网站 推荐,wordpress 账号一、数据不一致的典型场景 写入顺序不一致 当业务逻辑需要同时更新数据库和缓存时,若出现"先删缓存后更新DB"或"先更新DB后删缓存"操作失败,会导致缓存与数据库数据版本不一致。 并发读写冲突 高并发场景下可能出现: …

一、数据不一致的典型场景

  1. 写入顺序不一致
    当业务逻辑需要同时更新数据库和缓存时,若出现"先删缓存后更新DB"或"先更新DB后删缓存"操作失败,会导致缓存与数据库数据版本不一致。

  2. 并发读写冲突
    高并发场景下可能出现:

  • 线程A更新数据库
  • 线程B读取旧缓存
  • 线程A删除/更新缓存 此时缓存中残留旧数据
  1. 异步同步延迟
    基于消息队列或binlog解析的异步同步方案,在网络波动或系统负载时可能出现同步延迟

  2. 缓存穿透/雪崩
    恶意请求或突发流量导致:

  • 缓存穿透:大量请求直接访问数据库
  • 缓存雪崩:大量缓存同时过期
// 典型双写示例(问题代码)
public void updateProduct(Product product) {// 先更新数据库productDao.update(product); // 再更新缓存redisTemplate.opsForValue().set(product.getId(), product);
}

二、主流解决方案与Java实现

1. 延迟双删策略

public void updateProductWithDelay(Product product) {// 第一次删除缓存redisTemplate.delete(product.getId());  // 更新数据库productDao.update(product);  // 延迟二次删除(使用异步线程)CompletableFuture.runAsync(() -> {try {Thread.sleep(1000); // 根据业务设置合理延迟redisTemplate.delete(product.getId());} catch (InterruptedException e) {Thread.currentThread().interrupt();}});
}

2. 基于Binlog的同步(Canal实现)

架构流程
MySQL -> Canal Server -> Kafka -> 数据消费服务 -> Redis

// Canal客户端示例
@KafkaListener(topics = "canal_topic")
public void handleMessage(String message) {CanalMessage canalMsg = JSON.parseObject(message, CanalMessage.class);if ("UPDATE".equals(canalMsg.getType())) {canalMsg.getData().forEach(item -> {String key = "product:" + item.get("id");redisTemplate.opsForValue().set(key, item);});}
}

3. 分布式锁保障一致性

public Product getProduct(String id) {String cacheKey = "product:" + id;Product product = redisTemplate.opsForValue().get(cacheKey);if (product == null) {RLock lock = redissonClient.getLock("lock:" + cacheKey);try {lock.lock();// 双重检查锁product = redisTemplate.opsForValue().get(cacheKey);if (product == null) {product = productDao.findById(id);redisTemplate.opsForValue().set(cacheKey, product, 30, TimeUnit.MINUTES);}} finally {lock.unlock();}}return product;
}

三、优化实践方案

1. 异步批处理优化

// 使用Guava的批量收集器
@Bean
public BatchProcessor<DataChangeEvent> batchProcessor() {return BatchProcessor.create(events -> {List<RedisCommand> commands = events.stream().map(e -> new RedisCommand("SET", e.getKey(), e.getValue())).collect(Collectors.toList());redisTemplate.executePipelined(commands);},500, // 批量大小100, // 缓冲时间(ms)4    // 并发线程数);
}

2. 熔断降级策略

// 使用Resilience4j实现
CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("redis");
RateLimiter rateLimiter = RateLimiter.of(100, Duration.ofSeconds(1));public Product getProductSafe(String id) {return Decorators.ofSupplier(() -> getProduct(id)).withCircuitBreaker(circuitBreaker).withRateLimiter(rateLimiter).withFallback(Exception.class, e -> productDao.findById(id)).get();
}

3. 数据版本控制

// 添加版本号字段
@Data
public class Product {private Long id;private String name;private Long version; // 数据版本
}// 更新时校验版本
public boolean updateWithVersion(Product product) {String luaScript = "if redis.call('get', KEYS[1]) == ARGV[1] then " +"redis.call('set', KEYS[1], ARGV[2]) return 1 else return 0 end";Long result = redisTemplate.execute(new DefaultRedisScript<>(luaScript, Long.class),Collections.singletonList("product:" + product.getId()),String.valueOf(product.getVersion() - 1),product.toString());return result == 1;
}

四、监控指标体系建设

  1. 关键监控指标:

    • 同步延迟时间(Redis_Last_Update - DB_Update_Time)
    • 缓存命中率(keyspace_hits / (keyspace_hits + keyspace_misses))
    • 同步失败率(failed_sync_count / total_sync_count)
  2. 日志追踪方案:

// 使用MDC实现请求链路追踪
public Product getProduct(String id) {MDC.put("traceId", UUID.randomUUID().toString());try {// 业务逻辑} finally {MDC.clear();}
}

五、总结与展望

通过组合使用延迟双删、binlog同步、分布式锁等方案,可构建不同一致性级别的同步系统。建议根据业务场景选择:

  • 强一致性场景:分布式锁 + 同步双写
  • 最终一致性场景:Canal + 消息队列
  • 高性能场景:多级缓存 + 异步批处理

文章转载自:

http://moujmPnW.bnhyd.cn
http://CteIa770.bnhyd.cn
http://yRD4eDOm.bnhyd.cn
http://d6aCWasp.bnhyd.cn
http://7wZJzNO9.bnhyd.cn
http://wHzmxsAY.bnhyd.cn
http://SSf0hryQ.bnhyd.cn
http://pqMXdLtO.bnhyd.cn
http://u6Cz9Y4E.bnhyd.cn
http://0A5NDu7H.bnhyd.cn
http://VTuFVrMQ.bnhyd.cn
http://qYm9s2LA.bnhyd.cn
http://U0A0vP0I.bnhyd.cn
http://MxzstPVd.bnhyd.cn
http://JGjZXMfa.bnhyd.cn
http://EFfgjPOE.bnhyd.cn
http://sDwidbxA.bnhyd.cn
http://ZKVV8Zy4.bnhyd.cn
http://w1VYcvcm.bnhyd.cn
http://fym8EWVS.bnhyd.cn
http://HAcL6nbo.bnhyd.cn
http://drUq1SEa.bnhyd.cn
http://AZDFgzQ2.bnhyd.cn
http://Dwfzmz7y.bnhyd.cn
http://mrcvjtXo.bnhyd.cn
http://6gfT8QsL.bnhyd.cn
http://ZBslaVCU.bnhyd.cn
http://3D78WPQV.bnhyd.cn
http://FyeBBgLk.bnhyd.cn
http://d6lFZYIJ.bnhyd.cn
http://www.dtcms.com/wzjs/690295.html

相关文章:

  • 济南好的网站建设公司排名网站广告网络推广价格低
  • 年前做招聘网站话术淘宝流量
  • 铁岭新区旅行社电话重庆seo网络推广关键词
  • 制作手机网站工具网站改版分析
  • 动漫网站首页设计上饶做网站最好的公司
  • 网站建设劳务合同嘉峪关市建设局公示公告网站
  • 建设网站宝安区外链网站大全
  • 建筑工程 技术支持 东莞网站建设wordpress进阶教程
  • 做网站的要多钱vps架设好网站访问不了
  • 网站大屏轮播图效果怎么做网站建设 核算
  • 网站建设的难点和问题wordpress导出导入
  • 河南省网站制作公司知名网站制作公
  • 大连网站平台研发护肤网站模版
  • 万网的成品网站常用的网络营销方式有
  • 有没有做海报的网站推荐济南又出现5例
  • 网站开发的大致流程wordpress 主题 图片
  • 网站设计与建设系统会计信息系统网站建设流程图
  • 成都网站建设公司盈利吗韩国外贸网站
  • 便宜的网站空间替换wordpress管理路径
  • 有阿里云的主机了怎么做网站wordpress menu_walker
  • 苏州网站开发公司有哪些做淘客要有好的网站
  • 做西餐的网站网站运营名词解释
  • 网站开发实验报告三做网站开发公司电话
  • 企业网站建设市场报价合同管理软件
  • 嘉兴网站推广优化费用wordpress4.7.4+for+sae
  • 广告策划书安阳网站制作优化
  • 精品成品源码网站下载ps软件免费版
  • 吉林省建设工程造价网站WordPress 视频cdn
  • 如何做好一个企业网站童美童程儿童编程价格
  • 所得税 网站建设费公司网站asp源码