net.rubyeye.xmemcached.XMemcachedClientBuilder Java Examples
The following examples show how to use
net.rubyeye.xmemcached.XMemcachedClientBuilder.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XMemcachedCacheManagerFactory.java From memcached-spring-boot with Apache License 2.0 | 6 votes |
@Override IMemcachedClient memcachedClient() throws IOException { final List<InetSocketAddress> servers = properties.getServers(); final MemcachedCacheProperties.Provider provider = properties.getProvider(); final MemcachedCacheProperties.Protocol protocol = properties.getProtocol(); final MemcachedClientBuilder builder = MemcachedCacheProperties.Provider.AWS.equals(provider) ? new AWSElasticCacheClientBuilder(servers) : new XMemcachedClientBuilder(servers); builder.setOpTimeout(properties.getOperationTimeout().toMillis()); builder.setCommandFactory(MemcachedCacheProperties.Protocol.BINARY.equals(protocol) ? new BinaryCommandFactory() : new TextCommandFactory()); if (properties.getHashStrategy() != MemcachedCacheProperties.HashStrategy.STANDARD) { builder.setSessionLocator(getSessionLocator(properties.getHashStrategy())); } return new XMemcachedClient(builder.build()); }
Example #2
Source File: TestFqueueServer.java From fqueue with Apache License 2.0 | 6 votes |
protected void setUp() throws Exception { Config.setSetting("port", "12001"); Config.setSetting("path", "dbtest"); Config.setSetting("logsize", "40"); Config.setSetting("authorization", "key|abc@@bbs|pass"); StartNewQueue.newQueueInstance(Integer.parseInt(Config.getSetting("port"))); log.info("running at port " + Config.getSetting("port")); builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("127.0.0.1:12001")); builder.setConnectionPoolSize(50); // set connection pool size to five try { client = builder.build(); client.setOptimizeGet(false); builder.setSocketOption(StandardSocketOption.SO_KEEPALIVE, true); builder.setSocketOption(StandardSocketOption.SO_RCVBUF, 64 * 1024); builder.setSocketOption(StandardSocketOption.SO_SNDBUF, 64 * 1024); builder.setSocketOption(StandardSocketOption.SO_REUSEADDR, true); builder.setSocketOption(StandardSocketOption.TCP_NODELAY, false); } catch (IOException e) { throw new RuntimeException(e); } client.get("clear|key|abc"); }
Example #3
Source File: TestFqueueServer.java From fqueue with Apache License 2.0 | 6 votes |
protected void setUp() throws Exception { Config.setSetting("port", "12001"); Config.setSetting("path", "dbtest"); Config.setSetting("logsize", "40"); Config.setSetting("authorization", "key|abc@@bbs|pass"); StartNewQueue.newQueueInstance(Integer.parseInt(Config.getSetting("port"))); log.info("running at port " + Config.getSetting("port")); builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("127.0.0.1:12001")); builder.setConnectionPoolSize(50); // set connection pool size to five try { client = builder.build(); client.setOptimizeGet(false); builder.setSocketOption(StandardSocketOption.SO_KEEPALIVE, true); builder.setSocketOption(StandardSocketOption.SO_RCVBUF, 64 * 1024); builder.setSocketOption(StandardSocketOption.SO_SNDBUF, 64 * 1024); builder.setSocketOption(StandardSocketOption.SO_REUSEADDR, true); builder.setSocketOption(StandardSocketOption.TCP_NODELAY, false); } catch (IOException e) { throw new RuntimeException(e); } client.get("clear|key|abc"); }
Example #4
Source File: MemcacheServlet.java From java-docs-samples with Apache License 2.0 | 6 votes |
@Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { String addr = System.getenv().containsKey("GAE_MEMCACHE_HOST") ? System.getenv("GAE_MEMCACHE_HOST") : "localhost"; String port = System.getenv().containsKey("GAE_MEMCACHE_HOST") ? System.getenv("GAE_MEMCACHE_PORT") : "11211"; String key = "count"; MemcachedClientBuilder builder = new XMemcachedClientBuilder( AddrUtil.getAddresses(addr + ":" + port)); MemcachedClient client = builder.build(); long count = 0L; try { count = client.incr(key, 1L, 0L); } catch (TimeoutException | InterruptedException | MemcachedException e) { throw new ServletException("Memcache error", e); } resp.setContentType("text/plain"); resp.getWriter().print("Value is " + count + "\n"); }
Example #5
Source File: MemcachedPool.java From jea with Apache License 2.0 | 6 votes |
/** * 分布策略为一致性哈希 */ private void initPool(){ StringBuffer memcachedAddr = new StringBuffer(); for(String addr : addresses){ memcachedAddr.append(addr).append(" "); } socketAddress = AddrUtil.getAddresses (memcachedAddr.toString().trim()); builder = new XMemcachedClientBuilder(socketAddress); builder.setConnectionPoolSize(poolConfig.getMaxTotal()); builder.setConnectTimeout(poolConfig.getMaxConnectMillis()); builder.setFailureMode(poolConfig.getFailureMode()); builder.setOpTimeout(poolConfig.getMaxWaitMillis()); builder.setEnableHealSession(poolConfig.getEnableHealSession()); builder.setHealSessionInterval(poolConfig.getHealSessionInterval()); /** * 分布策略 * 默认分布的策略是按照key的哈希值模以连接数得到的余数 * KetamaMemcachedSessionLocator:一致性哈希(consistent hash) * ElectionMemcachedSessionLocator:选举散列哈希算法 */ builder.setSessionLocator(new KetamaMemcachedSessionLocator()); }
Example #6
Source File: XmemcachedFactory.java From pippo with Apache License 2.0 | 6 votes |
/** * Create a memcached client with params. * * @param hosts whitespace separated host or IP addresses and port numbers * of the form "host:port host2:port hostN:portN" * @param protocol opcional, BINARY or TEXT * @param user opcional, user name o null * @param pass opcional, password o null * @param authMechanisms opcional, CRAM-MD5 and/or PLAIN * @return memcached client */ public static MemcachedClient create( String hosts, CommandFactory protocol, String user, String pass, String[] authMechanisms) { MemcachedClient client = null; try { MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses(hosts)); builder.setCommandFactory(protocol); if (isNotNullOrEmpty(user)) { builder.addAuthInfo( AddrUtil.getAddresses(hosts).get(0), new AuthInfo( new PlainCallbackHandler(user, pass), authMechanisms)); } client = builder.build(); } catch (IOException ex) { log.error("An error occurred when creating the MemcachedClient.", ex); throw new PippoRuntimeException(ex); } return client; }
Example #7
Source File: MemcachedConfig.java From ChengFeng1.5 with MIT License | 5 votes |
@Bean public MemcachedClient getMemcachedClient() { MemcachedClient memcachedClient = null; try { MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil. getAddresses(memcachedProperties.getServers())); builder.setConnectionPoolSize(memcachedProperties.getPoolSize()); builder.setOpTimeout(memcachedProperties.getOpTimeout()); memcachedClient = builder.build(); } catch (IOException e) { log.error("inint MemcachedClient failed ",e); } return memcachedClient; }
Example #8
Source File: XmemcachedCacheProvider.java From J2Cache with Apache License 2.0 | 5 votes |
@Override public void start(Properties props) { long ct = System.currentTimeMillis(); String servers = props.getProperty("servers", "127.0.0.1:11211"); String username = props.getProperty("username", ""); String password = props.getProperty("password", ""); MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses(servers)); builder.setCommandFactory(new BinaryCommandFactory()); boolean needAuth = username != null && password != null && username.trim().length() > 0 && password.trim().length() > 0; if(needAuth) builder.addAuthInfo(AddrUtil.getOneAddress(servers), AuthInfo.typical(username, password)); builder.setConnectionPoolSize(Integer.valueOf(props.getProperty("connectionPoolSize", "10"))); builder.setConnectTimeout(Long.valueOf(props.getProperty("connectTimeout", "1000"))); builder.setHealSessionInterval(Long.valueOf(props.getProperty("healSessionInterval", "1000"))); builder.setMaxQueuedNoReplyOperations(Integer.valueOf(props.getProperty("maxQueuedNoReplyOperations", "100"))); builder.setOpTimeout(Long.valueOf(props.getProperty("opTimeout", "100"))); builder.setSanitizeKeys("true".equalsIgnoreCase(props.getProperty("sanitizeKeys", "false"))); try { client = builder.build(); log.info("Memcached client starts with servers({}),auth({}),pool-size({}),time({}ms)", servers, needAuth, builder.getConfiguration().getSelectorPoolSize(), System.currentTimeMillis() - ct ); } catch (IOException e) { log.error("Failed to connect to memcached", e); } }
Example #9
Source File: MemcachedConfig.java From SpringMVC-Project with MIT License | 5 votes |
@Bean public MemcachedClient memcachedClient() throws IOException { MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil. getAddresses(address), new int[]{1}); //存储的数据使用JSON格式,兼容不同客户端,如果是单一客户端可以不需要 Transcoder JSON_TRANSCODER = new JSONTranscoder(); builder.setTranscoder(JSON_TRANSCODER); return builder.build(); }
Example #10
Source File: MemcachedCacheIT.java From memcached-spring-boot with Apache License 2.0 | 5 votes |
@Bean public IMemcachedClient memcachedClient() throws IOException { final String host = memcached.getContainerIpAddress(); final int port = memcached.getMappedPort(11211); return new XMemcachedClient(new XMemcachedClientBuilder(Collections.singletonList(new InetSocketAddress(host, port))).build()); }
Example #11
Source File: MemcacheClientFactoryImpl.java From simple-spring-memcached with MIT License | 4 votes |
private void setProviderBuilderSpecificSettings(final XMemcachedClientBuilder builder, final XMemcachedConfiguration conf) { if (conf.getConnectionPoolSize() != null) { builder.setConnectionPoolSize(conf.getConnectionPoolSize()); } if (conf.getConfiguration() != null) { builder.setConfiguration(conf.getConfiguration()); } if (conf.getFailureMode() != null) { builder.setFailureMode(conf.getFailureMode()); } if (conf.getSocketOptions() != null) { for (Map.Entry<SocketOption<?>, Object> entry : conf.getSocketOptions().entrySet()) { builder.setSocketOption(entry.getKey(), entry.getValue()); } } if (conf.getDefaultTranscoder() != null) { builder.setTranscoder(conf.getDefaultTranscoder()); } if (conf.getConnectionTimeout() != null) { builder.setConnectTimeout(conf.getConnectionTimeout()); } if (conf.getMaxQueuedNoReplyOperations() != null) { builder.setMaxQueuedNoReplyOperations(conf.getMaxQueuedNoReplyOperations()); } if (conf.getEnableHealSession() != null) { builder.setEnableHealSession(conf.getEnableHealSession()); } if (conf.getAuthInfoMap() != null) { builder.setAuthInfoMap(conf.getAuthInfoMap()); } if (conf.getOperationTimeout() != null) { builder.setOpTimeout(conf.getOperationTimeout()); } if (conf.getSanitizeKeys() != null) { builder.setSanitizeKeys(conf.getSanitizeKeys()); } if (conf.getSelectorPoolSize() != null) { builder.setSelectorPoolSize(conf.getSelectorPoolSize()); } if (conf.getStateListeners() != null) { builder.setStateListeners(conf.getStateListeners()); } }