@@ -148,9 +148,13 @@ public String toString() {
148
148
*/
149
149
private class DestinationCache {
150
150
151
- /** Map from destination -> <sessionId, subscriptionId> */
151
+ /** Map from destination -> <sessionId, subscriptionId> for fast look-ups */
152
+ private final Map <String , MultiValueMap <String , String >> accessCache =
153
+ new ConcurrentHashMap <String , MultiValueMap <String , String >>(DEFAULT_CACHE_LIMIT );
154
+
155
+ /** Map from destination -> <sessionId, subscriptionId> with locking */
152
156
@ SuppressWarnings ("serial" )
153
- private final Map <String , MultiValueMap <String , String >> cache =
157
+ private final Map <String , MultiValueMap <String , String >> updateCache =
154
158
new LinkedHashMap <String , MultiValueMap <String , String >>(DEFAULT_CACHE_LIMIT , 0.75f , true ) {
155
159
@ Override
156
160
protected boolean removeEldestEntry (Map .Entry <String , MultiValueMap <String , String >> eldest ) {
@@ -159,57 +163,64 @@ protected boolean removeEldestEntry(Map.Entry<String, MultiValueMap<String, Stri
159
163
};
160
164
161
165
162
-
163
166
public MultiValueMap <String , String > getSubscriptions (String destination ) {
164
- synchronized (this .cache ) {
165
- return this .cache .get (destination );
166
- }
167
+ return this .accessCache .get (destination );
167
168
}
168
169
169
170
public void addSubscriptions (String destination , MultiValueMap <String , String > subscriptions ) {
170
- synchronized (this .cache ) {
171
- this .cache .put (destination , subscriptions );
171
+ synchronized (this .updateCache ) {
172
+ this .updateCache .put (destination , subscriptions );
173
+ this .accessCache .put (destination , new LinkedMultiValueMap <String , String >(subscriptions ));
172
174
}
173
175
}
174
176
175
177
public void updateAfterNewSubscription (String destination , String sessionId , String subsId ) {
176
- synchronized (this .cache ) {
177
- for (String cachedDestination : this .cache .keySet ()) {
178
+ synchronized (this .updateCache ) {
179
+ for (String cachedDestination : this .updateCache .keySet ()) {
178
180
if (getPathMatcher ().match (destination , cachedDestination )) {
179
- MultiValueMap <String , String > subscriptions = this .cache .get (cachedDestination );
180
- subscriptions .add (sessionId , subsId );
181
+ MultiValueMap <String , String > subs = this .updateCache .get (cachedDestination );
182
+ subs .add (sessionId , subsId );
183
+ this .accessCache .put (cachedDestination , new LinkedMultiValueMap <String , String >(subs ));
181
184
}
182
185
}
183
186
}
184
187
}
185
188
186
189
public void updateAfterRemovedSubscription (String destination , String sessionId , String subsId ) {
187
- synchronized (this .cache ) {
188
- for (String cachedDestination : this .cache .keySet ()) {
190
+ synchronized (this .updateCache ) {
191
+ for (String cachedDestination : this .updateCache .keySet ()) {
189
192
if (getPathMatcher ().match (destination , cachedDestination )) {
190
- MultiValueMap <String , String > subscriptions = this .cache .get (cachedDestination );
191
- List <String > subsIds = subscriptions .get (sessionId );
193
+ MultiValueMap <String , String > subs = this .updateCache .get (cachedDestination );
194
+ List <String > subsIds = subs .get (sessionId );
192
195
subsIds .remove (subsId );
193
196
if (subsIds .isEmpty ()) {
194
- subscriptions .remove (sessionId );
197
+ subs .remove (sessionId );
195
198
}
196
- if (subscriptions .isEmpty ()) {
197
- this .cache .remove (cachedDestination );
199
+ if (subs .isEmpty ()) {
200
+ this .updateCache .remove (cachedDestination );
201
+ this .accessCache .remove (cachedDestination );
202
+ }
203
+ else {
204
+ this .accessCache .put (cachedDestination , new LinkedMultiValueMap <String , String >(subs ));
198
205
}
199
206
}
200
207
}
201
208
}
202
209
}
203
210
204
211
public void updateAfterRemovedSession (SessionSubscriptionInfo info ) {
205
- synchronized (this .cache ) {
212
+ synchronized (this .updateCache ) {
206
213
for (String destination : info .getDestinations ()) {
207
- for (String cachedDestination : this .cache .keySet ()) {
214
+ for (String cachedDestination : this .updateCache .keySet ()) {
208
215
if (getPathMatcher ().match (destination , cachedDestination )) {
209
- MultiValueMap <String , String > map = this .cache .get (cachedDestination );
210
- map .remove (info .getSessionId ());
211
- if (map .isEmpty ()) {
212
- this .cache .remove (cachedDestination );
216
+ MultiValueMap <String , String > subs = this .updateCache .get (cachedDestination );
217
+ subs .remove (info .getSessionId ());
218
+ if (subs .isEmpty ()) {
219
+ this .updateCache .remove (cachedDestination );
220
+ this .accessCache .remove (cachedDestination );
221
+ }
222
+ else {
223
+ this .accessCache .put (cachedDestination ,new LinkedMultiValueMap <String , String >(subs ));
213
224
}
214
225
}
215
226
}
@@ -219,7 +230,7 @@ public void updateAfterRemovedSession(SessionSubscriptionInfo info) {
219
230
220
231
@ Override
221
232
public String toString () {
222
- return "[cache=" + this .cache + "]" ;
233
+ return "[cache=" + this .accessCache + "]" ;
223
234
}
224
235
}
225
236
0 commit comments