@@ -43,7 +43,7 @@ public class SimpleCache<T> implements SnapshotCache<T> {
43
43
44
44
@ GuardedBy ("lock" )
45
45
private final Map <T , Snapshot > snapshots = new HashMap <>();
46
- private final ConcurrentMap <T , CacheStatusInfo <T >> statuses = new ConcurrentHashMap <>();
46
+ private final ConcurrentMap <T , ConcurrentMap < String , CacheStatusInfo <T > >> statuses = new ConcurrentHashMap <>();
47
47
48
48
private AtomicLong watchCount = new AtomicLong ();
49
49
@@ -64,10 +64,10 @@ public boolean clearSnapshot(T group) {
64
64
// we take a writeLock to prevent watches from being created
65
65
writeLock .lock ();
66
66
try {
67
- CacheStatusInfo <T > status = statuses .get (group );
67
+ Map < String , CacheStatusInfo <T > > status = statuses .get (group );
68
68
69
69
// If we don't know about this group, do nothing.
70
- if (status != null && status .numWatches () > 0 ) {
70
+ if (status != null && status .values (). stream (). mapToLong ( CacheStatusInfo :: numWatches ). sum () > 0 ) {
71
71
LOGGER .warn ("tried to clear snapshot for group with existing watches, group={}" , group );
72
72
73
73
return false ;
@@ -106,7 +106,8 @@ public Watch createWatch(
106
106
// doesn't conflict
107
107
readLock .lock ();
108
108
try {
109
- CacheStatusInfo <T > status = statuses .computeIfAbsent (group , g -> new CacheStatusInfo <>(group ));
109
+ CacheStatusInfo <T > status = statuses .computeIfAbsent (group , g -> new ConcurrentHashMap <>())
110
+ .computeIfAbsent (request .getTypeUrl (), s -> new CacheStatusInfo <>(group ));
110
111
status .setLastWatchRequestTime (System .currentTimeMillis ());
111
112
112
113
Snapshot snapshot = snapshots .get (group );
@@ -212,7 +213,7 @@ public Collection<T> groups() {
212
213
@ Override
213
214
public synchronized void setSnapshot (T group , Snapshot snapshot ) {
214
215
// we take a writeLock to prevent watches from being created while we update the snapshot
215
- CacheStatusInfo <T > status ;
216
+ ConcurrentMap < String , CacheStatusInfo <T > > status ;
216
217
writeLock .lock ();
217
218
try {
218
219
// Update the existing snapshot entry.
@@ -238,15 +239,26 @@ public StatusInfo statusInfo(T group) {
238
239
readLock .lock ();
239
240
240
241
try {
241
- return statuses .get (group );
242
+ ConcurrentMap <String , CacheStatusInfo <T >> statusMap = statuses .get (group );
243
+ if (statusMap == null || statusMap .isEmpty ()) {
244
+ return null ;
245
+ }
246
+
247
+ return new GroupCacheStatusInfo <>(statusMap .values ());
242
248
} finally {
243
249
readLock .unlock ();
244
250
}
245
251
}
246
252
247
253
@ VisibleForTesting
248
- protected void respondWithSpecificOrder (T group , Snapshot snapshot , CacheStatusInfo <T > status ) {
254
+ protected void respondWithSpecificOrder (T group , Snapshot snapshot ,
255
+ ConcurrentMap <String , CacheStatusInfo <T >> statusMap ) {
249
256
for (String typeUrl : Resources .TYPE_URLS ) {
257
+ CacheStatusInfo <T > status = statusMap .get (typeUrl );
258
+ if (status == null ) {
259
+ continue ;
260
+ }
261
+
250
262
status .watchesRemoveIf ((id , watch ) -> {
251
263
if (!watch .request ().getTypeUrl ().equals (typeUrl )) {
252
264
return false ;
0 commit comments