Java源码示例:net.spy.memcached.internal.OperationCompletionListener
示例1
public <T> Future<Boolean> appendOrAdd(String key, CachedData value, int timeToLive, EVCacheLatch evcacheLatch) throws Exception {
final MemcachedNode node = evcacheMemcachedClient.getEVCacheNode(key);
if (!ensureWriteQueueSize(node, key, Call.APPEND_OR_ADD)) {
if (log.isInfoEnabled()) log.info("Node : " + node + " is not active. Failing fast and dropping the write event.");
final ListenableFuture<Boolean, OperationCompletionListener> defaultFuture = (ListenableFuture<Boolean, OperationCompletionListener>) getDefaultFuture();
if (evcacheLatch != null && evcacheLatch instanceof EVCacheLatchImpl && !isInWriteOnly()) ((EVCacheLatchImpl) evcacheLatch).addFuture(defaultFuture);
return defaultFuture;
}
try {
return evcacheMemcachedClient.asyncAppendOrAdd(key, timeToLive, value, evcacheLatch);
} catch (Exception e) {
log.error(e.getMessage(), e);
throw e;
}
}
示例2
public void testSetWithCallback() throws Exception {
OperationFuture<Boolean> setOp =
client.set("setWithCallback", 0, "content");
final CountDownLatch latch = new CountDownLatch(1);
setOp.addListener(new OperationCompletionListener() {
@Override
public void onComplete(OperationFuture<?> f) throws Exception {
latch.countDown();
}
});
assertTrue(latch.await(2, TimeUnit.SECONDS));
}
示例3
private Future<Boolean> _replace(String key, CachedData value, int timeToLive, EVCacheLatch evcacheLatch) throws Exception {
final MemcachedNode node = evcacheMemcachedClient.getEVCacheNode(key);
if (!ensureWriteQueueSize(node, key, Call.REPLACE)) {
if (log.isInfoEnabled()) log.info("Node : " + node + " is not active. Failing fast and dropping the replace event.");
final ListenableFuture<Boolean, OperationCompletionListener> defaultFuture = (ListenableFuture<Boolean, OperationCompletionListener>) getDefaultFuture();
if (evcacheLatch != null && evcacheLatch instanceof EVCacheLatchImpl && !isInWriteOnly()) ((EVCacheLatchImpl) evcacheLatch).addFuture(defaultFuture);
return defaultFuture;
}
try {
final int dataSize = ((CachedData) value).getData().length;
if (enableChunking.get() && dataSize > chunkSize.get()) {
final CachedData[] cd = createChunks(value, key);
final int len = cd.length;
final OperationFuture<Boolean>[] futures = new OperationFuture[len];
for (int i = 0; i < cd.length; i++) {
final String prefix = (i < 10) ? "0" : "";
futures[i] = evcacheMemcachedClient.replace(key + "_" + prefix + i, timeToLive, cd[i], null, null);
}
return new EVCacheFutures(futures, key, appName, serverGroup, evcacheLatch);
} else if(shouldHashKey()) {
final String hKey = getHashedKey(key);
final CachedData cVal = getEVCacheValue(key, value, timeToLive);
return evcacheMemcachedClient.replace(hKey, timeToLive, cVal, null, evcacheLatch);
} else {
return evcacheMemcachedClient.replace(key, timeToLive, value, null, evcacheLatch);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
throw e;
}
}
示例4
public <T> Future<Boolean> touch(String key, int timeToLive, EVCacheLatch latch) throws Exception {
if(ignoreTouch.get()) {
final ListenableFuture<Boolean, OperationCompletionListener> sf = new SuccessFuture();
if (latch != null && latch instanceof EVCacheLatchImpl && !isInWriteOnly()) ((EVCacheLatchImpl) latch).addFuture(sf);
return sf;
}
final MemcachedNode node = evcacheMemcachedClient.getEVCacheNode(key);
if (!ensureWriteQueueSize(node, key, Call.TOUCH)) {
final ListenableFuture<Boolean, OperationCompletionListener> defaultFuture = (ListenableFuture<Boolean, OperationCompletionListener>) getDefaultFuture();
if (latch != null && latch instanceof EVCacheLatchImpl && !isInWriteOnly()) ((EVCacheLatchImpl) latch).addFuture(defaultFuture);
return defaultFuture;
}
if (enableChunking.get()) {
final ChunkDetails<?> cd = getChunkDetails(key);
if (cd.isChunked()) {
final List<String> keys = cd.getChunkKeys();
OperationFuture<Boolean>[] futures = new OperationFuture[keys.size() + 1];
futures[0] = evcacheMemcachedClient.touch(key + "_00", timeToLive, latch);
for (int i = 0; i < keys.size(); i++) {
final String prefix = (i < 10) ? "0" : "";
final String _key = key + "_" + prefix + i;
futures[i + 1] = evcacheMemcachedClient.touch(_key, timeToLive, latch);
}
return new EVCacheFutures(futures, key, appName, serverGroup, latch);
} else {
return evcacheMemcachedClient.touch(key, timeToLive, latch);
}
} else if(shouldHashKey()) {
final String hKey = getHashedKey(key);
return evcacheMemcachedClient.touch(hKey, timeToLive, latch);
} else {
return evcacheMemcachedClient.touch(key, timeToLive, latch);
}
}
示例5
public Future<Boolean> delete(String key, EVCacheLatch latch) throws Exception {
final MemcachedNode node = evcacheMemcachedClient.getEVCacheNode(key);
if (!ensureWriteQueueSize(node, key, Call.DELETE)) {
final ListenableFuture<Boolean, OperationCompletionListener> defaultFuture = (ListenableFuture<Boolean, OperationCompletionListener>) getDefaultFuture();
if (latch != null && latch instanceof EVCacheLatchImpl && !isInWriteOnly()) ((EVCacheLatchImpl) latch).addFuture(defaultFuture);
return defaultFuture;
}
if (enableChunking.get()) {
final ChunkDetails<?> cd = getChunkDetails(key);
if (cd == null) {
// Paranoid delete : cases where get fails and we ensure the first key is deleted just in case
return evcacheMemcachedClient.delete(key + "_00", latch);
}
if (!cd.isChunked()) {
return evcacheMemcachedClient.delete(key, latch);
} else {
final List<String> keys = cd.getChunkKeys();
OperationFuture<Boolean>[] futures = new OperationFuture[keys.size() + 1];
futures[0] = evcacheMemcachedClient.delete(key + "_00");
for (int i = 0; i < keys.size(); i++) {
futures[i + 1] = evcacheMemcachedClient.delete(keys.get(i), null);
}
return new EVCacheFutures(futures, key, appName, serverGroup, latch);
}
} else if(shouldHashKey()) {
final String hKey = getHashedKey(key);
return evcacheMemcachedClient.delete(hKey, latch);
} else {
return evcacheMemcachedClient.delete(key, latch);
}
}
示例6
private Future<Boolean> _set(String key, CachedData value, int timeToLive, EVCacheLatch evcacheLatch) throws Exception {
final MemcachedNode node = evcacheMemcachedClient.getEVCacheNode(key);
if (!ensureWriteQueueSize(node, key, Call.SET)) {
if (log.isInfoEnabled()) log.info("Node : " + node + " is not active. Failing fast and dropping the write event.");
final ListenableFuture<Boolean, OperationCompletionListener> defaultFuture = (ListenableFuture<Boolean, OperationCompletionListener>) getDefaultFuture();
if (evcacheLatch != null && evcacheLatch instanceof EVCacheLatchImpl && !isInWriteOnly()) ((EVCacheLatchImpl) evcacheLatch).addFuture(defaultFuture);
return defaultFuture;
}
try {
final int dataSize = ((CachedData) value).getData().length;
if (enableChunking.get()) {
if (dataSize > chunkSize.get()) {
final CachedData[] cd = createChunks(value, key);
final int len = cd.length;
final OperationFuture<Boolean>[] futures = new OperationFuture[len];
for (int i = 0; i < cd.length; i++) {
final String prefix = (i < 10) ? "0" : "";
futures[i] = evcacheMemcachedClient.set(key + "_" + prefix + i, timeToLive, cd[i], null, null);
}
// ensure we are deleting the unchunked key if it exists.
// Ignore return value since it may not exist.
evcacheMemcachedClient.delete(key);
return new EVCacheFutures(futures, key, appName, serverGroup, evcacheLatch);
} else {
// delete all the chunks if they exist as the
// data is moving from chunked to unchunked
delete(key);
return evcacheMemcachedClient.set(key, timeToLive, value, null, evcacheLatch);
}
} else if(shouldHashKey()) {
final String hKey = getHashedKey(key);
final CachedData cVal = getEVCacheValue(key, value, timeToLive);
return evcacheMemcachedClient.set(hKey, timeToLive, cVal, null, evcacheLatch);
} else {
return evcacheMemcachedClient.set(key, timeToLive, value, null, evcacheLatch);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
throw e;
}
}
示例7
@Override
public Future<Boolean> addListener(OperationCompletionListener listener) {
return this;
}
示例8
@Override
public Future<Boolean> removeListener(OperationCompletionListener listener) {
return this;
}
示例9
@Override
public Future<Boolean> addListener(OperationCompletionListener listener) {
return this;
}
示例10
@Override
public Future<Boolean> removeListener(OperationCompletionListener listener) {
return this;
}
示例11
public void addFuture(ListenableFuture<Boolean, OperationCompletionListener> future) {
future.addListener(this);
if (future.isDone()) countDown();
this.futures.add(future);
}
示例12
@Override
public Future<Boolean> addListener(OperationCompletionListener listener) {
return this;
}
示例13
@Override
public Future<Boolean> removeListener(OperationCompletionListener listener) {
return this;
}