JetCache是阿里推出的一套替代springcache的缓存方案。
JetCache是对SpringCache进行了封装,在原有基础上实现了多级缓存、缓存统计、自动刷新、异步调用、数据报表等功能。
JetCache设定了本地缓存与远程缓存的多级缓存方案
jetcache官方源码: https://github.com/alibaba/jetcache
本demo的springboot版本2.5.4
<dependency>
<groupId>com.alicp.jetcachegroupId>
<artifactId>jetcache-starter-redisartifactId>
<version>2.6.2version>
dependency>
jetcache:
remote:
default:
type: redis
host: localhost
port: 6379
poolConfig:
maxTotal: 50
@SpringBootApplication
@EnableCreateCacheAnnotation //开启缓存
public class SpringbootMybatisplusApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisplusApplication.class, args);
}
}
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<String,String> 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);
}
}
配置area
@CreateCache(area = “sms”,name=“jetCache”,expire = 3600)
配置对应空间名的配置
jetcache:
local:
default:
type: linkedhashmap
keyConvertor: fastjson #key的转换器
@CreateCache(name="jetCache",expire = 3600,cacheType= CacheType.LOCAL)
private Cache<String,String> jetCache;
默认的方案是REMOTE远程方案。可以只选择本地方案或者远程方案中的一种,也可以同时存在。
@SpringBootApplication
@EnableCreateCacheAnnotation //开启缓存
@EnableMethodCache(basePackages = {"com.it2"}) //配置缓存方法需要扫描的包
public class SpringbootMybatisplusApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisplusApplication.class, args);
}
}
@Cached(name="book",key="#id",expire = 3600)
public Book queryById(Integer id) {
System.out.println("读取数据库:id="+id);
return bookMapper.selectById(id);
}
@Data
public class Book implements Serializable {
private int id;
private String name;
private String type;
private String description;
}
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的转换器
使用@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(name=“book_”,key=“#id”)注解
@CacheInvalidate(name="book_",key="#id")
public boolean deleteBook(Integer id) {
System.out.println("删除了数据:id="+id);
return true;
}
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快速入门