本文整理汇总了Java中net.spy.memcached.CASResponse类的典型用法代码示例。如果您正苦于以下问题:Java CASResponse类的具体用法?Java CASResponse怎么用?Java CASResponse使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CASResponse类属于net.spy.memcached包,在下文中一共展示了CASResponse类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testAsyncGets
import net.spy.memcached.CASResponse; //导入依赖的package包/类
@Test
public void testAsyncGets() throws Exception {
String value = UUID.randomUUID().toString();
Boolean didSet = memcacheClient.set("key", 0, value).get();
assertTrue(didSet);
Snapshot expectedValues = tracker.snapshot();
expectedValues.increment("get");
CASValue<Object> casValue = memcacheClient.asyncGets("key").get();
assertEquals(value, casValue.getValue());
tracker.validate(expectedValues);
String value1 = UUID.randomUUID().toString();
CASResponse response = memcacheClient.cas("key", casValue.getCas(), value1);
assertEquals(CASResponse.OK, response);
}
示例2: testGets
import net.spy.memcached.CASResponse; //导入依赖的package包/类
@Test
public void testGets() throws Exception {
String value = UUID.randomUUID().toString();
Boolean didSet = memcacheClient.set("key", 0, value).get();
assertTrue(didSet);
Snapshot expectedValues = tracker.snapshot();
expectedValues.increment("get");
CASValue<Object> casValue = memcacheClient.gets("key");
assertEquals(value, casValue.getValue());
tracker.validate(expectedValues);
String value1 = UUID.randomUUID().toString();
CASResponse response = memcacheClient.cas("key", casValue.getCas(), value1);
assertEquals(CASResponse.OK, response);
}
示例3: testAsyncCas
import net.spy.memcached.CASResponse; //导入依赖的package包/类
@Test
public void testAsyncCas() throws Exception {
String value = UUID.randomUUID().toString();
OperationFuture<Boolean> setFuture = memcacheClient.set("key", 0, value);
Boolean didSet = setFuture.get();
assertTrue(didSet);
Long cas = setFuture.getCas();
Snapshot expectedValues = tracker.snapshot();
expectedValues.increment("set");
String value1 = UUID.randomUUID().toString();
CASResponse response = memcacheClient.asyncCAS("key", cas, value1).get();
assertEquals(CASResponse.OK, response);
tracker.validate(expectedValues);
Object val = memcacheClient.get("key");
assertEquals(value1, val);
}
示例4: testCas
import net.spy.memcached.CASResponse; //导入依赖的package包/类
@Test
public void testCas() throws Exception {
String value = UUID.randomUUID().toString();
OperationFuture<Boolean> setFuture = memcacheClient.set("key", 0, value);
Boolean didSet = setFuture.get();
assertTrue(didSet);
Long cas = setFuture.getCas();
Snapshot expectedValues = tracker.snapshot();
expectedValues.increment("set");
String value1 = UUID.randomUUID().toString();
CASResponse response = memcacheClient.cas("key", cas, value1);
assertEquals(CASResponse.OK, response);
tracker.validate(expectedValues);
Object val = memcacheClient.get("key");
assertEquals(value1, val);
}
示例5: testAsyncGetAndTouch
import net.spy.memcached.CASResponse; //导入依赖的package包/类
@Test
public void testAsyncGetAndTouch() throws Exception {
if (serverSupportsTouch()) {
return;
}
String value = UUID.randomUUID().toString();
Boolean didSet = memcacheClient.set("key", 0, value).get();
assertTrue(didSet);
Snapshot expectedValues = tracker.snapshot();
expectedValues.increment("gat");
CASValue<Object> casValue = memcacheClient.getAndTouch("key", 0);
assertEquals(value, casValue.getValue());
tracker.validate(expectedValues);
String value1 = UUID.randomUUID().toString();
CASResponse response = memcacheClient.cas("key", casValue.getCas(), value1);
assertEquals(CASResponse.OK, response);
}
示例6: testGetAndTouch
import net.spy.memcached.CASResponse; //导入依赖的package包/类
@Test
public void testGetAndTouch() throws Exception {
if (serverSupportsTouch()) {
return;
}
String value = UUID.randomUUID().toString();
Boolean didSet = memcacheClient.set("key", 0, value).get();
assertTrue(didSet);
Snapshot expectedValues = tracker.snapshot();
expectedValues.increment("gat");
CASValue<Object> casValue = memcacheClient.getAndTouch("key", 0);
assertEquals(value, casValue.getValue());
tracker.validate(expectedValues);
String value1 = UUID.randomUUID().toString();
CASResponse response = memcacheClient.cas("key", casValue.getCas(), value1);
assertEquals(CASResponse.OK, response);
}
示例7: testMemcachedException
import net.spy.memcached.CASResponse; //导入依赖的package包/类
@Test(groups = "unit")
public void testMemcachedException() {
MemcachedClientIF failingMemcachedClient = mock(MemcachedClientIF.class);
when(failingMemcachedClient.gets(anyString())).thenThrow(
new RuntimeException("Exception waiting for value",
new ExecutionException("Operation timed out", null)));
when(failingMemcachedClient.cas(anyString(), anyLong(), anyObject()))
.thenThrow(
new RuntimeException("Exception waiting for value",
new ExecutionException("Operation timed out",
null)));
MemcachedCacheService failingCacheService = new MemcachedCacheService(
failingMemcachedClient);
CASValue<Object> casValue = failingCacheService.gets(TEST_KEY);
Assert.assertNull(casValue);
CASResponse casResponse = failingCacheService.cas(TEST_KEY, 0L, "1");
Assert.assertNull(casResponse);
}
示例8: testCas
import net.spy.memcached.CASResponse; //导入依赖的package包/类
@Test(groups = "functional")
public void testCas() {
cacheService.put(FIRST_TEST_KEY, "0");
CASValue<Object> casValue = cacheService.gets(FIRST_TEST_KEY);
CASResponse casResponse = cacheService.cas(FIRST_TEST_KEY,
casValue.getCas(), "1");
Assert.assertEquals(casResponse, CASResponse.OK);
Assert.assertEquals((String) cacheService.get(FIRST_TEST_KEY), "1");
casValue = cacheService.gets(FIRST_TEST_KEY);
cacheService.put(FIRST_TEST_KEY, "2");
casResponse = cacheService.cas(FIRST_TEST_KEY, casValue.getCas(), "3");
Assert.assertEquals(casResponse, CASResponse.EXISTS);
Assert.assertEquals((String) cacheService.get(FIRST_TEST_KEY), "2");
casValue = cacheService.gets(FIRST_TEST_KEY);
casResponse = cacheService.cas(SECOND_TEST_KEY, casValue.getCas(), "4");
Assert.assertEquals(casResponse, CASResponse.NOT_FOUND);
}
示例9: storeInMemcached
import net.spy.memcached.CASResponse; //导入依赖的package包/类
/**
* Tries to update an object value in memcached considering the cas validation
*
* Returns true if the object passed the cas validation and was modified.
*
* @param keyString
* @param value
* @return
*/
private boolean storeInMemcached(String keyString, ObjectWithCas value) {
if (value != null && value.getObject() != null
&& !Serializable.class.isAssignableFrom(value.getObject().getClass())) {
throw new CacheException("Object of type '" + value.getObject().getClass().getName()
+ "' that's non-serializable is not supported by Memcached");
}
CASResponse response;
if (configuration.isCompressionEnabled()) {
response = client.cas(keyString, value.getCas(), value.getObject(), new CompressorTranscoder());
} else {
response = client.cas(keyString, value.getCas(), value.getObject());
}
return (response.equals(CASResponse.OBSERVE_MODIFIED) || response.equals(CASResponse.OK));
}
示例10: testCas
import net.spy.memcached.CASResponse; //导入依赖的package包/类
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);
}
示例11: getStatusForErrorCode
import net.spy.memcached.CASResponse; //导入依赖的package包/类
/**
* Get the OperationStatus object for the given error code.
*
* @param errCode the error code
* @return the status to return, or null if this is an exceptional case
*/
protected OperationStatus getStatusForErrorCode(int errCode, byte[] errPl)
throws IOException {
switch (errCode) {
case SUCCESS:
return STATUS_OK;
case ERR_NOT_FOUND:
return new CASOperationStatus(false, new String(errPl),
CASResponse.NOT_FOUND);
case ERR_EXISTS:
return new CASOperationStatus(false, new String(errPl),
CASResponse.EXISTS);
case ERR_NOT_STORED:
return new CASOperationStatus(false, new String(errPl),
CASResponse.NOT_FOUND);
case ERR_2BIG:
case ERR_INTERNAL:
handleError(OperationErrorType.SERVER, new String(errPl));
case ERR_INVAL:
case ERR_DELTA_BADVAL:
case ERR_NOT_MY_VBUCKET:
case ERR_UNKNOWN_COMMAND:
case ERR_NO_MEM:
case ERR_NOT_SUPPORTED:
case ERR_BUSY:
case ERR_TEMP_FAIL:
return new OperationStatus(false, new String(errPl));
default:
return null;
}
}
示例12: setAsync
import net.spy.memcached.CASResponse; //导入依赖的package包/类
@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 && result == CASResponse.EXISTS) {
return setAsync(key, mapper, maxIterations - 1);
}
return fromValue(false);
});
}
示例13: casUpdate
import net.spy.memcached.CASResponse; //导入依赖的package包/类
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);
}
}
示例14: addAlbum
import net.spy.memcached.CASResponse; //导入依赖的package包/类
@RequestMapping("/addAlbum/{genre}/{album}")
public ResponseEntity<String> addAlbum(final @PathVariable String genre, final @PathVariable String album) throws Exception {
final CASValue<Object> foundGenre = db.gets("genre::" + genre);
if(foundGenre == null) {
return new ResponseEntity<String>("Genre not found", HttpStatus.NOT_FOUND);
}
Album randomAlbum = new Album("System Of A Down", "Toxicity", 15, Arrays.asList(
new Album.Track("Deer Dance", 180),
new Album.Track("Jet Pilot", 170),
new Album.Track("Chop Suey!", 200)
));
final String albumKey = "album::" + album;
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<CASResponse> casResponse = new AtomicReference<CASResponse>();
db.add(albumKey, mapper.writeValueAsString(randomAlbum)).addListener(new OperationCompletionListener() {
@Override
public void onComplete(OperationFuture<?> future) throws Exception {
OperationStatus status = future.getStatus();
if (!status.isSuccess()) {
LOGGER.warn("/addAlbum/{}/{} failed because of {}", genre, album, status.getMessage());
return;
}
Genre convertedGenre = mapper.readValue((String) foundGenre.getValue(), Genre.class);
convertedGenre.getAlbums().add(albumKey);
casResponse.set(db.cas("genre::" + genre, foundGenre.getCas(), mapper.writeValueAsString(convertedGenre)));
latch.countDown();
}
});
latch.await();
if (casResponse.get() == CASResponse.OK) {
return new ResponseEntity<String>(HttpStatus.CREATED);
} else {
LOGGER.warn("/addAlbum/{}/{} failed because of {}", genre, album, casResponse.get());
return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
示例15: updateEntry
import net.spy.memcached.CASResponse; //导入依赖的package包/类
@Override
public void updateEntry(final String url, final HttpCacheUpdateCallback callback)
throws HttpCacheUpdateException, IOException {
int numRetries = 0;
final String key = getCacheKey(url);
if (key == null) {
throw new HttpCacheUpdateException("couldn't generate cache key");
}
do {
try {
final CASValue<Object> v = client.gets(key);
MemcachedCacheEntry mce = (v == null) ? null
: reconstituteEntry(v.getValue());
if (mce != null && (!url.equals(mce.getStorageKey()))) {
mce = null;
}
final HttpCacheEntry existingEntry = (mce == null) ? null
: mce.getHttpCacheEntry();
final HttpCacheEntry updatedEntry = callback.update(existingEntry);
if (existingEntry == null) {
putEntry(url, updatedEntry);
return;
} else {
final byte[] updatedBytes = serializeEntry(url, updatedEntry);
final CASResponse casResult = client.cas(key, v.getCas(),
updatedBytes);
if (casResult != CASResponse.OK) {
numRetries++;
} else {
return;
}
}
} catch (final OperationTimeoutException ex) {
throw new MemcachedOperationTimeoutException(ex);
}
} while (numRetries <= maxUpdateRetries);
throw new HttpCacheUpdateException("Failed to update");
}