|
20 | 20 | import java.util.concurrent.TimeUnit;
|
21 | 21 | import java.util.concurrent.TimeoutException;
|
22 | 22 | import java.util.function.Function;
|
| 23 | +import java.util.stream.Collectors; |
23 | 24 |
|
24 | 25 | import javax.management.MBeanServer;
|
25 | 26 | import javax.management.ObjectName;
|
@@ -3259,7 +3260,21 @@ public <T> boolean add(String key, T value, Transcoder<T> tc, int timeToLive) th
|
3259 | 3260 | @Override
|
3260 | 3261 | public <T> EVCacheLatch add(String key, T value, Transcoder<T> tc, int timeToLive, Policy policy) throws EVCacheException {
|
3261 | 3262 | EVCacheClient[] clients = _pool.getEVCacheClientForWrite();
|
3262 |
| - return this.add(key, value, tc, timeToLive, policy, clients, clients.length - _pool.getWriteOnlyEVCacheClients().length); |
| 3263 | + EVCacheClient[] writeOnlyClients = _pool.getWriteOnlyEVCacheClients(); |
| 3264 | + // In case of adds , we skip adds to the pool if value is already present in the 1st client |
| 3265 | + // Sorting to make sure the 1st element of the list is a read/write client and not just write-only client |
| 3266 | + EVCacheClient[] sortedClients = sortClients(clients, writeOnlyClients); |
| 3267 | + return this.add(key, value, tc, timeToLive, policy, sortedClients, clients.length - _pool.getWriteOnlyEVCacheClients().length); |
| 3268 | + } |
| 3269 | + |
| 3270 | + public EVCacheClient[] sortClients(EVCacheClient[] clients, EVCacheClient[] writeOnlyClients) { |
| 3271 | + List<EVCacheClient> writeOnlyClientsList = Arrays.asList(writeOnlyClients); |
| 3272 | + List<EVCacheClient> clientList = Arrays.stream(clients).sorted((s1, s2) -> { |
| 3273 | + if (writeOnlyClientsList.contains(s1)) |
| 3274 | + return 1; |
| 3275 | + return -1; |
| 3276 | + }).collect(Collectors.toList()); |
| 3277 | + return clientList.stream().toArray(EVCacheClient[]::new); |
3263 | 3278 | }
|
3264 | 3279 |
|
3265 | 3280 | protected <T> EVCacheLatch add(String key, T value, Transcoder<T> tc, int timeToLive, Policy policy, EVCacheClient[] clients, int latchCount) throws EVCacheException {
|
|
0 commit comments