class 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());
        }

        // allows to cache data larger than 1MB when using provider serialization
        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);
    }

}