Skip to content

Commit

Permalink
Sort Clients for add (#132)
Browse files Browse the repository at this point in the history
Co-authored-by: Sriram Rangarajan <[email protected]>
  • Loading branch information
sriram-rangarajan and srrangarajan authored Jul 26, 2023
1 parent ff2e09b commit db4014a
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion evcache-core/src/main/java/com/netflix/evcache/EVCacheImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Function;
import java.util.stream.Collectors;

import javax.management.MBeanServer;
import javax.management.ObjectName;
Expand Down Expand Up @@ -3259,7 +3260,21 @@ public <T> boolean add(String key, T value, Transcoder<T> tc, int timeToLive) th
@Override
public <T> EVCacheLatch add(String key, T value, Transcoder<T> tc, int timeToLive, Policy policy) throws EVCacheException {
EVCacheClient[] clients = _pool.getEVCacheClientForWrite();
return this.add(key, value, tc, timeToLive, policy, clients, clients.length - _pool.getWriteOnlyEVCacheClients().length);
EVCacheClient[] writeOnlyClients = _pool.getWriteOnlyEVCacheClients();
// In case of adds , we skip adds to the pool if value is already present in the 1st client
// Sorting to make sure the 1st element of the list is a read/write client and not just write-only client
EVCacheClient[] sortedClients = sortClients(clients, writeOnlyClients);
return this.add(key, value, tc, timeToLive, policy, sortedClients, clients.length - _pool.getWriteOnlyEVCacheClients().length);
}

public EVCacheClient[] sortClients(EVCacheClient[] clients, EVCacheClient[] writeOnlyClients) {
List<EVCacheClient> writeOnlyClientsList = Arrays.asList(writeOnlyClients);
List<EVCacheClient> clientList = Arrays.stream(clients).sorted((s1, s2) -> {
if (writeOnlyClientsList.contains(s1))
return 1;
return -1;
}).collect(Collectors.toList());
return clientList.stream().toArray(EVCacheClient[]::new);
}

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

0 comments on commit db4014a

Please sign in to comment.