예제 #1
0
 public Serializable recall(String key) {
   if (memcache != null) {
     try {
       GetFuture<Object> future = memcache.asyncGet(key);
       return (Serializable) future.get(1, TimeUnit.SECONDS);
     } catch (Exception e) {
       log.error("Cache access timed out: {}", e.getMessage());
       return null;
     }
   }
   return null;
 }
  public void testSyncGetTimeouts() throws Exception {
    final String key = "timeoutTestKey";
    final String value = "timeoutTestValue";

    int j = 0;
    boolean set = false;

    // Do not execute this for CI
    if (TestConfig.isCITest()) {
      return;
    }

    do {
      set = client.set(key, 0, value).get();
      j++;
    } while (!set && j < 10);
    assert set;

    // Shutting down the default client to get one with a short timeout.
    assertTrue("Couldn't shut down within five seconds", client.shutdown(5, TimeUnit.SECONDS));

    syncGetTimeoutsInitClient();
    Thread.sleep(100); // allow connections to be established

    int i = 0;
    GetFuture<Object> g = null;
    try {
      for (i = 0; i < 1000000; i++) {
        g = client.asyncGet(key);
        g.get();
      }
      throw new Exception("Didn't get a timeout.");
    } catch (Exception e) {
      assert !g.getStatus().isSuccess();
      System.err.println("Got a timeout at iteration " + i + ".");
    }
    Thread.sleep(100); // let whatever caused the timeout to pass
    try {
      if (value.equals(client.asyncGet(key).get(30, TimeUnit.SECONDS))) {
        System.err.println("Got the right value.");
      } else {
        throw new Exception("Didn't get the expected value.");
      }
    } catch (java.util.concurrent.TimeoutException timeoutException) {
      debugNodeInfo(client.getNodeLocator().getAll());
      throw new Exception("Unexpected timeout after 30 seconds waiting", timeoutException);
    }
  }
 public void testABunchOfCancelledOperations() throws Exception {
   final String k = "bunchOCancel";
   Collection<Future<?>> futures = new ArrayList<Future<?>>();
   for (int i = 0; i < 1000; i++) {
     futures.add(client.set(k, 5, "xval"));
     futures.add(client.asyncGet(k));
   }
   OperationFuture<Boolean> sf = client.set(k, 5, "myxval");
   GetFuture<Object> gf = client.asyncGet(k);
   for (Future<?> f : futures) {
     f.cancel(true);
   }
   assertTrue(sf.get());
   assert sf.getStatus().isSuccess();
   assertEquals("myxval", gf.get());
   assert gf.getStatus().isSuccess();
 }
예제 #4
0
 @Override
 public String get(String key) {
   if (StringTools.isEmpty(key) || !enable) {
     return "";
   }
   GetFuture<Object> result = client.asyncGet(buildKey(key));
   if (result != null) {
     try {
       return (String) result.get(3, TimeUnit.SECONDS);
     } catch (InterruptedException e) {
       logger.error("Memcached asyncGet InterruptedException:", e);
     } catch (ExecutionException e) {
       logger.error("Memcached asyncGet ExecutionException:", e);
     } catch (TimeoutException e) {
       logger.error("Memcached asyncGet TimeoutException:", e);
     }
   }
   return "";
 }