- jetCache对SpringCache进行了封装,在原有功能基础上实现了多级缓存、缓存统计、自动刷新、异步调用、数据报表等功能。
- jetCache 设定了本地缓存和远程缓存的多级缓存解决方案:
1、本地缓存
(1)LinkedHashMap
(2)Caffeine
2、远程缓存
(1)Redis
(2)Tair
- jetCache 本地缓存、远程缓存配置:org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test com.alicp.jetcache jetcache-starter-redis 2.6.7 com.baomidou mybatis-plus-boot-starter 3.4.3 mysql mysql-connector-java 8.0.28 org.springframework.boot spring-boot-starter-web com.alibaba druid-spring-boot-starter 1.2.6
jetcache:
statIntervalMinutes: 15 # 指定统计间隔,以分钟为单位。0表示没有统计数据。
areaInCacheName: true # jetcache-anno使用缓存名称作为远程缓存密钥前缀,在jetcache 2.4.3 和之前的版本中,它总是在缓存名称中添加区域名称,从2.4.4开始我们有这个配置项,为兼容原因,默认值为true。
local:
default:
keyConvertor: fastjson # key 通过 fastjson 转换为 json
type: linkedhashmap
poolConfig:
minIdle: 5 # 连接池中的最小空闲连接数
maxIdle: 20 # 连接池中的最大空闲连接数
maxTotal: 50 # 连接池中的最大连接数
remote:
default:
keyConvertor: fastjson # key 通过 fastjson 转换为 json
valueEncoder: java #全局配置值编码器只需要远程缓存。两个内置valueEncoder是java和kryo
valueDecoder: java #全局配置值解码器只需要远程缓存。两个内置valueEncoder是java和kryo
type: redis # 类型,远程缓存类型有redis和tair
host: localhost # 远程缓存地址
port: 6379 # 远程缓存端口
password: hd123 # 缓存密码
poolConfig:
minIdle: 5 # 连接池中的最小空闲连接数
maxIdle: 20 # 连接池中的最大空闲连接数
maxTotal: 50 # 连接池中的最大连接数
- 实体类Book.java
import lombok.*;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Book implements Serializable {
private String id;
private String name;
private String description;
private Float price;
}
- 业务层代码:
// BookService.java import com.baomidou.mybatisplus.extension.service.IService; import com.example.springboot.entity.Book; public interface BookService extends IService{ Book getCacheById(String id); Boolean checkCacheById(String id, String name); }
// BookServiceImpl.java import com.alicp.jetcache.Cache; import com.alicp.jetcache.anno.CreateCache; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.example.springboot.dao.BookDao; import com.example.springboot.entity.Book; import com.example.springboot.service.BookService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.Cacheable; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import java.util.concurrent.TimeUnit; @Service public class BookServiceImpl extends ServiceImpl- 表现层代码:implements BookService { // 这个注解中的 area 要与yml中的配置保持一致 // cacheType 配置用本地缓存还是远程缓存 // CacheType.LOCAL、CacheType.REMOTE、CacheType.BOTH 三种 @CreateCache(area = "default", name="jetCache_", expire = 3600, timeUnit = TimeUnit.SECONDS, cacheType = CacheType.LOCAL) private Cache jetCache; @Override public Book getCacheById(String id) { jetCache.put(id, getById(id)); return null; } @Override public Boolean checkCacheById(String id, String name) { Book cacheData = jetCache.get(id); return name.equals(cacheData.getName()); } }
import com.example.springboot.entity.Book;
import com.example.springboot.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private BookService bookService;
@GetMapping("{id}")
public Book getById(@PathVariable String id){
return bookService.getCacheById(id);
}
@PostMapping
public Boolean checkById(@RequestBody Book book){
return bookService.checkCacheById(book.getId(), book.getName());
}
}
- 主启动类要添加@EnableCreateCacheAnnotation注解
import com.alicp.jetcache.anno.config.EnableCreateCacheAnnotation;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
// jetcache 启用缓存的主开关
@EnableCreateCacheAnnotation
public class SpringBootjetCacheApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootjetCacheApplication.class, args);
}
}
- Postman 测试如下:
-
往缓存中放数据:
-
从缓存中取数据:
- 解决办法:SpringBoot在整合jetcache时出现循环依赖报错时,是SpringBoot和jetcache的版本冲突,寻找对应版本即可:SpringBoot2.7.1和jetcache2.6.7经测试可以。
import com.alicp.jetcache.anno.config.EnableCreateCacheAnnotation;
import com.alicp.jetcache.anno.config.EnableMethodCache;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
// jetcache 启用缓存的主开关
@EnableCreateCacheAnnotation
// 开启方法注解缓存
@EnableMethodCache(basePackages = "com.example.springboot")
public class SpringBootjetCacheApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootjetCacheApplication.class, args);
}
}
- 业务层代码:
// BookService.java import com.baomidou.mybatisplus.extension.service.IService; import com.example.springboot.entity.Book; public interface BookService extends IService{ // Book getCacheById(String id); // Boolean checkCacheById(String id, String name); Book getBookById(String id); Boolean updateBook(Book book); Boolean deleteBookById(String id); }
// BookServiceImpl.java import com.alicp.jetcache.Cache; import com.alicp.jetcache.anno.*; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.example.springboot.dao.BookDao; import com.example.springboot.entity.Book; import com.example.springboot.service.BookService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.Cacheable; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import java.util.concurrent.TimeUnit; @Service public class BookServiceImpl extends ServiceImpl- 表现层代码:implements BookService { // @CreateCache(area = "default", name="jetCache_", expire = 3600, timeUnit = TimeUnit.SECONDS, cacheType = CacheType.REMOTE) // private Cache jetCache; // @Override // public Book getCacheById(String id) { // jetCache.put(id, getById(id)); // return null; // } // @Override // public Boolean checkCacheById(String id, String name) { // Book cacheData = jetCache.get(id); // return name.equals(cacheData.getName()); // } @Override // @Cached 注解用于向缓存中放数据 @Cached(area = "default", name="jetMethodCache_", key = "#id", expire = 3600, cacheType = CacheType.REMOTE) // @CacheRefresh 注解用于在 60s 就重新向数据库查询数据并放到缓存中 @CacheRefresh(refresh = 60, timeUnit = TimeUnit.SECONDS) public Book getBookById(String id) { return getById(id); } @Override // @CacheUpdate 注解用于在更新数据库数据时,同步更新到缓存中 @CacheUpdate(name="jetMethodCache_", key = "#book.id", value = "#book") public Boolean updateBook(Book book) { return updateById(book); } @Override // @CacheInvalidate 注解用于在删除数据库数据时,同步将缓存中的对应数据删除 @CacheInvalidate(name = "jetMethodCache_", key = "#id") public Boolean deleteBookById(String id) { return removeById(id); } }
// BookController.java
import com.example.springboot.entity.Book;
import com.example.springboot.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private BookService bookService;
// @GetMapping("{id}")
// public Book getById(@PathVariable String id){
// return bookService.getCacheById(id);
// }
// @PostMapping
// public Boolean checkById(@RequestBody Book book){
// return bookService.checkCacheById(book.getId(), book.getName());
// }
@GetMapping("{id}")
public Book getBookById(@PathVariable String id){
return bookService.getBookById(id);
}
@PutMapping
public Boolean updateBook(@RequestBody Book book){
return bookService.updateBook(book);
}
@DeleteMapping("{id}")
public Boolean deleteBookById(@PathVariable String id){
return bookService.deleteBookById(id);
}
}
- @Cached、@CacheRefresh、@CacheUpdate、@CacheInvalidate介绍:
- @Cached 注解用于向缓存中放数据
- @CacheRefresh 注解用于在指定时间后就重新向数据库查询数据并放到缓存中
- @CacheUpdate 注解用于在更新数据库数据时,同步更新到缓存中
- @CacheInvalidate 注解用于在删除数据库数据时,同步将缓存中的对应数据删除



