Skip to content

Commit dd654ea

Browse files
committed
更新ApiBoot Resource Load 文档
1 parent 89a9f9f commit dd654ea

File tree

1 file changed

+144
-1
lines changed
  • api-boot-samples/api-boot-sample-resource-load

1 file changed

+144
-1
lines changed

api-boot-samples/api-boot-sample-resource-load/README.md

Lines changed: 144 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,149 @@ public Map<String, SampleUserInfo> mapSample() {
164164

165165
`Map`类型作为返回值时,其中注意`map -> value`必须是对象类型。
166166

167+
### 内存方式缓存资源
167168

169+
`ApiBoot Resource Load`提供了内存缓存的支持,相同类型、相同业务逻辑的资源数据只会从数据库内读取一次,除非执行资源的删除、更新,否则只能等到下次重启项目时进行更新。
170+
171+
### Redis方式缓存资源
172+
173+
`ApiBoot Resource Load`支持使用`redis`进行自动缓存资源数据,尽可能减轻数据库的读取压力。
174+
175+
#### 添加redis集成
176+
177+
使用`spring-boot-starter-data-redis`依赖来完成集成`redis`,在`pom.xml`添加依赖如下所示:
178+
179+
```xml
180+
<!--Spring Boot Redis Starter-->
181+
<dependency>
182+
<groupId>org.springframework.boot</groupId>
183+
<artifactId>spring-boot-starter-data-redis</artifactId>
184+
</dependency>
185+
```
186+
187+
添加依赖后需要进行相应的配置,在`application.yml`配置文件内,如下所示:
188+
189+
```yaml
190+
spring:
191+
redis:
192+
password: 123456
193+
```
194+
195+
在上面仅仅配置了连接密码,`spring-boot-starter-data-redis`配置文件提供了大多数的默认配置,可自行修改。
196+
197+
198+
199+
> `ApiBoot Resource Load`自动通过`RedisTemplate`来完成资源缓存,无需额外配置
200+
201+
### 表达式使用
202+
203+
`@ResourceField`注解的`name`、`source`属性都支持表达式使用,如下示例:
204+
205+
```java
206+
@ResourceLoad(event = ResourceStoreEvent.INSERT)
207+
@ResourceFields({
208+
@ResourceField(name = "#p2", source = "#p1.userId", type = ResourceConstant.SHORT_IMAGE),
209+
@ResourceField(name = "#p3", source = "#p0", type = ResourceConstant.HEAD_IMAGE)
210+
})
211+
public void insertResource(String userId, UserInfo userInfo, List<String> shortImage, String headImage) {
212+
//...
213+
}
214+
```
215+
216+
在上面代码中,`#p`是正则表达式所匹配的规则,`#p0`为第一个参数,`#p1`则为第二个参数,以此类推。
217+
218+
如果参数是对象类型,可以通过`#p1.userId`来指定`source`对应业务编号的字段。
219+
220+
`#p1.userId`则对应参数`userInfo`对象内的`userId`字段。
221+
222+
### 自动添加资源
223+
224+
配置资源的自动添加,是通过方法的参数值来进行实现,如下所示:
225+
226+
```java
227+
@ResourceLoad(event = ResourceStoreEvent.INSERT)
228+
@ResourceFields({
229+
@ResourceField(name = "#p1", source = "#p0.userId", type = ResourceConstant.SHORT_IMAGE),
230+
@ResourceField(name = "#p2", source = "#p0.userId", type = ResourceConstant.HEAD_IMAGE)
231+
})
232+
public void insertUser(UserInfo userInfo, List<String> shortImage, String headImage) {
233+
//..
234+
}
235+
```
236+
237+
在上面添加资源示例中,要注意,`@ResourceLoad`的`event`属性需要修改为`ResourceStoreEvent.INSERT`。
238+
239+
```java
240+
@ResourceField(name = "#p2", source = "#p1.userId", type = ResourceConstant.SHORT_IMAGE)
241+
```
242+
243+
- `name`:配置使用第二个参数作为`SHORT_IMAGE`类型的资源列表。
244+
- `source`:注意`#p`索引是从`0`开始,所以这里`#p1`是第二个参数,`#p0.userId`配置使用第一个参数的`userId`作为业务逻辑编号字段。
245+
- `type`:常量,自定义
246+
247+
> 解释:
248+
>
249+
> 当调用配置以上注解的方法时,会自动调用`ApiBootResourceStoreDelegate#addResource`方法完成资源的添加,在调用之前,需要从`#p0.userId`标注的参数对象中拿到`userId`的值作为资源编号,然后拿到`#p1`标注的参数值作为资源列表,最终拿到`type`的值一并传递给`ApiBootResourceStoreDelegate#addResource`方法,做自行保存处理。
250+
251+
```java
252+
@ResourceField(name = "#p2", source = "#p0.userId", type = ResourceConstant.HEAD_IMAGE)
253+
```
254+
255+
> 解释:
256+
>
257+
> 调用方法之前,需要拿到`#p0.userId`第一个参数作为业务逻辑编号,然后拿到`#p2`第三个参数作为资源列表,最后拿到`type`的值一并传递给`ApiBootResourceStoreDelegate#addResource`方法。
258+
259+
260+
261+
注意:`insertResource`方法配置了两个`@ResourceField`所以在执行时,会调用两次`ApiBootResourceStoreDelegate#addResource`方法。
262+
263+
### 自动更新资源
264+
265+
配置资源的自动更新,同样是通过方法的参数值来进行实现,如下所示:
266+
267+
```java
268+
@ResourceLoad(event = ResourceStoreEvent.UPDATE)
269+
@ResourceFields({
270+
@ResourceField(name = "#p1", source = "#p0", type = ResourceConstant.SHORT_IMAGE)
271+
})
272+
public void updateUserImage(String userId, List<String> shortImage) {
273+
//...
274+
}
275+
```
276+
277+
在上面示例中,配置`@ResourceField`注解则会完成,类型为`SHORT_IMAGE`且业务逻辑编号为第一个参数值的资源更新,而更新的资源列表则是第二个参数,也就是List集合。
278+
279+
280+
281+
具体解释与自动添加资源一致。
282+
283+
### 自动删除资源
284+
285+
配置资源的自动删除,同样是通过方法的参数值来进行实现,如下所示:
286+
287+
```java
288+
@ResourceLoad(event = ResourceStoreEvent.DELETE)
289+
@ResourceFields({
290+
@ResourceField(name = "shortImage", source = "#p0", type = ResourceConstant.SHORT_IMAGE),
291+
@ResourceField(name = "headImage", source = "#p0", type = ResourceConstant.HEAD_IMAGE)
292+
})
293+
public void deleteUser(String userId) {
294+
//删除用户逻辑
295+
}
296+
```
297+
298+
在上面代码中,删除用户时,会自动删除`userId`业务编号下的`SHORT_IMAGE`、`HEAD_IMAGE`等资源列表。
299+
300+
301+
302+
### 资源字段是List或者Array?
303+
304+
如果查询资源时,返回值对象接收资源的字段为List或者Array,可以通过`@ResourceField`字段的属性来配置,如下所示:
305+
306+
```java
307+
// isList
308+
@ResourceField(name = "shortImage", source = "userId", type = ResourceConstant.SHORT_IMAGE, isList = true)
309+
// isArray
310+
@ResourceField(name = "shortImage", source = "userId", type = ResourceConstant.SHORT_IMAGE, isArray = true)
311+
```
168312

169-
> 如果你有想要的使用方式,你就可以提交issuse!!!

0 commit comments

Comments
 (0)