public static MemcachedCache create(final MemcachedCacheConfig config, String memcachedPrefix, int timeToLive) { try { SerializingTranscoder transcoder = new SerializingTranscoder(config.getMaxObjectSize()); // always no compression inside, we compress/decompress outside transcoder.setCompressionThreshold(Integer.MAX_VALUE); OperationQueueFactory opQueueFactory; int maxQueueSize = config.getMaxOperationQueueSize(); if (maxQueueSize > 0) { opQueueFactory = new ArrayOperationQueueFactory(maxQueueSize); } else { opQueueFactory = new LinkedOperationQueueFactory(); } String hostsStr = config.getHosts(); ConnectionFactory connectionFactory = new MemcachedConnectionFactoryBuilder() .setProtocol(ConnectionFactoryBuilder.Protocol.BINARY) .setHashAlg(DefaultHashAlgorithm.FNV1A_64_HASH) .setLocatorType(ConnectionFactoryBuilder.Locator.CONSISTENT).setDaemon(true) .setFailureMode(FailureMode.Redistribute).setTranscoder(transcoder).setShouldOptimize(true) .setOpQueueMaxBlockTime(config.getTimeout()).setOpTimeout(config.getTimeout()) .setReadBufferSize(config.getReadBufferSize()).setOpQueueFactory(opQueueFactory).build(); return new MemcachedCache(new MemcachedClient(new MemcachedConnectionFactory(connectionFactory), AddrUtil.getAddresses(hostsStr)), config, memcachedPrefix, timeToLive); } catch (IOException e) { logger.error("Unable to create MemcachedCache instance.", e); throw Throwables.propagate(e); } }
.setProtocol(ConnectionFactoryBuilder.Protocol.valueOf(StringUtils.toUpperCase(config.getProtocol()))) .setLocatorType(ConnectionFactoryBuilder.Locator.valueOf(StringUtils.toUpperCase(config.getLocator()))) .setDaemon(true) .setFailureMode(FailureMode.Cancel) .setTranscoder(transcoder) .setShouldOptimize(true) .setOpQueueMaxBlockTime(config.getTimeout()) .setOpTimeout(config.getTimeout()) .setReadBufferSize(config.getReadBufferSize()) .setOpQueueFactory(opQueueFactory) .setMetricCollector(metricCollector) .setEnableMetrics(MetricType.DEBUG) // Not as scary as it sounds .build();
private ConnectionFactoryBuilder newConnectionFactoryBuilder(final Config conf) { ConnectionFactoryBuilder builder = new ConnectionFactoryBuilder(); ifset(conf, "authWaitTime", path -> builder .setAuthWaitTime(conf.getDuration(path, TimeUnit.MILLISECONDS))); ifset(conf, "daemon", path -> builder.setDaemon(conf.getBoolean(path))); ifset(conf, "enableMetrics", path -> builder .setEnableMetrics(enumFor(conf.getString(path), MetricType.values()))); ifset(conf, "failureMode", path -> builder .setFailureMode(enumFor(conf.getString(path), FailureMode.values()))); ifset(conf, "locator", path -> builder .setLocatorType(enumFor(conf.getString(path), Locator.values()))); ifset(conf, "maxReconnectDelay", path -> builder .setMaxReconnectDelay(conf.getDuration(path, TimeUnit.SECONDS))); ifset(conf, "opQueueMaxBlockTime", path -> builder .setOpQueueMaxBlockTime(conf.getDuration(path, TimeUnit.MILLISECONDS))); ifset(conf, "opTimeout", path -> builder .setOpTimeout(conf.getDuration(path, TimeUnit.MILLISECONDS))); ifset(conf, "protocol", path -> builder .setProtocol(enumFor(conf.getString(path), Protocol.values()))); ifset(conf, "readBufferSize", path -> builder .setReadBufferSize(conf.getInt(path))); ifset(conf, "shouldOptimize", path -> builder .setShouldOptimize(conf.getBoolean(path))); ifset(conf, "timeoutExceptionThreshold", path -> builder .setTimeoutExceptionThreshold(conf.getInt(path))); ifset(conf, "useNagleAlgorithm", path -> builder .setUseNagleAlgorithm(conf.getBoolean(path))); return builder; }
public MemcachedBlockCache(Configuration c) throws IOException { LOG.info("Creating MemcachedBlockCache"); long opTimeout = c.getLong(MEMCACHED_OPTIMEOUT_KEY, MEMCACHED_DEFAULT_TIMEOUT); long queueTimeout = c.getLong(MEMCACHED_TIMEOUT_KEY, opTimeout + MEMCACHED_DEFAULT_TIMEOUT); boolean optimize = c.getBoolean(MEMCACHED_OPTIMIZE_KEY, MEMCACHED_OPTIMIZE_DEFAULT); ConnectionFactoryBuilder builder = new ConnectionFactoryBuilder() .setOpTimeout(opTimeout) .setOpQueueMaxBlockTime(queueTimeout) // Cap the max time before anything times out .setFailureMode(FailureMode.Redistribute) .setShouldOptimize(optimize) .setDaemon(true) // Don't keep threads around past the end of days. .setUseNagleAlgorithm(false) // Ain't nobody got time for that .setReadBufferSize(HConstants.DEFAULT_BLOCKSIZE * 4 * 1024); // Much larger just in case // Assume only the localhost is serving memecached. // A la mcrouter or co-locating memcached with split regionservers. // // If this config is a pool of memecached servers they will all be used according to the // default hashing scheme defined by the memcache client. Spy Memecache client in this // case. String serverListString = c.get(MEMCACHED_CONFIG_KEY,"localhost:11211"); String[] servers = serverListString.split(","); List<InetSocketAddress> serverAddresses = new ArrayList<>(servers.length); for (String s:servers) { serverAddresses.add(Addressing.createInetSocketAddressFromHostAndPortStr(s)); } client = new MemcachedClient(builder.build(), serverAddresses); }
protected net.spy.memcached.MemcachedClient createMemcachedClient() throws Exception { ConnectionFactoryBuilder connectionFactoryBuilder = new ConnectionFactoryBuilder(); connectionFactoryBuilder.setReadBufferSize(Integer.parseInt( getProperties().getProperty(READ_BUFFER_SIZE_PROPERTY, DEFAULT_READ_BUFFER_SIZE))); connectionFactoryBuilder.setOpTimeout(Integer.parseInt( getProperties().getProperty(OP_TIMEOUT_PROPERTY, DEFAULT_OP_TIMEOUT))); connectionFactoryBuilder.setProtocol( protocolString == null ? DEFAULT_PROTOCOL : ConnectionFactoryBuilder.Protocol.valueOf(protocolString.toUpperCase())); connectionFactoryBuilder.setFailureMode( failureString == null ? FAILURE_MODE_PROPERTY_DEFAULT : FailureMode.valueOf(failureString)); connectionFactoryBuilder.build(), addresses);
public ConnectionFactoryBuilder(ConnectionFactory cf) { setAuthDescriptor(cf.getAuthDescriptor()); setDaemon(cf.isDaemon()); setFailureMode(cf.getFailureMode()); setHashAlg(cf.getHashAlg()); setInitialObservers(cf.getInitialObservers()); setMaxReconnectDelay(cf.getMaxReconnectDelay()); setOpQueueMaxBlockTime(cf.getOpQueueMaxBlockTime()); setOpTimeout(cf.getOperationTimeout()); setReadBufferSize(cf.getReadBufSize()); setShouldOptimize(cf.shouldOptimize()); setTimeoutExceptionThreshold(cf.getTimeoutExceptionThreshold()); setTranscoder(cf.getDefaultTranscoder()); setUseNagleAlgorithm(cf.useNagleAlgorithm()); }
ConnectionFactory cf = new ConnectionFactoryBuilder() .setProtocol(ConnectionFactoryBuilder.Protocol.BINARY) .setAuthDescriptor(ad) .build();
new ConnectionFactoryBuilder().setProtocol(ConnectionFactoryBuilder.Protocol.BINARY) .setHashAlg(DefaultHashAlgorithm.FNV1A_64_HASH) .setLocatorType(ConnectionFactoryBuilder.Locator.CONSISTENT) .setDaemon(true) .setFailureMode(FailureMode.Retry) .setTranscoder(transcoder) .setShouldOptimize(true) .build(), AddrUtil.getAddresses(hosts) );
memcachedNodesManager.getSessionIdFormat(), statistics, operationTimeout, maxReconnectDelay, authDescriptor) : new ConnectionFactoryBuilder().setProtocol(ConnectionFactoryBuilder.Protocol.BINARY) .setAuthDescriptor(authDescriptor) .setOpTimeout(operationTimeout) .setMaxReconnectDelay(maxReconnectDelay) .build();
@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) { 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 (conf instanceof SpymemcachedConfiguration) { setProviderSpecificSettings(builder, (SpymemcachedConfiguration) conf); } connectionFactory = builder.build(); } return new MemcacheClientWrapper(new MemcachedClient(connectionFactory, addrs)); }
ConnectionFactoryBuilder cfb = new ConnectionFactoryBuilder(); cfb.setDaemon(true); // Just to keep the process from hanging cfb.setFailureMode(failMode); // How to handle operations when they fail cfb.setMaxReconnectDelay(60); // At most, wait 1 minute for attempting to reconnect to a server cfb.setOpTimeout(opTimeoutMillis); // Use the same for the connection as the concurrent Future object cfb.setProtocol(Protocol.BINARY); client = new MemcachedClient(cfb.build(), servers); } else { client = testClient;
public MemcachedCache(CacheConfig<K, V> config) throws IOException { this.cacheConfig = config; String addr = config.getHostname() + ":" + config.getPortNumber(); this.client = new MemcachedClient(new ConnectionFactoryBuilder().setDaemon(true).setFailureMode(FailureMode.Retry).build(), AddrUtil.getAddresses(addr)); }
@Override public MemcachedClient get() { client = throwingSupplier(() -> { ConnectionFactory connectionFactory = builder.build(); this.builder = null; return new MemcachedClient(connectionFactory, servers); }).get(); return client; }
private MemcachedClient memcachedClient() throws IOException { final List<InetSocketAddress> servers = properties.getServers(); final ClientMode mode = properties.getMode(); final MemcachedCacheProperties.Protocol protocol = properties.getProtocol(); final ConnectionFactoryBuilder connectionFactoryBuilder = new ConnectionFactoryBuilder() .setClientMode(mode) .setProtocol(protocol.value()); return new MemcachedClient(connectionFactoryBuilder.build(), servers); } }
private MemcachedClient createMemcachedClient(long timeout, int port) throws IOException { ConnectionFactory cf = new DefaultConnectionFactory() { @Override public long getOperationTimeout() { return timeout; } }; if (transcoder != null) { cf = new ConnectionFactoryBuilder(cf).setTranscoder(transcoder).build(); } return new MemcachedClient(cf, Collections.singletonList(new InetSocketAddress("127.0.0.1", port))); }
ConnectionFactoryBuilder builder = new ConnectionFactoryBuilder(); builder.setHashAlgorithm(HashAlgorithm.CRC_HASH); ConnectionFactory factory = builder.build(); MemcachedClient client = new MemcachedClient(Arrays.asList(new InetSocketAddr("localhost", 11211)), factory);
public void setProtocol(final Protocol prot) { connectionFactoryBuilder.setProtocol(prot); }
protected ConnectionFactoryBuilder getConnectionFactoryBuilder() { return new ConnectionFactoryBuilder(); }
cfb.setInitialObservers(Collections.singleton(observer)); for (int i = 0; i < poolSize; i++) { try { client[i] = ArcusClient.getInstance(cfb.build(), socketList); client[i].setName("Memcached IO for " + serviceCode); client[i].setCacheManager(this);
public void setDaemon(final boolean d) { connectionFactoryBuilder.setDaemon(d); }