コード例 #1
0
ファイル: MemcachedTest.java プロジェクト: gailo22/myproject
  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();
    }
  }
コード例 #2
0
ファイル: MemcacheImpl.java プロジェクト: shefansxyh/eap
 public boolean delete(String key) {
   try {
     return memcachedClient.delete(key);
   } catch (Exception e) {
     throw new IllegalStateException(e.getMessage(), e);
   }
 }
コード例 #3
0
 @Override
 public void evict(Object key) {
   try {
     memcachedClient.delete(keyToString(key));
   } catch (Exception e) {
     log.warn("Unable to delete object from cache", throwableOrMessage(e));
   }
 }
コード例 #4
0
ファイル: LoginService.java プロジェクト: huaj12/weguan
 @Override
 public void logout(HttpServletRequest request, HttpServletResponse response, long uid) {
   loginSessionManager.logout(request, response);
   try {
     memcachedClient.delete(MemcachedKeyGenerator.genUserOnlineKey(uid));
   } catch (Exception e) {
     log.error(e.getMessage(), e);
   }
 }
コード例 #5
0
  public void testDelete() throws Exception {
    assertTrue(memcachedClient.set("name", 0, "dennis"));
    assertEquals("dennis", memcachedClient.get("name"));
    assertTrue(memcachedClient.delete("name"));
    assertNull(memcachedClient.get("name"));
    assertFalse(memcachedClient.delete("not_exists"));

    memcachedClient.set("name", 0, "dennis");
    assertEquals("dennis", memcachedClient.get("name"));
    assertTrue(memcachedClient.delete("name"));
    assertNull(memcachedClient.get("name"));
    memcachedClient.set("name", 0, "dennis");
    assertEquals("dennis", memcachedClient.get("name"));
    assertTrue(memcachedClient.delete("name", 2000L));
    assertNull(memcachedClient.get("name"));

    // add,replace success
    assertTrue(memcachedClient.add("name", 0, "zhuang"));
    assertTrue(memcachedClient.replace("name", 0, "zhuang"));
  }
コード例 #6
0
ファイル: MemcachedTest.java プロジェクト: huaj12/weguan
 @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));
 }
コード例 #7
0
  public void testSanitizeKey() throws Exception {
    memcachedClient.setSanitizeKeys(true);

    String key = "The string 眉@foo-bar";
    assertTrue(memcachedClient.add(key, 0, 0));
    assertEquals(0, memcachedClient.get(key));

    assertTrue(memcachedClient.replace(key, 0, 1));
    assertEquals(1, memcachedClient.get(key, 2000));

    assertTrue(memcachedClient.set(key, 0, 2));
    assertEquals((Integer) 2, memcachedClient.get(key, 2000, new IntegerTranscoder()));

    assertTrue(memcachedClient.set(key, 0, "xmemcached", new StringTranscoder()));
    assertTrue(memcachedClient.append(key, " great"));
    assertTrue(memcachedClient.prepend(key, "hello "));

    assertEquals("hello xmemcached great", memcachedClient.get(key));

    // test bulk get
    List<String> keys = new ArrayList<String>();
    for (int i = 0; i < 100; i++) {
      memcachedClient.add(key + i, 0, i);
      keys.add(key + i);
    }

    Map<String, Integer> result = memcachedClient.get(keys, 5000);
    for (int i = 0; i < 100; i++) {
      assertEquals((Integer) i, result.get(key + i));
    }

    for (int i = 0; i < 100; i++) {
      assertTrue(memcachedClient.delete(key + i));
      assertNull(memcachedClient.get(key + i));
    }

    // test cas
    memcachedClient.set(key, 0, 1);
    memcachedClient.cas(
        key,
        new CASOperation<Integer>() {

          public int getMaxTries() {
            return 1;
          }

          public Integer getNewValue(long currentCAS, Integer currentValue) {
            return currentValue + 1;
          }
        });
    assertEquals((Integer) 2, memcachedClient.get(key, 2000, new IntegerTranscoder()));
  }
コード例 #8
0
  public void testDecr() throws Exception {
    assertEquals(0, memcachedClient.decr("a", 5));

    assertTrue(memcachedClient.set("a", 0, "100"));
    assertEquals(50, memcachedClient.decr("a", 50));
    assertEquals(46, memcachedClient.decr("a", 4));

    // test decr with initValue
    memcachedClient.delete("a");
    assertEquals(100, memcachedClient.decr("a", 5, 100));
    assertEquals(50, memcachedClient.decr("a", 50));
    assertEquals(46, memcachedClient.decr("a", 4));

    // test decr with initValue and expire time
    memcachedClient.delete("a");
    assertEquals(1, memcachedClient.decr("a", 5, 1, 1000, 1));
    Thread.sleep(2000);
    assertNull(memcachedClient.get("a"));

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

    // invalid key
    new InValidKeyChecker() {
      @Override
      public void call() throws Exception {
        memcachedClient.decr("test\r\n", 0, 1);
      }
    }.check();
    new InValidKeyChecker() {
      @Override
      public void call() throws Exception {
        memcachedClient.decr("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.decr(sb.toString(), 0, 1);
      }
    }.check();
  }
コード例 #9
0
  public void testAppendPrepend() throws Exception {
    // append,prepend
    assertTrue(memcachedClient.set("name", 0, "dennis", new StringTranscoder(), 1000));
    assertTrue(memcachedClient.prepend("name", "hello "));
    assertEquals("hello dennis", memcachedClient.get("name"));
    assertTrue(memcachedClient.append("name", " zhuang"));
    assertEquals("hello dennis zhuang", memcachedClient.get("name"));
    memcachedClient.delete("name");
    assertFalse(memcachedClient.prepend("name", "hello ", 2000));
    assertFalse(memcachedClient.append("name", " zhuang", 2000));
    // append test
    // blank key
    new BlankKeyChecker() {
      @Override
      public void call() throws Exception {
        memcachedClient.append("", 0, 1);
      }
    }.check();
    // null key
    new BlankKeyChecker() {
      @Override
      public void call() throws Exception {
        memcachedClient.append((String) null, 0, 1);
      }
    }.check();

    // invalid key
    new InValidKeyChecker() {
      @Override
      public void call() throws Exception {
        memcachedClient.append("test\r\n", 0, 1);
      }
    }.check();
    new InValidKeyChecker() {
      @Override
      public void call() throws Exception {
        memcachedClient.append("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.append(sb.toString(), 0, 1);
      }
    }.check();

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

    // invalid key
    new InValidKeyChecker() {
      @Override
      public void call() throws Exception {
        memcachedClient.prepend("test\r\n", 0, 1);
      }
    }.check();
    new InValidKeyChecker() {
      @Override
      public void call() throws Exception {
        memcachedClient.prepend("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.prepend(sb.toString(), 0, 1);
      }
    }.check();
  }
コード例 #10
0
ファイル: MemcachedTest.java プロジェクト: huaj12/weguan
 @After
 public void tearDown() throws Exception {
   memcachedClient.delete(key);
   memcachedClient.shutdown();
 }