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

实战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 {
    @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); // 使缓存中的数据失效
    }
}
  • 使用缓存服务

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

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

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

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

  • 定义缓存配置和服务:

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

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);
    }
}
  • 使用缓存服务:

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

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

  • 定义缓存配置和服务:

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

@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;
    }
}
  • 使用缓存服务:

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

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

  • 定义缓存配置和服务:

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

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); // 写入数据到主数据源
    }
}
  • 使用缓存服务:

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

@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负责在数据写入缓存的同时,异步地将数据写入主数据源。这样可以提高写入性能,并减轻主数据源的负载。


http://www.kler.cn/news/313041.html

相关文章:

  • 【4.4】图搜索算法-BFS和DFS两种方式解岛屿数量
  • WPF DataGrid 赋值与修改
  • Spring Boot利用dag加速Spring beans初始化
  • 无人机黑飞打击技术详解
  • 页面关键路径渲染详解
  • Python中使用Scikit-learn进行线性回归分析的实用指南
  • API应用安全风险倍增,F5助企业赢得关键安全挑战
  • esp32s3 NVS空间读写操作
  • Java 每日一刊(第13期):this super static
  • 【Redis入门到精通三】Redis核心数据类型(List,Set)详解
  • Qt 中 `QTimer`定时器的使用方法详解
  • 蓝禾,汤臣倍健,三七互娱,得物,顺丰,快手,游卡,oppo,康冠科技,途游游戏,埃科光电25秋招内推
  • Java并发集合框架:高效多线程数据访问
  • Flask 实现用户登录功能的完整示例:前端与后端整合(附Demo)
  • ubuntu 20.04 ‘Wired Unmanaged‘ 网络无法配置解决方法
  • 政务安全运营核心能力模块
  • Stable Diffusion绘画 | ControlNet应用-instant-ID控制器:快速生成人物多角度图片
  • 大模型——LLaVA和LLaMA的介绍和区别
  • redis-shake v4全量增量同步redis数据
  • 海康VM脚本中使用opencvsharp和halcon
  • HelpLook VS GitBook,在线文档管理工具对比
  • 【工具变量】科技金融试点城市DID数据集(2000-2023年)
  • 论文阅读-《Attention is All You Need》
  • Redis 哨兵模式的选举算法是什么?
  • Python 课程12-Python 自动化应用
  • Java NIO(非阻塞IO)简介
  • 【秋招笔试-支持在线评测】8.28华为秋招(已改编)-三语言题解
  • 算法打卡 Day34(贪心算法)-分发饼干 + 摆动序列 + 最大子序和
  • 《粮油与饲料科技》是什么级别的期刊?是正规期刊吗?能评职称吗?
  • 【设计模式-桥接】