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

Redis结合Caffeine实现二级缓存:提高应用程序性能

本文将详细介绍如何使用CacheFrontend和Caffeine来实现二级缓存。

1. 简介

CacheFrontend: 是一种用于缓存的前端组件或服务。通俗的讲:该接口可以实现本地缓存与redis自动同步,如果本地缓存(JVM级)有数据,则直接从本地缓存中返回数据。否则从redis获取。

CacheFrontend的工作方式通常涉及以下两个方面:

  • 数据存储:CacheFrontend会将从后端(Redis)获取的数据存储在本地缓存中。这样,在后续请求中,如果相同的数据被请求,CacheFrontend可以直接从本地缓存中返回,而无需再次访问后端存储(Redis)。这减少了对后端存储的访问次数,提高了响应速度。

  • 数据更新和失效:当后端存储中的数据发生变化时,CacheFrontend需要相应地更新本地缓存中的数据。它可以通过主动监测后端存储的变化。

通过使用CacheFrontend,应用程序可以减轻后端存储的负载,提高系统的性能和可扩展性,从而改善用户体验。

CacheFrontend是由 Lettuce 提供,Lettuce 提供了 ClientSideCaching 实现,在该实现类中接受一个CacheAccessor缓存访问器。我们可以根据具体需要实现自己的缓存访问器。本文将自定义CacheAccessor结合Caffeine实现缓存策略。

2. 实战案例

2.1 依赖管理

<!--高性能本地缓存实现-->
<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.2 配置文件

spring:
  redis:
    host: localhost
    password: xxxooo
    database: 10
    port: 6379

2.2 配置

本地缓存配置
@Bean
public Cache<String, String> localCache() {
  return  Caffeine.newBuilder()
    // 初始容量
    .initialCapacity(100)
    .maximumSize(1000)
    .build() ;
}
RedisClient配置
@Bean
public RedisClient redisClient(RedisProperties props) {
  RedisURI clientResources = RedisURI.Builder
      .redis(props.getHost(), props.getPort())
      .withPassword(props.getPassword().toCharArray())
      .withDatabase(props.getDatabase())
      .build() ;
  RedisClient client = RedisClient.create(clientResources) ;
  return client ;
}

RedisProperties是系统内部自动注册的Bean。

缓存前端配置CacheFrontend
@Bean
  public CacheFrontend<String, String> cacheFrontend(RedisClient redisClient, Cache<String, String> localCache) {
    StatefulRedisConnection<String, String> connection = redisClient.connect() ;
    // 添加监听器,当缓存失效后会被监听到。
    connection.addListener(new PushListener() {
      @Override
      public void onPushMessage(PushMessage message) {
        String type = message.getType() ;
        if ("invalidate".equals(type)) {
          System.out.println("...") ;
        }
        List<Object> contents = message.getContent() ;
        Object content = contents.get(0);
        if (content instanceof ByteBuffer) {
          ByteBuffer buf = (ByteBuffer) content ;
          System.out.printf("response content: %s%n", StringCodec.UTF8.decodeValue(buf)) ;
        }
      }
    }) ;
    // 缓存访问器
    CacheAccessor<String, String> cacheAccessor = new CacheAccessor<String, String>() {
      @Override
      public String get(String key) {
        @Nullable
        String present = localCache.getIfPresent(key) ;
        System.out.printf("get operator: %s%n", present) ;
        return present ;
      }
      @Override
      public void put(String key, String value) {
        localCache.put(key, value) ;
        System.out.printf("put operator: key = %s, value = %s%n", key, value) ;
      }
      @Override
      public void evict(String key) {
        localCache.invalidate(key) ;
        System.out.printf("evict operator: %s%n", key) ;
      }
    };
    CacheFrontend<String, String> frontend = ClientSideCaching.enable(
        cacheAccessor, 
        connection,
        TrackingArgs.Builder.enabled()) ;
    return frontend ;
  }

总结:通过本地缓存和Redis缓存可以带来诸多好处,如提高系统性能、减轻数据库压力、支持高并发等。通过合理配置和管理本地缓存与Redis缓存,可以优化应用程序的性能,提高系统的稳定性和可用性。在实际应用中,根据业务需求选择合适的缓存策略和数据结构,并进行持续的性能监控和调优,是确保系统高效运行的关键。


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

相关文章:

  • 计算机组成原理之数制与编码
  • 交通标志与路面标识检测系统源码分享
  • linux 系统磁盘空间查看与清理
  • 基于nodejs+vue的旅游管理系统
  • 【OS】计算机系统概述|操作系统基本概念|并发|并行|虚拟异步
  • 如何在O2OA中使用ElementUI组件进行审批流程工作表单设计
  • C++:模拟实现vector
  • 2025秋招内推|招联金融
  • 推荐常用的搜索渠道
  • Unity 热更新(HybridCLR+Addressable)-创建Addressable资源
  • H.264与H.265
  • FFmpeg源码:avio_seek函数分析
  • Codeforces Round 301 (Div. 2) C题 Ice Cave(BFS)
  • 昇思MindSpore进阶教程--高级自动微分
  • 基于springboot+小程序的儿童预防接种预约管理系统(疫苗1)(源码+sql脚本+视频导入教程+文档)
  • 依赖倒置原则(学习笔记)
  • PostgreSQL的表碎片
  • 学习Java (五)
  • Go Sonyflake学习与使用
  • 新能源汽车充电桩怎么选?
  • Linux基础(二):磁盘分区
  • js替换css主题变量并切换iconfont文件
  • uniapp中h5环境添加console.log输出
  • 2024年7月大众点评沈阳美食店铺基础信息
  • 数据结构和算法之树形结构(4)
  • springframework Ordered接口学习
  • BOE(京东方)携故宫博物院举办2024“照亮成长路”公益项目落地仪式以创新科技赋能教育可持续发展
  • 计算机网络--TCP、UDP抓包分析实验
  • 2024年配置YOLOX运行环境+windows+pycharm24.0.1+GPU
  • [C语言]--自定义类型: 结构体