public void testSetLoggingLevelVerbosity() throws Exception { if (memcachedClient.getProtocol() == Protocol.Text || memcachedClient.getProtocol() == Protocol.Binary) { memcachedClient.setLoggingLevelVerbosity( AddrUtil.getAddresses(properties.getProperty("test.memcached.servers")).get(0), 2); memcachedClient.setLoggingLevelVerbosityWithNoReply( AddrUtil.getAddresses(properties.getProperty("test.memcached.servers")).get(0), 3); memcachedClient.setLoggingLevelVerbosityWithNoReply( AddrUtil.getAddresses(properties.getProperty("test.memcached.servers")).get(0), 0); } else { // do nothing,binary protocol doesn't have verbosity protocol. } }
public static void main(String[] args) throws IOException { MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("localhost:11211")); MemcachedClient memcachedClient = builder.build(); try { memcachedClient.set("hello", 0, "Hello,xmemcached"); String value = memcachedClient.get("hello"); System.out.println("hello=" + value); memcachedClient.delete("hello"); value = memcachedClient.get("hello"); System.out.println("hello=" + value); } catch (MemcachedException e) { System.err.println("MemcachedClient operation fail"); e.printStackTrace(); } catch (TimeoutException e) { System.err.println("MemcachedClient operation timeout"); e.printStackTrace(); } catch (InterruptedException e) { // ignore } try { // close memcached client memcachedClient.shutdown(); } catch (IOException e) { System.err.println("Shutdown MemcachedClient fail"); e.printStackTrace(); } }
public void testWeightedServers() throws Exception { // shutdown current client memcachedClient.shutdown(); MemcachedClientBuilder builder = createWeightedBuilder(); builder.getConfiguration().setStatisticsServer(true); memcachedClient = builder.build(); memcachedClient.flushAll(5000); Map<InetSocketAddress, Map<String, String>> oldStats = memcachedClient.getStats(); for (int i = 0; i < 100; i++) { assertTrue(memcachedClient.set(String.valueOf(i), 0, i)); } for (int i = 0; i < 100; i++) { assertEquals(i, memcachedClient.get(String.valueOf(i))); } List<InetSocketAddress> addressList = AddrUtil.getAddresses(properties.getProperty("test.memcached.servers")); Map<InetSocketAddress, Map<String, String>> newStats = memcachedClient.getStats(); for (InetSocketAddress address : addressList) { int oldSets = Integer.parseInt(oldStats.get(address).get("cmd_set")); int newSets = Integer.parseInt(newStats.get(address).get("cmd_set")); System.out.println("sets:" + (newSets - oldSets)); int oldGets = Integer.parseInt(oldStats.get(address).get("cmd_get")); int newGets = Integer.parseInt(newStats.get(address).get("cmd_get")); System.out.println("gets:" + (newGets - oldGets)); } }
public void testRemoveAndAddServer() throws Exception { String servers = properties.getProperty("test.memcached.servers"); memcachedClient.set("name", 0, "dennis"); assertEquals("dennis", memcachedClient.get("name")); memcachedClient.removeServer(servers); synchronized (this) { while (memcachedClient.getAvaliableServers().size() > 0) { wait(1000); } } assertEquals(0, memcachedClient.getAvaliableServers().size()); try { memcachedClient.get("name"); fail(); } catch (MemcachedException e) { assertEquals("There is no available connection at this moment", e.getMessage()); } memcachedClient.addServer(properties.getProperty("test.memcached.servers")); synchronized (this) { while (memcachedClient.getAvaliableServers().size() < AddrUtil.getAddresses(servers).size()) { wait(1000); } } Thread.sleep(5000); assertEquals("dennis", memcachedClient.get("name")); }
@Override public MemcachedClientBuilder createBuilder() throws Exception { MemcachedClientBuilder builder = new XMemcachedClientBuilder( AddrUtil.getAddresses(this.properties.getProperty("test.memcached.servers"))); // builder.setConnectionPoolSize(Runtime.getRuntime().availableProcessors()); return builder; }
public void testGetAvaliableServers() { Collection<InetSocketAddress> servers = memcachedClient.getAvaliableServers(); List<InetSocketAddress> serverList = AddrUtil.getAddresses(properties.getProperty("test.memcached.servers")); assertEquals(servers.size(), serverList.size()); for (InetSocketAddress address : servers) { assertTrue(serverList.contains(address)); } }
public static net.rubyeye.xmemcached.MemcachedClient getXClient() { MemcachedClientBuilder builder = new XMemcachedClientBuilder( net.rubyeye.xmemcached.utils.AddrUtil.getAddresses(MEMCACHED_SERVER_ADDR)); try { return builder.build(); } catch (IOException e) { throw new RuntimeException(e); } }
/** * 获取MemcachedClient * * @param server 服务器 * @return MemcachedClient */ public static MemcachedClient getMemcachedClient(Config c) { MemcachedClientBuilder bulider = new XMemcachedClientBuilder(AddrUtil.getAddresses(c.getMemServer())); bulider.setConnectionPoolSize(2); try { return bulider.build(); } catch (IOException e) { e.printStackTrace(); } return null; }
@Override public MemcachedClientBuilder createWeightedBuilder() throws Exception { List<InetSocketAddress> addressList = AddrUtil.getAddresses(this.properties.getProperty("test.memcached.servers")); int[] weights = new int[addressList.size()]; for (int i = 0; i < weights.length; i++) { weights[i] = i + 1; } MemcachedClientBuilder builder = new XMemcachedClientBuilder(addressList, weights); builder.setSessionLocator(new KetamaMemcachedSessionLocator()); return builder; }
/* * (non-Javadoc) * @see junit.framework.TestCase#setUp() */ 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"); }