SpringBoot 整合 jetcache缓存

目前 jetcache 支持的本地缓存方案有两种,远程缓存支持两种,分别如下:

  • 本地缓存(Local)
    • LinkedHashMap
    • Caffeine
  • 远程缓存(Remote)
    • Redis

    • Tair

依赖导入

xml 复制代码
<dependency>
    <groupId>com.alicp.jetcache</groupId>
    <artifactId>jetcache-starter-redis</artifactId>
    <version>2.6.2</version>
</dependency>

配置

远程方案基本配置,当前默认使用的远程方案是 redis

yaml 复制代码
jetcache:
  statIntervalMinutes: 1
  local:
    default:
      type: linkedhashmap
      keyConvertor: fastjson
  remote:
    default:
      type: redis
      host: localhost
      port: 6379
      keyConvertor: fastjson
      valueEncode: java
      valueDecode: java
      poolConfig:
        maxTotal: 50
    localdemo:  # 自定义本地文件夹内(area)
      type: redis
      host: localhost
      port: 6379
      keyConvertor: fastjson
      valueEncode: java
      valueDecode: java
      poolConfig:
        maxTotal: 50
    remotedemo: # 自定义远程文件夹内(area)
      type: redis
      host: localhost
      port: 6379
      keyConvertor: fastjson
      valueEncode: java
      valueDecode: java
      poolConfig:
        maxTotal: 50

其中 poolConfig 是必配项,否则会报错

步骤③:启用缓存,在引导类上方标注注解 @EnableCreateCacheAnnotation 配置 springboot 程序中可以使用注解的形式创建缓存

java 复制代码
@EnableCreateCacheAnnotation  # 可以不要,属性缓存注解
@EnableMethodCache(basePackages = "com.example.demo")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }


}

cacheType 控制当前缓存使用本地缓存还是远程缓存,配置 cacheType=CacheType.LOCAL 即使用本地缓存。

本地 + 远程方案

在创建缓存的时候,配置 cacheType 为 BOTH 即则本地缓存与远程缓存同时使用。

cacheType 如果不进行配置,默认值是 REMOTE,即仅使用远程缓存方案。关于 jetcache 的配置,参考以下信息

属性 默认值 说明
jetcache.statIntervalMinutes 0 统计间隔,0 表示不统计
jetcache.hiddenPackages 无 自动生成 name 时,隐藏指定的包名前缀
jetcache.local|remote.${area}.type 无 缓存类型,本地支持 linkedhashmap、caffeine,远程支持 redis、tair
jetcache.local|remote.${area}.keyConvertor 无 key 转换器,当前仅支持 fastjson
jetcache.local|remote.${area}.valueEncoder java 仅 remote 类型的缓存需要指定,可选 java 和 kryo
jetcache.local|remote.${area}.valueDecoder java 仅 remote 类型的缓存需要指定,可选 java 和 kryo
jetcache.local|remote.${area}.limit 100 仅 local 类型的缓存需要指定,缓存实例最大元素数
jetcache.local|remote.${area}.expireAfterWriteInMillis 无穷大 默认过期时间,毫秒单位
jetcache.local.${area}.expireAfterAccessInMillis 0 仅 local 类型的缓存有效,毫秒单位,最大不活动间隔

以上方案仅支持手工控制缓存,但是 springcache 方案中的方法缓存特别好用,给一个方法添加一个注解,方法就会自动使用缓存。jetcache 也提供了对应的功能,即方法缓存。

java 复制代码
package com.example.demo.controller;

import com.alicp.jetcache.anno.*;
import com.example.demo.entity.User;
import com.example.demo.service.IUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author 蔡定努
 * @date 2023/10/05 14:41
 */
@RestController
public class CacheController {

    @Resource
    private IUserService userService;


    /**
     * 使用缓存
     * @author 蔡定努
     */
    @GetMapping("get")
    @Cached(name = "user:", key = "#id", expire = 36000, cacheType = CacheType.BOTH)
    @CacheRefresh(refresh = 5,timeUnit = TimeUnit.MINUTES) //定时刷新缓存
    public User get(String id) {
        return userService.getById(id);
    }


    @GetMapping("areaGet")
    @Cached(area = "demo",name = "user:", key = "#id", expire = 36000, cacheType = CacheType.REMOTE)
    public User areaGet(String id) {
        return userService.getById(id);
    }

    /**
     * 
     * 删除缓存
     * @author 蔡定努
     */
    @GetMapping("in")
    @CacheInvalidate(name = "user:", key = "#id")
    public void invaid(String id) {
//     数据处理逻辑


    }


    /**
     * 更新缓存
     * @author 蔡定努
     */
    @PostMapping("upadte")
    @CacheUpdate(name = "user:", key = "#id", value = "#user")
    public void upadte(@RequestBody  User user) {
//        跟新user
        userService.updateById(user);
//        int a=1/0;  //缓存不一致
    }



}

设置后,每 1 分钟在控制台输出缓存数据命中信息

shell 复制代码
2023-10-05 16:15:00.013  INFO 16027 --- [DefaultExecutor] c.alicp.jetcache.support.StatInfoLogger  : jetcache stat from 2023-10-05 16:14:28,692 to 2023-10-05 16:15:00,003
cache     |       qps|   rate|           get|           hit|          fail|        expire|avgLoadTime|maxLoadTime
----------+----------+-------+--------------+--------------+--------------+--------------+-----------+-----------
demo_user:|      0.08| 50.00%|             2|             1|             0|             0|      149.0|        149
----------+----------+-------+--------------+--------------+--------------+--------------+-----------+-----------

总结

  1. jetcache 是一个类似于 springcache 的缓存解决方案,自身不具有缓存功能,它提供有本地缓存与远程缓存多级共同使用的缓存解决方案
  2. 注意数据进入远程缓存时的类型转换问题
  3. jetcache 提供方法缓存,并提供了对应的缓存更新与刷新功能
  4. jetcache 提供有简单的缓存信息命中报表方便开发者即时监控缓存数据命中情况
相关推荐
CoderYanger8 小时前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第八弹):MyBatis Generator
java·数据库·spring boot·spring·java-ee·tomcat·mybatis
jaysee-sjc11 小时前
【黑马点评】Day02:优惠券秒杀与分布式锁
java·分布式·nginx·spring·node.js·tomcat·maven
五彩小白11 小时前
Redis的过期删除和内存淘汰机制
数据库·redis·缓存
学长毕设11 小时前
基于SpringBoot 博物馆文物租赁及修复管理系统-附源码
大数据·数据库·spring boot
行者-全栈开发12 小时前
SpringBoot + GraalVM 原生编译实战:启动时间从8秒到0.5秒的极致优化
java·spring boot·后端
CoderYanger12 小时前
Java EE 进阶:10. MyBatis-Plus使用(第一弹)
java·spring boot·后端·sql·spring·java-ee·mybatis
千寻技术帮16 小时前
基于Spring Boot+Vue的农产品销售管理系统(源码+文档+数据库,附部署教程)
java·数据库·spring boot·后端·mysql·vue
码云社区16 小时前
Spring Boot陪诊陪护系统源码:医院目录、服务项目与城市业务配置
java·spring boot·后端·陪诊陪护·医院陪诊·医院陪护·陪护小程序
码云社区16 小时前
Spring Boot云快充协议源码:充电桩远程重启、参数配置与升级运维方案
运维·spring boot·后端·充电桩·充电系统·充电软件
千寻技术帮17 小时前
微信小程序血压管理系统设计与实现_50054(Spring Boot + Vue + MySQL)
spring boot·小程序·vue·毕业设计·课程设计·血压助手