|
| 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 | + ``` |
0 commit comments