Beispiel #1
0
 public <T> GetsResponse<T> getResponse(String key) {
   try {
     net.rubyeye.xmemcached.GetsResponse item = this._client.gets(key);
     if (item == null) {
       return null;
     }
     return new GetsResponse(item.getCas(), item.getValue());
   } catch (MemcachedException ex) {
     return null;
   } catch (InterruptedException e) {
     return null;
   } catch (TimeoutException e) {
   }
   return null;
 }
  public void testCAS() throws Exception {
    memcachedClient.add("name", 0, "dennis");
    GetsResponse<String> getsResponse = memcachedClient.gets("name");
    assertEquals("dennis", getsResponse.getValue());
    final CASOperation<String> operation =
        new CASOperation<String>() {

          public int getMaxTries() {
            return 1;
          }

          public String getNewValue(long currentCAS, String currentValue) {
            return "zhuang";
          }
        };
    assertTrue(memcachedClient.cas("name", getsResponse, operation));
    assertEquals("zhuang", memcachedClient.get("name"));
    getsResponse = memcachedClient.gets("name");
    memcachedClient.set("name", 0, "dennis");
    // cas fail
    assertFalse(memcachedClient.cas("name", 0, "zhuang", getsResponse.getCas()));
    assertEquals("dennis", memcachedClient.get("name"));

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

    // invalid key
    new InValidKeyChecker() {
      @Override
      public void call() throws Exception {
        memcachedClient.cas("test\r\n", operation);
      }
    }.check();
    new InValidKeyChecker() {
      @Override
      public void call() throws Exception {
        memcachedClient.cas("test test2", operation);
      }
    }.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.cas(sb.toString(), operation);
      }
    }.check();
  }
  public void testGets() throws Exception {
    memcachedClient.add("name", 0, "dennis");
    GetsResponse<String> getsResponse = memcachedClient.gets("name");
    GetsResponse<String> oldGetsResponse = getsResponse;
    assertEquals("dennis", getsResponse.getValue());
    long oldCas = getsResponse.getCas();
    getsResponse = memcachedClient.gets("name", 2000, new StringTranscoder());
    assertEquals("dennis", getsResponse.getValue());
    // check the same
    assertEquals(oldCas, getsResponse.getCas());
    assertEquals(oldGetsResponse, getsResponse);

    memcachedClient.set("name", 0, "zhuang");
    getsResponse = memcachedClient.gets("name", 2000);
    assertEquals("zhuang", getsResponse.getValue());
    assertFalse(oldCas == getsResponse.getCas());

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

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

    // client is shutdown
    try {
      memcachedClient.shutdown();
      memcachedClient.gets("name");
      fail();
    } catch (MemcachedException e) {
      assertEquals("Xmemcached is stopped", e.getMessage());
    }
  }