Skip to content

Commit db4014a

Browse files
Sort Clients for add (#132)
Co-authored-by: Sriram Rangarajan <[email protected]>
1 parent ff2e09b commit db4014a

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

evcache-core/src/main/java/com/netflix/evcache/EVCacheImpl.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.concurrent.TimeUnit;
2121
import java.util.concurrent.TimeoutException;
2222
import java.util.function.Function;
23+
import java.util.stream.Collectors;
2324

2425
import javax.management.MBeanServer;
2526
import javax.management.ObjectName;
@@ -3259,7 +3260,21 @@ public <T> boolean add(String key, T value, Transcoder<T> tc, int timeToLive) th
32593260
@Override
32603261
public <T> EVCacheLatch add(String key, T value, Transcoder<T> tc, int timeToLive, Policy policy) throws EVCacheException {
32613262
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);
32633278
}
32643279

32653280
protected <T> EVCacheLatch add(String key, T value, Transcoder<T> tc, int timeToLive, Policy policy, EVCacheClient[] clients, int latchCount) throws EVCacheException {

0 commit comments

Comments
 (0)