<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>com.alicp.jetcachegroupId>
<artifactId>jetcache-starter-redisartifactId>
<version>2.6.7version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.3version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.28version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.2.6version>
dependency>
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 # 连接池中的最大连接数
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> {
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<BookDao, Book> 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<String, Book> 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());
}
}
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);
}
}
往缓存中放数据:
从缓存中取数据:
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> {
// 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<BookDao, Book> 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);
}
}