实战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缓存模式的伪代码示例:

  • 定义缓存配置和服务

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

java 复制代码
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableCaching
public class CacheConfig {
    @Bean
    public 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); // 使缓存中的数据失效
    }
}
  • 使用缓存服务

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

javascript 复制代码
@Service
public class BusinessService {
    private final DataService dataService;
    private final CacheService cacheService;

    @Autowired
    public 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); // 使缓存中的数据失效
    }
}
  • 使用示例:

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

javascript 复制代码
@RestController
public class MyController {
    private final BusinessService businessService;

    @Autowired
    public 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缓存模式示例

  • 定义缓存配置和服务:

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

java 复制代码
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableCaching
public class CacheConfig {
    @Bean
    public CacheManager cacheManager() {
        return new ConcurrentMapCacheManager("myCache"); // 创建一个名为 "myCache" 的缓存
    }
}

@Service
public class DataService {
    @Autowired
    private DataRepository dataRepository; // 假设有一个数据仓库

    @Cacheable(value = "myCache", key = "#key")
    public Data getData(String key) {
        return dataRepository.findById(key).orElse(null);
    }
}
  • 使用缓存服务:

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

java 复制代码
@RestController
public class MyController {
    private final DataService dataService;

    @Autowired
    public MyController(DataService dataService) {
        this.dataService = dataService;
    }

    @GetMapping("/data/{key}")
    public Data getData(@PathVariable String key) {
        return dataService.getData(key); // 从缓存或数据仓库获取数据
    }
}

Write-Through缓存模式示例

  • 定义缓存配置和服务:

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

javascript 复制代码
@Configuration
@EnableCaching
public class CacheConfig {
    @Bean
    public CacheManager cacheManager() {
        return new ConcurrentMapCacheManager("myCache"); // 创建一个名为 "myCache" 的缓存
    }
}

@Service
public class DataService {
    @Autowired
    private DataRepository dataRepository; // 假设有一个数据仓库

    @CachePut(value = "myCache", key = "#data.key")
    public Data updateData(Data data) {
        dataRepository.save(data); // 更新数据到数据仓库
        return data;
    }
}
  • 使用缓存服务:

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

java 复制代码
@RestController
public class MyController {
    private final DataService dataService;

    @Autowired
    public 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缓存模式的伪代码示例:

  • 定义缓存配置和服务:

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

java 复制代码
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 {
    @Bean
    public CacheManager cacheManager() {
        return new ConcurrentMapCacheManager("myCache"); // 创建一个名为 "myCache" 的缓存
    }

    @Bean
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(25);
        return executor;
    }
}

@Service
public class CacheService {
    @Autowired
    private DataRepository dataRepository;

    @CachePut(value = "myCache", key = "#data.key")
    @Async
    public void writeBehind(Data data) {
        // 异步将数据写入主数据源
        writeDataToDataSource(data);
    }

    private void writeDataToDataSource(Data data) {
        dataRepository.save(data); // 写入数据到主数据源
    }
}
  • 使用缓存服务:

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

java 复制代码
@RestController
public class MyController {
    private final CacheService cacheService;

    @Autowired
    public MyController(CacheService cacheService) {
        this.cacheService = cacheService;
    }

    @PostMapping("/data")
    public void writeData(@RequestBody Data newData) {
        cacheService.writeBehind(newData); // 异步写入数据到缓存和主数据源
    }
}

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

相关推荐
wuyikeer25 分钟前
windows同时安装两个不同版本的Mysql
windows·mysql·adb
pqq的迷弟26 分钟前
基于redis实现限流逻辑
redis
深蓝轨迹1 小时前
黑马点评-day02-缓存笔记
redis·笔记·缓存·mybatis
清风徐来QCQ1 小时前
redis 面试可能会问的问题
数据库·redis·面试
油丶酸萝卜别吃1 小时前
springboot项目中redis常见的增删改查操作是哪些?
spring boot·redis·bootstrap
清风徐来QCQ2 小时前
Redis以及如何在springboot中使用
数据库·redis·缓存
xcLeigh2 小时前
告别 Excel 繁琐操作!Metabase让数据可视化触手可及
mysql·docker·信息可视化·excel·数据可视化·metabase·cpolar
编程饭碗2 小时前
【Mysql日期字段】
数据库·mysql
Thomas.Sir2 小时前
精通 MySQL 面试题
数据结构·数据库·mysql
鸽芷咕2 小时前
从语法兼容到语义一致:深度解析金仓如何“无感”承接MySQL复杂业务
数据库·mysql