class MemcacheClientFactoryImpl implements CacheClientFactory {

    @Override
    public CacheClient create(final List<InetSocketAddress> addrs, final CacheConfiguration conf) throws IOException {
        // currently its works because this factory creates clients with the same connection settings, only memcached
        // addresses can be changed
        if (connectionFactory == null) {
            ElastiCacheConfiguration elasticacheConf = null;
            if (conf instanceof ElastiCacheConfiguration) {
                elasticacheConf = (ElastiCacheConfiguration) conf;
            }
            
            if (elasticacheConf != null && Boolean.TRUE.equals(elasticacheConf.getUseAutoDiscovery())) {
                // there is no way to use custom client settings and auto discovery together
                LOGGER.info("All cache settings will be ignored because useAutoDiscovery is true");
                return new MemcacheClientWrapper(new MemcachedClient(addrs));
            }
            
            
            ConnectionFactoryBuilder builder = new ConnectionFactoryBuilder();

            if (conf.isConsistentHashing()) {
                builder.setHashAlg(DefaultHashAlgorithm.KETAMA_HASH);
                builder.setLocatorType(Locator.CONSISTENT);
            }

            builder.setProtocol(conf.isUseBinaryProtocol() ? Protocol.BINARY : Protocol.TEXT);
            if (conf.getOperationTimeout() != null) {
                builder.setOpTimeout(conf.getOperationTimeout());
            }

            if (elasticacheConf != null) {
                setProviderSpecificSettings(builder, elasticacheConf);
            }

            connectionFactory = builder.build();
        }

        return new MemcacheClientWrapper(new MemcachedClient(connectionFactory, addrs));
    }

}