protected MemcachedImpl(boolean isTTStore) throws Exception { Properties configuration = new Properties(); configuration.load(MemcachedImpl.class.getResourceAsStream("/config.properties")); List<InetSocketAddress> addrList = new ArrayList<InetSocketAddress>(); int nb = 1; String prefix = isTTStore ? "ttcached." : "memcached."; while (configuration.containsKey(prefix + nb + ".host")) { String hostandport = configuration.getProperty(prefix + nb + ".host"); String host = StringUtils.substringBefore(hostandport, ":"); String portStr = StringUtils.substringAfter(hostandport, ":"); int port = NumberUtils.toInt(portStr, 11211); System.out.println( "memcached init [host:" + host + ",port:" + port + ",portStr:" + portStr + "]"); addrList.add(new InetSocketAddress(host, port)); nb++; } if (addrList.size() == 0) { throw new Exception("Bad configuration for memcached or tt store"); } MemcachedClientBuilder builder = new XMemcachedClientBuilder(addrList); try { builder.setConnectionPoolSize(5); this.client = builder.build(); } catch (IOException e) { e.printStackTrace(); } }
/** * 获取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; }
/* * (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"); }