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

实战Redis与MySQL双写一致性的缓存模式

​Redis和MySQL都是常用的数据存储系统,它们各自有自己的优缺点。在实际应用中,我们可能需要将它们结合起来使用,比如将Redis作为缓存,MySQL作为持久化存储。

在这种情况下,我们需要保证Redis和MySQL的数据一致性,也就是当数据在Redis中进行修改时,也要相应地在MySQL中进行修改,反之亦然。


Cache-Aside Pattern

Cache-Aside Pattern(也称为 Lazy-Loading 缓存模式)是一种常见的缓存设计模式,用于在应用程序中手动管理缓存数据。在这种模式下,应用程序负责在需要的时候将数据加载到缓存中,以便提高数据的访问速度和性能。

Cache-Aside 模式的工作流程如下:

1. 读取数据

当应用程序需要读取数据时,它首先会检查缓存中是否存在所需数据。如果数据存在于缓存中,应用程序直接从缓存中获取数据。如果数据不在缓存中,应用程序会从主数据源(如数据库)中获取数据,并将数据加载到缓存中。

2. 写入数据

当应用程序执行写操作(如创建、更新、删除)时,它首先会更新主数据源中的数据。然后,应用程序手动更新或使缓存中的相关数据失效,以确保缓存中的数据保持与主数据源一致。

3. 缓存失效

在 Cache-Aside 模式中,缓存失效是由应用程序来管理的。这意味着应用程序需要根据数据的更新频率和业务需求,手动决定何时使缓存数据失效,以便在下次访问时重新加载最新的数据。

4.代码示例

基于Spring Cloud的Cache-Aside Pattern缓存模式可以通过Spring框架的缓存抽象和Spring Cloud的服务组件来实现。在这种模式下,应用程序负责手动地管理缓存数据的加载和失效。以下是一个基于Spring Cloud的Cache-Aside Pattern缓存模式的伪代码示例:

  • 定义缓存配置和服务

首先,需要定义一个缓存配置类和一个缓存服务类来管理缓存和数据读写操作。

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
@EnableCaching
public class CacheConfig {@Beanpublic CacheManager cacheManager() {return new ConcurrentMapCacheManager("myCache"); // 创建一个名为 "myCache" 的缓存}
}@Service
public class CacheService {private final Cache cache;public CacheService(CacheManager cacheManager) {this.cache = cacheManager.getCache("myCache"); // 获取名为 "myCache" 的缓存}public Object getDataFromCache(String key) {Cache.ValueWrapper valueWrapper = cache.get(key);if (valueWrapper != null) {return valueWrapper.get();}return null;}public void putDataInCache(String key, Object data) {cache.put(key, data); // 将数据加载到缓存中}public void invalidateCache(String key) {cache.evict(key); // 使缓存中的数据失效}
}
  • 使用缓存服务

业务逻辑中,可以使用缓存服务来读取和写入数据。

@Service
public class BusinessService {private final DataService dataService;private final CacheService cacheService;@Autowiredpublic BusinessService(DataService dataService, CacheService cacheService) {this.dataService = dataService;this.cacheService = cacheService;}public Object fetchData(String key) {Object cachedData = cacheService.getDataFromCache(key);if (cachedData != null) {return cachedData;} else {Object newData = dataService.fetchDataFromDataSource(key);cacheService.putDataInCache(key, newData);return newData;}}public void updateData(String key, Object newData) {dataService.updateDataSource(key, newData); // 更新主数据源cacheService.invalidateCache(key); // 使缓存中的数据失效}
}
  • 使用示例:

可以通过业务服务来读取和更新数据。

@RestController
public class MyController {private final BusinessService businessService;@Autowiredpublic MyController(BusinessService businessService) {this.businessService = businessService;}@GetMapping("/data/{key}")public Object getData(@PathVariable String key) {return businessService.fetchData(key); // 从缓存或数据源获取数据}@PostMapping("/data/{key}")public void updateData(@PathVariable String key, @RequestBody Object newData) {businessService.updateData(key, newData); // 更新数据并使缓存失效}
}

在这个示例中,我们使用了Spring框架的缓存注解和缓存管理器来实现Cache-Aside Pattern。通过CacheService来操作缓存数据的读写,通过BusinessService来处理业务逻辑,保证从缓存读取数据或将数据加载到缓存时的一致性。


Read-Through/Write through

Read-Through和Write-Through是两种常见的缓存模式,用于更有效地管理缓存中的数据读取和写入操作。它们分别用于在数据被读取和写入时自动操作缓存和主数据源。下面我将详细介绍这两种缓存模式,并提供基于Spring的伪代码示例。

Read-Through缓存模式

在Read-Through缓存模式中,当应用程序尝试读取缓存中的数据时,如果缓存中不存在该数据,会自动从主数据源(如数据库)中读取数据,并将数据加载到缓存中,以便下次读取时能够直接从缓存中获取。

工作流程:

  1. 应用程序尝试从缓存读取数据。

  2. 如果缓存中存在数据,应用程序直接从缓存中获取。

  3. 如果缓存中不存在数据,应用程序从主数据源中读取数据,并将数据加载到缓存中。

Write-Through缓存模式

在Write-Through缓存模式中,当应用程序执行写操作时,数据会首先被写入缓存,然后自动同步更新到主数据源(如数据库)中,以保持数据的一致性。

工作流程:

  1. 应用程序执行写操作,将数据写入缓存。

  2. 缓存自动将写入的数据同步更新到主数据源中。

代码示例

基于Spring Cloud的Read-Through和Write-Through缓存模式可以通过Spring框架的缓存抽象和Spring Cloud的服务组件来实现。这两种模式是更为自动化的缓存管理方法,它们分别处理数据的读取和写入操作,无需应用程序手动介入。以下是基于Spring Cloud的伪代码示例:

Read-Through缓存模式示例

  • 定义缓存配置和服务:

首先,需要定义一个缓存配置类和一个数据服务类,用于管理缓存和数据读取操作。

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
@EnableCaching
public class CacheConfig {@Beanpublic CacheManager cacheManager() {return new ConcurrentMapCacheManager("myCache"); // 创建一个名为 "myCache" 的缓存}
}@Service
public class DataService {@Autowiredprivate DataRepository dataRepository; // 假设有一个数据仓库@Cacheable(value = "myCache", key = "#key")public Data getData(String key) {return dataRepository.findById(key).orElse(null);}
}
  • 使用缓存服务:

在业务逻辑中,可以使用缓存服务来读取数据。

@RestController
public class MyController {private final DataService dataService;@Autowiredpublic MyController(DataService dataService) {this.dataService = dataService;}@GetMapping("/data/{key}")public Data getData(@PathVariable String key) {return dataService.getData(key); // 从缓存或数据仓库获取数据}
}

Write-Through缓存模式示例

  • 定义缓存配置和服务:

同样,你需要定义一个缓存配置类和一个数据服务类,用于管理缓存和数据写入操作。

@Configuration
@EnableCaching
public class CacheConfig {@Beanpublic CacheManager cacheManager() {return new ConcurrentMapCacheManager("myCache"); // 创建一个名为 "myCache" 的缓存}
}@Service
public class DataService {@Autowiredprivate DataRepository dataRepository; // 假设有一个数据仓库@CachePut(value = "myCache", key = "#data.key")public Data updateData(Data data) {dataRepository.save(data); // 更新数据到数据仓库return data;}
}
  • 使用缓存服务:

可以使用缓存服务来写入数据。

@RestController
public class MyController {private final DataService dataService;@Autowiredpublic MyController(DataService dataService) {this.dataService = dataService;}@PostMapping("/data")public Data updateData(@RequestBody Data newData) {return dataService.updateData(newData); // 写入数据到缓存和数据仓库}
}

在这些示例中,@Cacheable@CachePut注解用于实现Read-Through和Write-Through缓存模式。DataService类负责在数据读取和写入时处理缓存和主数据源之间的同步。


Write behind

Write-Behind缓存模式是一种缓存设计模式,它将写入操作先缓存起来,然后在合适的时机异步地将数据写入主数据源(例如数据库)。这种模式可以提高写入操作的性能和响应时间,同时通过异步写入减少主数据源的负载。

工作原理

  1. 当应用程序执行写入操作时,数据首先会被写入缓存,然后标记为"脏数据"。
  2. 后台异步线程定期或在特定事件触发时,将"脏数据"批量写入主数据源。

这种模式在需要频繁写入操作的场景中特别有用,因为它将写入操作进行了批处理,减少了与主数据源的交互次数,从而提高了性能。

Write-Behind缓存模式的优点

  • 提高写入操作的性能:写入操作首先在缓存中完成,减少了对主数据源的直接写入次数,从而提高了写入性能。
  • 减轻主数据源负载:异步写入减少了主数据源的负载,特别是在高并发的情况下。
  • 高吞吐量:通过批量写入的方式,可以提高系统的吞吐量。

代码示例

基于Spring Cloud的Write-Behind缓存模式可以使用Spring框架的缓存抽象和Spring Cloud的服务组件来实现。这种模式可以提高写入操作的性能和响应时间,同时通过异步写入减少主数据源的负载。以下是基于Spring Cloud的Write-Behind缓存模式的伪代码示例:

  • 定义缓存配置和服务:

首先,需要定义一个缓存配置类和一个缓存服务类,用于管理缓存和数据写入操作。

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;@Configuration
@EnableCaching
@EnableAsync
public class CacheConfig {@Beanpublic CacheManager cacheManager() {return new ConcurrentMapCacheManager("myCache"); // 创建一个名为 "myCache" 的缓存}@Beanpublic ThreadPoolTaskExecutor taskExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();executor.setCorePoolSize(5);executor.setMaxPoolSize(10);executor.setQueueCapacity(25);return executor;}
}@Service
public class CacheService {@Autowiredprivate DataRepository dataRepository;@CachePut(value = "myCache", key = "#data.key")@Asyncpublic void writeBehind(Data data) {// 异步将数据写入主数据源writeDataToDataSource(data);}private void writeDataToDataSource(Data data) {dataRepository.save(data); // 写入数据到主数据源}
}
  • 使用缓存服务:

在业务逻辑中,可以使用缓存服务来写入数据。

@RestController
public class MyController {private final CacheService cacheService;@Autowiredpublic MyController(CacheService cacheService) {this.cacheService = cacheService;}@PostMapping("/data")public void writeData(@RequestBody Data newData) {cacheService.writeBehind(newData); // 异步写入数据到缓存和主数据源}
}

在这个示例中,我们使用了Spring框架的@CachePut注解和异步任务来实现Write-Behind缓存模式。CacheService负责在数据写入缓存的同时,异步地将数据写入主数据源。这样可以提高写入性能,并减轻主数据源的负载。

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

相关文章:

  • KVM环境下制作ubuntu qcow2格式镜像
  • 基于SpringBoot+Vue的高校竞赛管理系统
  • PHP发邮件教程:配置SMTP服务器发送邮件?
  • SpringBootWeb增删改查入门案例
  • pytorch实现RNN网络
  • 智能工厂的软件设计 “程序program”表达式,即 接口模型的代理模式表达式
  • leetcode 难度【简单模式】标签【数据库】题型整理大全
  • 利士策分享,自我和解:通往赚钱与内心富足的和谐之道
  • 【物联网】深入解析时序数据库TDengine及其Java应用实践
  • 2023北华大学程序设计新生赛部分题解
  • PPP的配置
  • 回溯算法总结篇
  • 机器学习-点击率预估-论文速读-20240916
  • 【leetcode】堆习题
  • 前端大模型入门:编码(Tokenizer)和嵌入(Embedding)解析 - llm的输入
  • 一文读懂 JS 中的 Map 结构
  • C++校招面经(二)
  • Python Web 面试题
  • java日志框架之JUL(Logging)
  • ARM驱动学习之PWM
  • 我的AI工具箱Tauri版-VideoClipMixingCut视频批量混剪
  • postgres_fdw访问存储在外部 PostgreSQL 服务器中的数据
  • 什么是3D展厅?有何优势?怎么制作3D展厅?
  • Linux下的CAN通讯
  • 【Python】pip安装加速:使用国内镜像源
  • SpringBoot lombok(注解@Getter @Setter)
  • descrTable常用方法
  • 回归预测 | Matlab实现ReliefF-XGBoost多变量回归预测
  • 年度最强悬疑美剧重磅回归,一集比一集上头
  • AI一点通: 简化大数据与深度学习工作流程, Apache Spark、PyTorch 和 Mosaic Streaming