Example #1
0
  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();
    }
  }
Example #2
0
  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));
    }
  }
  protected void createClients()
      throws IOException, Exception, TimeoutException, InterruptedException, MemcachedException {
    properties = ResourcesUtils.getResourceAsProperties("test.properties");

    MemcachedClientBuilder builder = createBuilder();
    builder.getConfiguration().setStatisticsServer(true);
    memcachedClient = builder.build();
    memcachedClient.flushAll();
  }
 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);
   }
 }
Example #6
0
 /**
  * 获取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;
 }
  public void testCreateClientWithEmptyServers() throws Exception {
    MemcachedClient client = new XMemcachedClient();
    assertFalse(client.isShutdown());
    client.shutdown();
    assertTrue(client.isShutdown());

    MemcachedClientBuilder builder = new XMemcachedClientBuilder();
    client = builder.build();
    assertFalse(client.isShutdown());
    client.shutdown();
    assertTrue(client.isShutdown());
  }
  @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;
  }
Example #9
0
  /*
   * (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");
  }