MemcacheClientFactoryImpl implements CacheClientFactory { @Override public CacheClient create(final List<InetSocketAddress> addrs, final CacheConfiguration conf) throws IOException { XMemcachedClientBuilder builder = null; if (conf instanceof XMemcachedConfiguration) { int[] weights = ((XMemcachedConfiguration) conf).getWeights(); if (weights != null && weights.length > 0) { builder = new XMemcachedClientBuilder(addrs, weights); } } if (builder == null) { builder = new XMemcachedClientBuilder(addrs); } if (conf.isConsistentHashing()) { builder.setSessionLocator(new KetamaMemcachedSessionLocator()); } if (conf.isUseBinaryProtocol()) { builder.setCommandFactory(new BinaryCommandFactory()); } builder.setTranscoder(new SerializingTranscoder(CachedObject.MAX_SIZE)); if (conf instanceof XMemcachedConfiguration) { setProviderBuilderSpecificSettings(builder, (XMemcachedConfiguration) conf); } MemcachedClient client = builder.build(); if (conf.getOperationTimeout() != null) { client.setOpTimeout(conf.getOperationTimeout()); } if (conf instanceof XMemcachedConfiguration) { setProviderClientSpecificSettings(client, (XMemcachedConfiguration) conf); } return new MemcacheClientWrapper(client); }  @Override CacheClient create(final List<InetSocketAddress> addrs, final CacheConfiguration conf);  }
@Test public void createWithBaseConf() throws IOException { CacheConfiguration conf = new CacheConfiguration(); conf.setConsistentHashing(true); conf.setOperationTimeout(1000); conf.setUseBinaryProtocol(false); CacheClient client = factory.create(addrs, conf); assertNotNull(client); client.shutdown(); }
@Test public void createWithSpecificConf() throws IOException { XMemcachedConfiguration conf = new XMemcachedConfiguration(); conf.setConsistentHashing(true); conf.setOperationTimeout(10); conf.setUseBinaryProtocol(false); conf.setConnectionPoolSize(1); conf.setMaxAwayTime(100); conf.setOptimizeGet(true); conf.setSanitizeKeys(true); CacheClient client = factory.create(addrs, conf); assertNotNull(client); client.shutdown(); }
@Test(expected = IllegalArgumentException.class) public void createWithSpecificConfAndNotEnoughWeights() throws IOException { XMemcachedConfiguration conf = new XMemcachedConfiguration(); conf.setConsistentHashing(true); conf.setOperationTimeout(10); conf.setUseBinaryProtocol(false); conf.setConnectionPoolSize(1); conf.setMaxAwayTime(100); conf.setOptimizeGet(true); conf.setSanitizeKeys(true); conf.setWeights(new int[] { 4 }); List<InetSocketAddress> addrs = Arrays.asList(new InetSocketAddress(12345), new InetSocketAddress(12346)); factory.create(addrs, conf); }