net.spy.memcached.CASResponse Java Examples

The following examples show how to use net.spy.memcached.CASResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GemcachedDevelopmentJUnitTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public void testCas() throws Exception {
  MemcachedClient client = bootstrapClient();
  client.add("caskey", 10, "casValue").get();
  CASValue<Object> val = client.gets("caskey");
  assertEquals("casValue", val.getValue());
  CASResponse r = client.cas("caskey", val.getCas(), "newValue");
  assertEquals(CASResponse.OK, r);
  r = client.cas("caskey", val.getCas(), "newValue2");
  assertEquals(CASResponse.EXISTS, r);
}
 
Example #2
Source File: GemcachedDevelopmentJUnitTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public void testCas() throws Exception {
  MemcachedClient client = bootstrapClient();
  client.add("caskey", 10, "casValue").get();
  CASValue<Object> val = client.gets("caskey");
  assertEquals("casValue", val.getValue());
  CASResponse r = client.cas("caskey", val.getCas(), "newValue");
  assertEquals(CASResponse.OK, r);
  r = client.cas("caskey", val.getCas(), "newValue2");
  assertEquals(CASResponse.EXISTS, r);
}
 
Example #3
Source File: MemcacheClient.java    From ob1k with Apache License 2.0 5 votes vote down vote up
@Override
public ComposableFuture<Boolean> setAsync(final K key, final EntryMapper<K, V> mapper, final int maxIterations) {
  return casUpdate(key, mapper).flatMap(result -> {
    if (result == CASResponse.OK || result == CASResponse.OBSERVE_MODIFIED) {
      return fromValue(true);
    }

    if (maxIterations > 0 && shouldRetry(result)) {
      return setAsync(key, mapper, maxIterations - 1);
    }

    return fromValue(false);
  });
}
 
Example #4
Source File: MemcacheClient.java    From ob1k with Apache License 2.0 5 votes vote down vote up
private ComposableFuture<CASResponse> casUpdate(final K key, final EntryMapper<K, V> mapper) {
  try {
    final String cacheKey = keyTranslator.translateKey(key);
    final ComposableFuture<CASValue<V>> getFutureValue = SpyFutureHelper.fromCASValue(() -> {
      @SuppressWarnings("unchecked")
      final Transcoder<V> transcoder = (Transcoder<V>) spyClient.getTranscoder();
      return spyClient.asyncGets(cacheKey, transcoder);
    });

    return getFutureValue.flatMap(result -> {
      final V newValue = result == null ? mapper.map(key, null) : mapper.map(key, result.getValue());
      if (newValue == null) {
        return fromValue(CASResponse.OBSERVE_ERROR_IN_ARGS);
      }

      if (result != null) {
        return SpyFutureHelper.fromCASResponse(() -> spyClient.asyncCAS(cacheKey, result.getCas(), newValue));

      } else {
        final ComposableFuture<Boolean> addResponse = SpyFutureHelper.fromOperation(
          () -> spyClient.add(cacheKey, expirationSpyUnits, newValue));

        return addResponse.map(result1 -> {
          if (result1 == Boolean.TRUE) {
            return CASResponse.OK;
          } else {
            return CASResponse.EXISTS;
          }
        });
      }
    });
  } catch (final Exception e) {
    return fromError(e);
  }
}
 
Example #5
Source File: MemcacheClient.java    From ob1k with Apache License 2.0 4 votes vote down vote up
private boolean shouldRetry(final CASResponse result) {
  return result == CASResponse.EXISTS || result == CASResponse.NOT_FOUND;
}
 
Example #6
Source File: SpyFutureHelper.java    From ob1k with Apache License 2.0 votes vote down vote up
Future<CASResponse> createFuture();