net.spy.memcached.CASValue Java Examples

The following examples show how to use net.spy.memcached.CASValue. 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: MemCachePeer.java    From seldon-server with Apache License 2.0 6 votes vote down vote up
public static CASValue gets(String key)
{
	MemcachedClient client = getClient();
	if (client != null)
	{
	try
	{
		return client.gets(hashKey(key));
	}
	catch (Exception ex)
	{
		logger.error("Memcache get exeption ",ex);
		return null;
	}
	}
	else
		return null;
}
 
Example #2
Source File: GemcachedDevelopmentJUnitTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public void testGets() throws Exception {
  MemcachedClient client = bootstrapClient();
  client.add("getskey", 10, "casValue").get();
  CASValue<Object> val = client.gets("getskey");
  long oldCas = val.getCas();
  assertEquals("casValue", val.getValue());
  client.replace("getskey", 10, "myNewVal").get();
  val = client.gets("getskey");
  assertEquals(oldCas + 1, val.getCas());
  assertEquals("myNewVal", val.getValue());
}
 
Example #3
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 #4
Source File: GemcachedDevelopmentJUnitTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public void testGets() throws Exception {
  MemcachedClient client = bootstrapClient();
  client.add("getskey", 10, "casValue").get();
  CASValue<Object> val = client.gets("getskey");
  long oldCas = val.getCas();
  assertEquals("casValue", val.getValue());
  client.replace("getskey", 10, "myNewVal").get();
  val = client.gets("getskey");
  assertEquals(oldCas + 1, val.getCas());
  assertEquals("myNewVal", val.getValue());
}
 
Example #5
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 #6
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 #7
Source File: SpyFutureHelper.java    From ob1k with Apache License 2.0 votes vote down vote up
Future<CASValue<T>> createFuture();