栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot基础(42):jetCache缓存方案

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot基础(42):jetCache缓存方案

文章目录
  • 前言
  • jetcache远程缓存方案
    • 远程缓存(demo为redis)
    • 更换空间
  • jetcache本地缓存方案
    • 本地缓存(demo为linkedhashmap)
    • 默认的方案
  • 注解方式
    • 注解方式demo(@Cached)
    • 缓存更新(@CacheUpdate)
    • 缓存删除(@CacheInvalidate)
    • 缓存刷新(@CacheRefresh)
  • 缓存统计
  • 传送门

前言

JetCache是对SpringCache进行了封装,在原有基础上实现了多级缓存、缓存统计、自动刷新、异步调用、数据报表等功能。

JetCache设定了本地缓存与远程缓存的多级缓存方案

  • 本地缓存
    LinkedHashMap
    Caffeine
  • 远程缓存
    Redis
    Tair
    本地缓存和远程缓存可以任意组合。
jetcache远程缓存方案 远程缓存(demo为redis)

本demo的springboot版本2.5.4

  1. 导入依赖

        
            com.alicp.jetcache
            jetcache-starter-redis
            2.6.2
        
  1. 配置
jetcache:
  remote:
    default:
      type: redis
      host: localhost
      port: 6379
      poolConfig:
        maxTotal: 50
  1. 主类开启缓存注解
@SpringBootApplication
@EnableCreateCacheAnnotation  //开启缓存
public class SpringbootMybatisplusApplication {

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

}
  1. 使用
import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CreateCache;
import com.it2.springbootmybatisplus.pojo.SmsCode;
import com.it2.springbootmybatisplus.util.SmsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/sms")
public class SmsCodeController {

    @Autowired
    private SmsUtils smsUtils;

    @CreateCache(name="jetCache",expire = 3600)
    private Cache jetCache;

    @GetMapping
    public String getCode(String phone) {
        String code= smsUtils.createCode(phone);
        jetCache.put(phone,code);
        return code;
    }

    @PostMapping
    public boolean checkCode(SmsCode smsCode) {
        String code = smsCode.getCode();
//        String code2 = smsUtils.getCode(smsCode.getPhone());
        String code2 =  jetCache.get(smsCode.getPhone());
        return code.equals(code2);
    }

}
  1. 启动服务器测试


    查看缓存可以看到,它的空间是default,而前缀是jetCache。
更换空间

配置area
@CreateCache(area = “sms”,name=“jetCache”,expire = 3600)

配置对应空间名的配置

运行后可以查看到redis显示的前缀为sms为空间

jetcache本地缓存方案 本地缓存(demo为linkedhashmap)
  1. 配置本地缓存
jetcache:
  local:
    default:
      type: linkedhashmap
      keyConvertor: fastjson   #key的转换器
  1. 声明使用本地缓存
   @CreateCache(name="jetCache",expire = 3600,cacheType= CacheType.LOCAL)
    private Cache jetCache;
  1. 启动服务器测试

默认的方案

默认的方案是REMOTE远程方案。可以只选择本地方案或者远程方案中的一种,也可以同时存在。

注解方式 注解方式demo(@Cached)
  1. 在主类配置缓存方法需要扫描的包
@SpringBootApplication
@EnableCreateCacheAnnotation  //开启缓存
@EnableMethodCache(basePackages = {"com.it2"}) //配置缓存方法需要扫描的包
public class SpringbootMybatisplusApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisplusApplication.class, args);
    }
}
  1. 注解方法
    @Cached(name="book",key="#id",expire = 3600)
    public Book queryById(Integer id) {
        System.out.println("读取数据库:id="+id);
        return bookMapper.selectById(id);
    }
  1. 实现序列化接口Serializable,由于需要将Object缓存起来,所以需要实现序列化接口Serializable
@Data
public class Book implements Serializable {
    private int id;
    private String name;
    private String type;
    private String description;
}
  1. 修改配置文件
jetcache:
  remote:
    default:
      type: redis
      host: localhost
      port: 6379
      keyConvertor: fastjson   #key的转换器
      valueEncode: java
      valueDecode: java
      poolConfig:
        maxTotal: 50
    sms:
      type: redis
      host: localhost
      port: 6379
      poolConfig:
        maxTotal: 50
  local:
    default:
      type: linkedhashmap
      keyConvertor: fastjson   #key的转换器
  1. 启动服务器测试,发起请求,可以看到只有一次查询数据库操作
缓存更新(@CacheUpdate)

使用@CacheUpdate(name=“book_”,key=“#book.id”,value=“#book”)注解

    @CacheUpdate(name="book_",key="#book.id",value="#book")
    public boolean updateBook(Book book) {
        return bookMapper.updateById(book)>0;
    }
缓存删除(@CacheInvalidate)

使用@CacheInvalidate(name=“book_”,key=“#id”)注解

    @CacheInvalidate(name="book_",key="#id")
    public boolean deleteBook(Integer id) {
        System.out.println("删除了数据:id="+id);
        return true;
    }
缓存刷新(@CacheRefresh)

refresh 设置缓存自动刷新的时间,间隔秒

    @Cached(name="book_",key="#id",expire = 3600)
    @CacheRefresh(refresh = 30)
    public Book queryById(Integer id) {
        System.out.println("读取数据库:id="+id);
        return bookMapper.selectById(id);
    }
缓存统计

配置缓存统计,并重启

jetcache:
  statIntervalMinutes: 1 #缓存统计频率(分钟)

多次请求接口,在控制台查看统计缓存的命中信息

传送门

阿里巴巴开源的通用缓存访问框架JetCache介绍
JetCache快速入门

暑期编程PK赛 得CSDN机械键盘等精美礼品!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1016755.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号