/
CollectionCacheableTestRepository.java
119 lines (96 loc) · 4.79 KB
/
CollectionCacheableTestRepository.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package de.qaware.tools.collectioncacheableforspring;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@CacheConfig(cacheNames = CollectionCacheableTestRepository.CACHE_NAME)
public class CollectionCacheableTestRepository {
public static final String CACHE_NAME = "myCache";
private final CollectionCacheableTestDbRepository myDbRepository;
public CollectionCacheableTestRepository(CollectionCacheableTestDbRepository myDbRepository) {
this.myDbRepository = myDbRepository;
}
@Cacheable
public CollectionCacheableTestValue findById(CollectionCacheableTestId id) {
return myDbRepository.findById(id);
}
@Cacheable(cacheNames = CACHE_NAME, key = "#id.id")
public CollectionCacheableTestValue findByIdWithKey(CollectionCacheableTestId id) {
return myDbRepository.findById(id);
}
@CollectionCacheable(CACHE_NAME)
public Map<CollectionCacheableTestId, CollectionCacheableTestValue> findByIds(Collection<CollectionCacheableTestId> ids) {
return findByIdsInternal(ids);
}
@CollectionCacheable(CACHE_NAME)
public Map<CollectionCacheableTestId, CollectionCacheableTestValue> findByIdsSet(Set<CollectionCacheableTestId> ids) {
return findByIdsInternal(ids);
}
@CollectionCacheable(CACHE_NAME)
public Map<CollectionCacheableTestId, CollectionCacheableTestValue> findByIdsList(List<CollectionCacheableTestId> ids) {
return findByIdsInternal(ids);
}
@CollectionCacheable(CACHE_NAME)
public Map<CollectionCacheableTestId, CollectionCacheableTestValue> findByIdsArrayList(ArrayList<CollectionCacheableTestId> ids) {
return findByIdsInternal(ids);
}
@CollectionCacheable(CACHE_NAME)
public List<CollectionCacheableTestEntity> findByIdsListOfValues(Collection<CollectionCacheableTestId> ids) {
return mapToEntities(findByIdsInternal(ids));
}
private List<CollectionCacheableTestEntity> mapToEntities(Map<CollectionCacheableTestId, CollectionCacheableTestValue> map) {
return map.entrySet().stream()
.map(entry -> new CollectionCacheableTestEntity(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
}
@CollectionCacheable(condition = "#ids.size() < 3")
public Map<CollectionCacheableTestId, CollectionCacheableTestValue> findByIdsWithCondition(Collection<CollectionCacheableTestId> ids) {
return findByIdsInternal(ids);
}
@CollectionCacheable(unless = "#result.size() > 1")
public Map<CollectionCacheableTestId, CollectionCacheableTestValue> findByIdsWithUnless(Collection<CollectionCacheableTestId> ids) {
return findByIdsInternal(ids);
}
@CollectionCacheable(cacheNames = CACHE_NAME, key = "#p0.id")
public Map<CollectionCacheableTestId, CollectionCacheableTestValue> findByIdsWithKey(Collection<CollectionCacheableTestId> ids) {
return findByIdsInternal(ids);
}
@CollectionCacheable(putNull = true)
public Map<CollectionCacheableTestId, CollectionCacheableTestValue> findByIdsWithPutNull(Collection<CollectionCacheableTestId> ids) {
return findByIdsInternal(ids);
}
@CollectionCacheable(CACHE_NAME)
public Map<CollectionCacheableTestId, CollectionCacheableTestValue> findAll() {
return myDbRepository.findAll();
}
@CollectionCacheable(cacheNames = CACHE_NAME, unless = "#result.size() > 1")
public Map<CollectionCacheableTestId, CollectionCacheableTestValue> findAllWithUnless() {
return myDbRepository.findAll();
}
@CollectionCacheable(cacheNames = CACHE_NAME, key = "#result.id")
public Map<CollectionCacheableTestId, CollectionCacheableTestValue> findAllWithKey() {
return myDbRepository.findAll();
}
@CollectionCacheable(CACHE_NAME)
public List<CollectionCacheableTestEntity> findAllListOfValues() {
return mapToEntities(myDbRepository.findAll());
}
private Map<CollectionCacheableTestId, CollectionCacheableTestValue> findByIdsInternal(Collection<CollectionCacheableTestId> ids) {
// just a "simulation" of an efficient findByIds call to the underlying persistence layer
// in real use cases, this should be some efficient query!
Map<CollectionCacheableTestId, CollectionCacheableTestValue> result = new HashMap<>();
for (CollectionCacheableTestId id : ids) {
CollectionCacheableTestValue value = myDbRepository.findById(id);
// do not explicitly put null values into map
if (value != null) {
result.put(id, value);
}
}
return result;
}
}