mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
doc: 新增数据缓存说明文档
This commit is contained in:
parent
d0b7c8f269
commit
0e652ad529
@ -78,6 +78,7 @@ export default defineConfig({
|
||||
{text: '乐观锁', link: '/zh/core/version'},
|
||||
{text: '数据填充', link: '/zh/core/fill'},
|
||||
{text: '数据脱敏', link: '/zh/core/mask'},
|
||||
{text: '数据缓存', link: '/zh/core/data-cache'},
|
||||
{text: 'SQL 审计', link: '/zh/core/audit'},
|
||||
{text: 'SQL 打印', link: '/zh/core/sql-print'},
|
||||
{text: '多数据源', link: '/zh/core/multi-datasource'},
|
||||
|
||||
170
docs/zh/core/data-cache.md
Normal file
170
docs/zh/core/data-cache.md
Normal file
@ -0,0 +1,170 @@
|
||||
# 数据缓存
|
||||
|
||||
MyBatis-Flex 是一个 MyBatis 增强框架,所以您可以使用 MyBatis 提供的二级缓存来作为数据缓存。但是它仍然有很多的缺点,比如不适用于分布式环境,在这里推荐使用 [Spring Cache](https://docs.spring.io/spring-framework/docs/5.2.24.RELEASE/spring-framework-reference/integration.html#cache) 模块来处理数据缓存。
|
||||
|
||||
## 使用方法
|
||||
|
||||
因为要用到 Spring Cache 模块,所以您的项目必须要使用 Spring Framework 框架,这里以 Spring Boot 项目作为例子,实现 MyBatis-Flex 项目将缓存数据存入 Redis 组件中。
|
||||
|
||||
1、引入 `spring-boot-starter-cache` 和 `spring-boot-starter-data-redis`模块
|
||||
|
||||
```xml
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-cache</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
```
|
||||
|
||||
2、设置 Redis 连接信息(全是默认的话可以跳过这步)
|
||||
|
||||
```yaml
|
||||
spring:
|
||||
redis:
|
||||
port: 6379
|
||||
host: localhost
|
||||
```
|
||||
|
||||
3、在 Spring Boot 配置类上启用 Spring Cache 缓存
|
||||
|
||||
```java
|
||||
@EnableCaching
|
||||
@Configuration
|
||||
public class CacheConfig {
|
||||
}
|
||||
```
|
||||
|
||||
4、将 ServiceImpl 默认实现类换为 CacheableServiceImpl 实现类
|
||||
|
||||
```java
|
||||
public interface AccountService extends IService<Account> {
|
||||
}
|
||||
|
||||
@Service
|
||||
public class AccountServiceImpl extends CacheableServiceImpl<AccountMapper, Account> implements AccountService {
|
||||
}
|
||||
```
|
||||
|
||||
5、最后即可使用 Spring Cache 的相关注解实现数据缓存到 Redis 中了
|
||||
|
||||
```java
|
||||
// 设置统一的缓存名称
|
||||
@Service
|
||||
@CacheConfig(cacheNames = "account")
|
||||
public class AccountServiceImpl extends CacheableServiceImpl<AccountMapper, Account> implements AccountService {
|
||||
|
||||
// 根据主键缓存数据
|
||||
@Override
|
||||
@Cacheable(key = "#id")
|
||||
public Account getById(Serializable id) {
|
||||
return super.getById(id);
|
||||
}
|
||||
|
||||
// 根据方法名加查询 SQL 语句缓存结果数据
|
||||
// 加上方法名是为了避免不同的方法使用一样的 QueryWrapper
|
||||
@Override
|
||||
@Cacheable(key = "#root.methodName + ':' + #query.toDebugSQL()")
|
||||
public List<Account> list(QueryWrapper query) {
|
||||
return super.list(query);
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
## 使用说明
|
||||
|
||||
MyBatis-Flex 在 IService 接口中做了方法调用链优化,所以您只需将缓存注解加到一些特定的方法上,即可实现所有相关的方法也可以进行数据缓存。相关方法见如下示例:
|
||||
|
||||
```java
|
||||
@Service
|
||||
@CacheConfig(cacheNames = "account")
|
||||
public class AccountServiceImpl extends CacheableServiceImpl<MyAccountMapper, Account> {
|
||||
|
||||
@Override
|
||||
@CacheEvict(allEntries = true)
|
||||
public boolean remove(QueryWrapper query) {
|
||||
return super.remove(query);
|
||||
}
|
||||
|
||||
@Override
|
||||
@CacheEvict(key = "#id")
|
||||
public boolean removeById(Serializable id) {
|
||||
return super.removeById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
@CacheEvict(allEntries = true)
|
||||
public boolean removeByIds(Collection<? extends Serializable> ids) {
|
||||
return super.removeByIds(ids);
|
||||
}
|
||||
|
||||
@Override
|
||||
@CachePut(key = "#entity.id")
|
||||
public boolean update(Account entity, QueryWrapper query) {
|
||||
return super.update(entity, query);
|
||||
}
|
||||
|
||||
@Override
|
||||
@CachePut(key = "#entity.id")
|
||||
public boolean updateById(Account entity) {
|
||||
return super.updateById(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(key = "#id")
|
||||
public Account getById(Serializable id) {
|
||||
return super.getById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(key = "#root.methodName + ':' + #query.toDebugSQL()")
|
||||
public Account getOne(QueryWrapper query) {
|
||||
return super.getOne(query);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(key = "#root.methodName + ':' + #query.toDebugSQL()")
|
||||
public <R> R getOneAs(QueryWrapper query, Class<R> asType) {
|
||||
return super.getOneAs(query, asType);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(key = "#root.methodName + ':' + #query.toDebugSQL()")
|
||||
public List<Account> list(QueryWrapper query) {
|
||||
return super.list(query);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(key = "#root.methodName + ':' + #query.toDebugSQL()")
|
||||
public <R> List<R> listAs(QueryWrapper query, Class<R> asType) {
|
||||
return super.listAs(query, asType);
|
||||
}
|
||||
|
||||
// 无法通过注解进行缓存操作
|
||||
@Override
|
||||
@Deprecated
|
||||
public List<Account> listByIds(Collection<? extends Serializable> ids) {
|
||||
return super.listByIds(ids);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(key = "#root.methodName + ':' + #query.toDebugSQL()")
|
||||
public long count(QueryWrapper query) {
|
||||
return super.count(query);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(key = "#root.methodName + ':' + #query.toDebugSQL()")
|
||||
public Page<Account> page(Page<Account> page, QueryWrapper query) {
|
||||
return super.page(page, query);
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user