Skip to content

Commit 8d7e99f

Browse files
committed
增加 asElementField
1 parent b9b8e9d commit 8d7e99f

File tree

16 files changed

+570
-373
lines changed

16 files changed

+570
-373
lines changed

.github/workflows/maven-publish.yml

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
name: Maven Package
55

66
on:
7-
create:
8-
branches:
9-
- tag/*
7+
release:
8+
types: [ created ]
109

1110
jobs:
1211
build:
@@ -17,18 +16,18 @@ jobs:
1716

1817
steps:
1918
- uses: actions/checkout@v3
20-
- name: Set up JDK 8
21-
uses: actions/setup-java@v3
22-
with:
23-
java-version: '1.8'
24-
distribution: 'temurin'
25-
server-id: ossrh # Value of the distributionManagement/repository/id field of the pom.xml
26-
settings-path: ${{ github.workspace }} # location for the settings.xml file
19+
- name: Set up JDK 8
20+
uses: actions/setup-java@v3
21+
with:
22+
java-version: '1.8'
23+
distribution: 'temurin'
24+
server-id: ossrh # Value of the distributionManagement/repository/id field of the pom.xml
25+
settings-path: ${{ github.workspace }} # location for the settings.xml file
2726

28-
- name: Build with Maven
29-
run: mvn -B package -pl cache-as-multi
27+
- name: Build with Maven
28+
run: mvn -B package -pl cache-as-multi
3029

31-
- name: Publish to GitHub Packages Apache Maven
32-
run: mvn deploy -s $GITHUB_WORKSPACE/settings.xml -pl cache-as-multi
33-
env:
34-
GITHUB_TOKEN: ${{ github.token }}
30+
- name: Publish to GitHub Packages Apache Maven
31+
run: mvn deploy -s $GITHUB_WORKSPACE/settings.xml -pl cache-as-multi
32+
env:
33+
GITHUB_TOKEN: ${{ github.token }}

MORE-EXAMPLES-EN.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
## More Examples of Cacheable and CacheResult
2+
3+
When a method has multiple parameters, here are some examples:
4+
5+
When a method has multiple parameters, the key generation for caching may vary based on the configuration
6+
of `@Cacheable.key()` or the presence of `@CacheKey` annotations on the method parameters.
7+
8+
Here are some examples:
9+
10+
* When `@Cacheable.key()` is not configured or when `@CacheResult` is used and there is no `@CacheKey` on the method
11+
parameters:
12+
```java
13+
class FooService {
14+
@Cacheable(cacheNames = "foo", key="")
15+
@CacheResult(cacheName = "foo")
16+
public Foo getFoo(Integer fooId, String arg1) {
17+
// generate cache key using fooId and arg1 parameters, use return value as cache value
18+
}
19+
20+
@Cacheable(cacheNames = "foo", key="")
21+
@CacheResult(cacheName = "foo")
22+
public Map<Integer, Foo> getMultiFoo(@CacheAsMulti Collection<Integer> fooIds, String arg1) {
23+
// generate cache key for each element in fooIds parameter using arg1 parameter, use corresponding value from return Map as cache value
24+
}
25+
}
26+
```
27+
28+
* When `@Cacheable.key()` only refers to the `@CacheKey` annotated parameter of an object parameter or when `@CacheKey`
29+
is used only on the object parameter:
30+
```java
31+
class FooService {
32+
@Cacheable(cacheNames = "foo", key="#fooId")
33+
@CacheResult(cacheName = "foo")
34+
public Foo getFoo(@CacheKey Integer fooId, String arg1) {
35+
// generate cache key using fooId parameter, use return value as cache value
36+
}
37+
38+
@Cacheable(cacheNames = "foo", key="#fooIds")
39+
@CacheResult(cacheName = "foo")
40+
public Map<Integer, Foo> getMultiFoo(@CacheAsMulti @CacheKey Collection<Integer> fooIds, String arg1) {
41+
// generate cache key for each element in fooIds parameter, use corresponding value from return Map as cache value
42+
}
43+
}
44+
```
45+
46+
* When `@Cacheable.key()` refers to multiple parameters or when there are multiple `@CacheKey` annotations on the method
47+
parameters:
48+
```java
49+
class FooService {
50+
@Cacheable(cacheNames = "foo", key="#fooId+#arg1")
51+
@CacheResult(cacheName = "foo")
52+
public Foo getFoo(@CacheKey Integer fooId, @CacheKey String arg1, Float arg2) {
53+
// generate cache key using fooId and arg1 parameters, use return value as cache value
54+
}
55+
56+
@Cacheable(cacheNames = "foo", key="#fooIds+#arg1")
57+
@CacheResult(cacheName = "foo")
58+
public Map<Integer, Foo> getMultiFoo(@CacheAsMulti @CacheKey Collection<Integer> fooIds, @CacheKey String arg1, Float arg2) {
59+
// generate cache key for each element in fooIds parameter using arg1 parameter, use corresponding value from return Map as cache value
60+
// Note that the object collection parameter needs to have a @CacheKey annotation and needs to be included in Cacheable.key()
61+
}
62+
}
63+
```
64+
65+
## More Examples of @CachePut in JSR-107
66+
67+
* Multiple parameters as key, without configuring `@CacheKey`:
68+
```java
69+
class FooService {
70+
@CachePut(cacheName = "foo")
71+
public void putFoo(Integer fooId, String arg1, @CacheValue String value) {
72+
// Generate the cache key using the fooId and arg1 parameters, and use value as the cache value
73+
}
74+
75+
@CachePut(cacheName = "foo")
76+
public void putMultiFoo(@CacheAsMulti @CacheValue Map<Integer,String> fooIdValueMap, String arg1) {
77+
// In this case, the @CacheValue parameter of the method must be of type Map
78+
// Generate a cache key using each Entry key in fooIdValueMap along with the arg1 parameter, and use Entry value as the cache value
79+
}
80+
}
81+
```
82+
83+
* Only the `@CacheKey` annotation is present on the object parameter:
84+
```java
85+
class FooService {
86+
@CachePut(cacheName = "foo")
87+
public void putFoo(@CacheKey Integer fooId, String arg1, @CacheValue String value) {
88+
// Generate the cache key using the fooId parameter, and use value as the cache value
89+
}
90+
91+
@CachePut(cacheName = "foo")
92+
public void putMultiFoo(@CacheAsMulti @CacheKey @CacheValue Map<Integer,String> fooIdValueMap, String arg1) {
93+
// In this case, the @CacheValue parameter of the method must be of type Map
94+
// Generate a cache key using each Entry key in fooIdValueMap, and use Entry value as the cache value
95+
}
96+
}
97+
```
98+
99+
* If there are `@CacheKey` annotations on multiple parameters:
100+
```java
101+
class FooService {
102+
@CachePut(cacheName = "foo")
103+
public void putFoo(@CacheKey Integer fooId, @CacheKey String arg1, String arg2, @CacheValue String value) {
104+
// Generates a cache key using fooId and arg1 parameters, and uses value as the cache value
105+
}
106+
107+
@CachePut(cacheName = "foo")
108+
public void putMultiFoo(@CacheAsMulti @CacheKey @CacheValue Map<Integer,String> fooIdValueMap, @CacheKey String arg1, String arg2) {
109+
// The @CacheValue parameter of this method must be of type Map
110+
// Generates a cache key using each key of the entries in fooIdValueMap and arg1 parameter, and uses the value of the entry as the cache value
111+
}
112+
}
113+
```

MORE-EXAMPLES.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
## Cacheable 和 CacheResult 的更多示例
2+
3+
当方法有多个参数时,缓存的 key 可能因 `@Cacheable.key` 的配置或方法参数上的`@CacheKey` 注释而异。
4+
5+
示例如下:
6+
7+
* 使用 `@Cacheable``@Cacheable.key()` 未配置【或】使用 `@CacheResult` 时参数中没有 `@CacheKey`
8+
```java
9+
class FooService {
10+
@Cacheable(cacheNames = "foo", key="")
11+
@CacheResult(cacheName = "foo")
12+
public Foo getFoo(Integer fooId, String arg1) {
13+
// 用 fooId 和 arg1 两个参数生成缓存的 key,用返回值作为缓存值
14+
}
15+
16+
@Cacheable(cacheNames = "foo", key="")
17+
@CacheResult(cacheName = "foo")
18+
public Map<Integer, Foo> getMultiFoo(@CacheAsMulti Collection<Integer> fooIds, String arg1) {
19+
// 用 fooIds 中的每个【元素】分别和 arg1 参数生成缓存的 key,用返回 Map 中【元素】对应的值作为缓存值
20+
}
21+
}
22+
```
23+
24+
* 使用 `@Cacheable``@Cacheable.key()` 只配置了【对象参数】的引用【或】使用 `@CacheResult`
25+
时只有【对象参数】上有 `@CacheKey`
26+
```java
27+
class FooService {
28+
@Cacheable(cacheNames = "foo", key="#fooId")
29+
@CacheResult(cacheName = "foo")
30+
public Foo getFoo(@CacheKey Integer fooId, String arg1) {
31+
// 用 fooId 生成缓存的 key,用返回值作为缓存值
32+
}
33+
34+
@Cacheable(cacheNames = "foo", key="#fooIds")
35+
@CacheResult(cacheName = "foo")
36+
public Map<Integer, Foo> getMultiFoo(@CacheAsMulti @CacheKey Collection<Integer> fooIds, String arg1) {
37+
// 用 fooIds 中的每个【元素】分别生成缓存的 key,用返回 Map 中【元素】对应的值作为缓存值
38+
}
39+
}
40+
```
41+
42+
* 使用 `@Cacheable``@Cacheable.key()` 配置了若干参数的引用【或】使用 `@CacheResult` 时参数中有若干 `@CacheKey`
43+
```java
44+
class FooService {
45+
@Cacheable(cacheNames = "foo", key="#fooId+#arg1")
46+
@CacheResult(cacheName = "foo")
47+
public Foo getFoo(@CacheKey Integer fooId, @CacheKey String arg1, Float arg2) {
48+
// 用 fooId 和 arg1 两个参数生成缓存的 key,用返回值作为缓存值
49+
}
50+
51+
@Cacheable(cacheNames = "foo", key="#fooIds+#arg1")
52+
@CacheResult(cacheName = "foo")
53+
public Map<Integer, Foo> getMultiFoo(@CacheAsMulti @CacheKey Collection<Integer> fooIds, @CacheKey String arg1, Float arg2) {
54+
// 用 fooIds 中的每个【元素】分别和 arg1 参数生成缓存的 key,用返回 Map 中【元素】对应的值作为缓存值
55+
// 注意此时【对象集合参数】需要在 Cacheable.key() 中,需要有 @CacheKey 注解
56+
}
57+
}
58+
```
59+
60+
## JSR-107 的 @CachePut 的更多示例
61+
62+
* 多个参数做key,未配置 `@CacheKey`
63+
```java
64+
class FooService {
65+
@CachePut(cacheName = "foo")
66+
public void putFoo(Integer fooId, String arg1, @CacheValue String value) {
67+
// 用 fooId 和 arg1 两个参数生成缓存的 key,用 value 作为缓存值
68+
}
69+
70+
@CachePut(cacheName = "foo")
71+
public void putMultiFoo(@CacheAsMulti @CacheValue Map<Integer,String> fooIdValueMap, String arg1) {
72+
// 此时方法的 @CacheValue 参数必须为 Map 类型
73+
// 用 fooIdValueMap 中的每个 Entry 的 key 分别和 arg1 参数生成缓存的 key,用 Entry 的 value 作为缓存值
74+
}
75+
}
76+
```
77+
78+
* 只有【对象参数】上有 `@CacheKey`
79+
```java
80+
class FooService {
81+
@CachePut(cacheName = "foo")
82+
public void putFoo(@CacheKey Integer fooId, String arg1, @CacheValue String value) {
83+
// 用 fooId 参数生成缓存的 key,用 value 作为缓存值
84+
}
85+
86+
@CachePut(cacheName = "foo")
87+
public void putMultiFoo(@CacheAsMulti @CacheKey @CacheValue Map<Integer,String> fooIdValueMap, String arg1) {
88+
// 此时方法的 @CacheValue 参数必须为 Map 类型
89+
// 用 fooIdValueMap 中的每个 Entry 的 key 分别生成缓存的 key,用 Entry 的 value 作为缓存值
90+
}
91+
}
92+
```
93+
94+
* 若干参数上有 `@CacheKey`
95+
```java
96+
class FooService {
97+
@CachePut(cacheName = "foo")
98+
public void putFoo(@CacheKey Integer fooId, @CacheKey String arg1, String arg2, @CacheValue String value) {
99+
// 用 fooId 和 arg1 两个参数生成缓存的 key,用 value 作为缓存值
100+
}
101+
102+
@CachePut(cacheName = "foo")
103+
public void putMultiFoo(@CacheAsMulti @CacheKey @CacheValue Map<Integer,String> fooIdValueMap, @CacheKey String arg1, String arg2) {
104+
// 此时方法的 @CacheValue 参数必须为 Map 类型
105+
// 用 fooIdValueMap 中的每个 Entry 的 key 分别和 arg1 参数生成缓存的 key,用 Entry 的 value 作为缓存值
106+
}
107+
}
108+
```

0 commit comments

Comments
 (0)