public void _testErrorCommand() throws Exception {
    Command nonexisCmd =
        new Command() {

          @Override
          public boolean decode(MemcachedTCPSession session, ByteBuffer buffer) {
            return decodeError(ByteUtils.nextLine(buffer));
          }

          @Override
          public void encode() {
            ioBuffer = IoBuffer.wrap(ByteBuffer.wrap("test\r\n".getBytes()));
          }
        };
    nonexisCmd.setKey("test");
    nonexisCmd.setLatch(new CountDownLatch(1));
    memcachedClient.getConnector().send(nonexisCmd);
    // this.memcachedClient.flushAll();
    nonexisCmd.getLatch().await();

    assertNotNull(nonexisCmd.getException());
    assertEquals(
        "Nonexist command,check your memcached version please.",
        nonexisCmd.getException().getMessage());
    assertTrue(nonexisCmd.getException() instanceof UnknownCommandException);

    memcachedClient.set("name", 0, "dennis");
    assertEquals("dennis", memcachedClient.get("name"));
  }
Example #2
0
 public long incr(String key, long delta, long initValue, int exp) {
   try {
     return memcachedClient.incr(key, delta, initValue, memcachedClient.getOpTimeout(), exp);
   } catch (Exception e) {
     throw new IllegalStateException(e.getMessage(), e);
   }
 }
  public void testStoreCollection()
      throws TimeoutException, InterruptedException, MemcachedException {
    // store list
    List<String> list = new ArrayList<String>();
    for (int i = 0; i < 100; i++) {
      list.add(String.valueOf(i));
    }
    assertTrue(memcachedClient.add("list", 0, list));
    List<String> listFromCache = memcachedClient.get("list");
    assertEquals(100, listFromCache.size());

    for (int i = 0; i < listFromCache.size(); i++) {
      assertEquals(list.get(i), listFromCache.get(i));
    }
    // store map
    Map<String, Integer> map = new HashMap<String, Integer>();

    for (int i = 0; i < 100; i++) {
      map.put(String.valueOf(i), i);
    }
    assertTrue(memcachedClient.add("map", 0, map));
    Map<String, Integer> mapFromCache = memcachedClient.get("map");
    assertEquals(100, listFromCache.size());

    for (int i = 0; i < listFromCache.size(); i++) {
      assertEquals(mapFromCache.get(i), map.get(i));
    }
  }
 public void testStats() throws Exception {
   assertTrue(memcachedClient.getStats().size() > 0);
   System.out.println(memcachedClient.getStats());
   memcachedClient.set("a", 0, 1);
   assertTrue(memcachedClient.getStatsByItem("items").size() > 0);
   System.out.println(memcachedClient.getStatsByItem("items"));
 }
  public void testAdd() throws Exception {
    assertTrue(memcachedClient.add("name", 0, "dennis"));
    assertFalse(memcachedClient.add("name", 0, "dennis"));
    assertEquals("dennis", memcachedClient.get("name", 2000));

    // blank key
    new BlankKeyChecker() {
      @Override
      public void call() throws Exception {
        memcachedClient.add("", 0, 1);
      }
    }.check();
    // null key
    new BlankKeyChecker() {
      @Override
      public void call() throws Exception {
        memcachedClient.add((String) null, 0, 1);
      }
    }.check();

    // invalid key
    new InValidKeyChecker() {
      @Override
      public void call() throws Exception {
        memcachedClient.add("test\r\n", 0, 1);
      }
    }.check();
    new InValidKeyChecker() {
      @Override
      public void call() throws Exception {
        memcachedClient.add("test test2", 0, 1);
      }
    }.check();

    // key is too long
    new TooLongKeyChecker(memcachedClient) {
      @Override
      public void call() throws Exception {
        int keyLength = memcachedClient.getProtocol() == Protocol.Text ? 256 : 65536;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < keyLength; i++) {
          sb.append(i);
        }
        memcachedClient.add(sb.toString(), 0, 1);
      }
    }.check();

    // Transcoder
    new TranscoderChecker(mockTranscoder, 2) {
      @Override
      public void call() throws Exception {
        memcachedClient.add("a", 0, 100, mockTranscoder);
        assertEquals(100, memcachedClient.get("a", mockTranscoder));
      }
    }.check();
  }
  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"));
  }
 public void testIssue150() throws Exception {
   memcachedClient.set("a", 0, 1);
   try {
     memcachedClient.incr("a", 1);
     fail();
   } catch (MemcachedException e) {
     // assertEquals("cannot increment or decrement non-numeric value",e.getMessage());
   }
   memcachedClient.set("a", 0, "1");
   assertEquals(3, memcachedClient.incr("a", 2));
 }
Example #8
0
 @Test
 public void simpleTest() throws TimeoutException, InterruptedException, MemcachedException {
   long start = System.currentTimeMillis();
   String value = "simpleValue";
   Assert.assertTrue(memcachedClient.set(key, 120, value));
   Assert.assertEquals(value, memcachedClient.get(key));
   // Assert.assertTrue(memcachedClient.touch(key, 120));
   Assert.assertTrue(memcachedClient.delete(key));
   Assert.assertNull(memcachedClient.get(key));
   System.out.println("use time: " + (System.currentTimeMillis() - start));
 }
Example #9
0
 public void authorization() throws TimeoutException, InterruptedException, IOException {
   try {
     client.get("xxxxxxxxxx");
   } catch (MemcachedException e) {
     assertEquals("xxxxxxxxxx command Unsupported now", e.getMessage());
   }
   try {
     client.get("xxxxxxxxxx_xxx");
   } catch (MemcachedException e) {
     assertEquals("Authorization error", e.getMessage());
   }
 }
 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.
   }
 }
Example #11
0
 /**
  * 测试类
  *
  * @param client
  */
 public void test(MemcachedClient client) {
   try {
     System.out.println(client.get("1"));
     if (client.get("1") == null) {
       client.set("1", 0, "11111111111");
     }
   } catch (TimeoutException e) {
     e.printStackTrace();
   } catch (InterruptedException e) {
     e.printStackTrace();
   } catch (MemcachedException e) {
     e.printStackTrace();
   }
 }
Example #12
0
 public void deleteWithNoReply(String key) {
   try {
     memcachedClient.deleteWithNoReply(key);
   } catch (Exception e) {
     throw new IllegalStateException(e.getMessage(), e);
   }
 }
Example #13
0
 public boolean delete(String key) {
   try {
     return memcachedClient.delete(key);
   } catch (Exception e) {
     throw new IllegalStateException(e.getMessage(), e);
   }
 }
  public void testTouch() throws Exception {
    this.memcachedClient.set("x", 1, 0);
    assertEquals(0, this.memcachedClient.get("x"));
    assertTrue(this.memcachedClient.touch("x", 1));
    assertEquals(0, this.memcachedClient.get("x"));
    assertTrue(this.memcachedClient.touch("x", 1));
    Thread.sleep(1100);
    assertNull(this.memcachedClient.get("x"));
    if (memcachedClient.getProtocol() == Protocol.Binary) {
      this.memcachedClient.set("x", 1, 0);
      assertEquals(0, this.memcachedClient.getAndTouch("x", 1));
    }

    // touch not exists
    assertFalse(memcachedClient.touch("not_exists", 0));
  }
 public static Object getXCache(net.rubyeye.xmemcached.MemcachedClient mcc, String key) {
   try {
     return mcc.get(key);
   } catch (Exception e) {
     throw new RuntimeException(e);
   }
 }
Example #16
0
 private int getSize() throws TimeoutException, InterruptedException, MemcachedException {
   String value = client.get("size|key");
   if (value != null) {
     return Integer.parseInt(value);
   }
   return -1;
 }
Example #17
0
 public void flushAllWithNoReply() {
   try {
     memcachedClient.flushAllWithNoReply();
   } catch (Exception e) {
     throw new IllegalStateException(e.getMessage(), e);
   }
 }
Example #18
0
 public long decr(String key, long delta, long initValue) {
   try {
     return memcachedClient.decr(key, delta, initValue);
   } catch (Exception e) {
     throw new IllegalStateException(e.getMessage(), e);
   }
 }
Example #19
0
 public boolean add(String key, Object value, int exp) {
   try {
     return memcachedClient.add(key, exp, value);
   } catch (Exception e) {
     throw new IllegalStateException(e.getMessage(), e);
   }
 }
Example #20
0
 public void addWithNoReply(String key, Object value, int exp) {
   try {
     memcachedClient.addWithNoReply(key, exp, value);
   } catch (Exception e) {
     throw new IllegalStateException(e.getMessage(), e);
   }
 }
Example #21
0
 public Object getAndTouch(String key, int newExp) {
   try {
     return memcachedClient.getAndTouch(key, newExp);
   } catch (Exception e) {
     throw new IllegalStateException(e.getMessage(), e);
   }
 }
Example #22
0
 public boolean set(String key, Object value) {
   try {
     return memcachedClient.set(key, Integer.MAX_VALUE, value); // TODO
   } catch (Exception e) {
     throw new IllegalStateException(e.getMessage(), e);
   }
 }
Example #23
0
 public Object get(String key) {
   try {
     return memcachedClient.get(key);
   } catch (Exception e) {
     throw new IllegalStateException(e.getMessage(), e);
   }
 }
Example #24
0
 public Map<String, Object> get(String[] keys) {
   try {
     return memcachedClient.get(Arrays.asList(keys));
   } catch (Exception e) {
     throw new IllegalStateException(e.getMessage(), e);
   }
 }
 public static boolean setXCache(
     net.rubyeye.xmemcached.MemcachedClient mcc, String key, int exp, Object value) {
   try {
     return mcc.set(key, exp, value);
   } catch (Exception e) {
     throw new RuntimeException(e);
   }
 }
 public void testFlushAll() throws Exception {
   for (int i = 0; i < 50; i++) {
     assertTrue(memcachedClient.add(String.valueOf(i), 0, i));
   }
   List<String> keys = new ArrayList<String>();
   for (int i = 0; i < 100; i++) {
     keys.add(String.valueOf(i));
   }
   Map<String, Integer> result = memcachedClient.get(keys);
   assertEquals(50, result.size());
   for (int i = 0; i < 50; i++) {
     assertEquals((Integer) i, result.get(String.valueOf(i)));
   }
   memcachedClient.flushAll();
   result = memcachedClient.get(keys);
   assertTrue(result.isEmpty());
 }
 @Override
 public void setPage(String key, String pageContent, int expires) throws PageCacheException {
   try {
     memcachedClient.set(key, expires, pageContent);
   } catch (Exception e) {
     throw new PageCacheException(e.getMessage());
   }
 }
 @Override
 public void evict(Object key) {
   try {
     memcachedClient.delete(keyToString(key));
   } catch (Exception e) {
     log.warn("Unable to delete object from cache", throwableOrMessage(e));
   }
 }
 @Override
 public void put(Object key, Object value) {
   try {
     memcachedClient.set(keyToString(key), expiry, value);
   } catch (Exception e) {
     log.warn("Unable to put object to cache", throwableOrMessage(e));
   }
 }
Example #30
0
 @Override
 public void updateOnlineState(long uid) {
   try {
     memcachedClient.set(MemcachedKeyGenerator.genUserOnlineKey(uid), userOnlineExpireTime, true);
   } catch (Exception e) {
     log.error(e.getMessage(), e);
   }
 }