public void testPrependWithoutCAS() throws Exception {
   final String key = "prepend.key";
   assertTrue(client.set(key, 5, "test").get());
   OperationFuture<Boolean> op = client.prepend(key, "es");
   assertTrue(op.get());
   assert op.getStatus().isSuccess();
   assertEquals("estest", client.get(key));
 }
 public static void setSpyCache(
     net.spy.memcached.MemcachedClient mcc, String key, int exp, Object value) {
   OperationFuture<Boolean> set = mcc.set(key, 0, key);
   try {
     if (!set.get(10, TimeUnit.SECONDS)) System.err.println(key + " set not ok");
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
 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();
 }
  /**
   * Unlock the given key asynchronously from the cache.
   *
   * @param key the key to unlock
   * @param casId the CAS identifier
   * @param tc the transcoder to serialize and unserialize value
   * @return whether or not the operation was performed
   * @throws IllegalStateException in the rare circumstance where queue is too full to accept any
   *     more requests
   */
  public <T> OperationFuture<Boolean> asyncUnlock(
      final String key, long casId, final Transcoder<T> tc) {
    final CountDownLatch latch = new CountDownLatch(1);
    final OperationFuture<Boolean> rv = new OperationFuture<Boolean>(key, latch, operationTimeout);
    Operation op =
        opFact.unlock(
            key,
            casId,
            new OperationCallback() {

              @Override
              public void receivedStatus(OperationStatus s) {
                rv.set(s.isSuccess(), s);
              }

              @Override
              public void complete() {
                latch.countDown();
              }
            });
    rv.setOperation(op);
    mconn.enqueueOperation(key, op);
    return rv;
  }
Пример #5
0
  public boolean remember(String key, Serializable object) {

    if (memcache != null) {

      log.debug("Storing object as {}", key);
      OperationFuture<Boolean> future = null;

      if (memcache.get(key) == null) {
        log.debug("using memcache.add(..)");
        future = memcache.add(key, expireTime, object);
      } else {
        log.debug("Using memcache.set(..)");
        future = memcache.set(key, expireTime, object);
      }

      try {
        return future.get(1, TimeUnit.SECONDS);
      } catch (Exception e) {
        log.error("Failed to store value for key {}: {}", key, e.getMessage());
      }
    }

    return false;
  }
 /**
  * Set a value and Observe.
  *
  * @param key the key to set
  * @param exp the Expiry value
  * @param value the Key value
  * @param req the Persistence to Master value
  * @param rep the Persistence to Replicas
  * @return whether or not the operation was performed
  */
 public OperationFuture<Boolean> set(
     String key, int exp, String value, PersistTo req, ReplicateTo rep) {
   OperationFuture<Boolean> setOp = set(key, exp, value);
   try {
     if (setOp.get()) {
       observePoll(key, setOp.getCas(), req, rep);
     }
   } catch (InterruptedException e) {
     setOp.set(false, setOp.getStatus());
   } catch (ExecutionException e) {
     setOp.set(false, setOp.getStatus());
   } catch (TimeoutException e) {
     setOp.set(false, setOp.getStatus());
   } catch (IllegalArgumentException e) {
     setOp.set(false, setOp.getStatus());
   } catch (RuntimeException e) {
     setOp.set(false, setOp.getStatus());
   }
   return (setOp);
 }
 /**
  * Delete a value and Observe.
  *
  * @param key the key to set
  * @param req the Persistence to Master value
  * @param rep the Persistence to Replicas
  * @return whether or not the operation was performed
  */
 public OperationFuture<Boolean> delete(String key, PersistTo req, ReplicateTo rep) {
   OperationFuture<Boolean> deleteOp = delete(key);
   try {
     deleteOp.get();
     deleteOp.set(true, deleteOp.getStatus());
     observePoll(key, 0L, req, rep);
   } catch (InterruptedException e) {
     deleteOp.set(false, deleteOp.getStatus());
   } catch (ExecutionException e) {
     deleteOp.set(false, deleteOp.getStatus());
   } catch (TimeoutException e) {
     deleteOp.set(false, deleteOp.getStatus());
   } catch (IllegalArgumentException e) {
     deleteOp.set(false, deleteOp.getStatus());
   } catch (RuntimeException e) {
     deleteOp.set(false, deleteOp.getStatus());
   }
   return (deleteOp);
 }
  public void testSetReturnsCAS() throws Exception {

    OperationFuture<Boolean> setOp = client.set("testSetReturnsCAS", 0, "testSetReturnsCAS");
    setOp.get();
    assertTrue(setOp.getCas() > 0);
  }
Пример #9
0
 @Test
 public void saveTest() throws InterruptedException, ExecutionException {
   OperationFuture<Boolean> valFuture = client.set("Test_Key", 200, "Test Value");
   assertEquals(true, valFuture.get().booleanValue());
 }