net.spy.memcached.internal.OperationCompletionListener Java Examples

The following examples show how to use net.spy.memcached.internal.OperationCompletionListener. 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: EVCacheClient.java    From EVCache with Apache License 2.0 6 votes vote down vote up
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;
    }
}
 
Example #2
Source File: ProtocolBaseTestFile.java    From KodeBeagle with Apache License 2.0 5 votes vote down vote up
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));
}
 
Example #3
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
Example #4
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example #5
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example #6
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 4 votes vote down vote up
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;
    }
}
 
Example #7
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 4 votes vote down vote up
@Override
public Future<Boolean> addListener(OperationCompletionListener listener) {
    return this;
}
 
Example #8
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 4 votes vote down vote up
@Override
public Future<Boolean> removeListener(OperationCompletionListener listener) {
    return this;
}
 
Example #9
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 4 votes vote down vote up
@Override
public Future<Boolean> addListener(OperationCompletionListener listener) {
    return this;
}
 
Example #10
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 4 votes vote down vote up
@Override
public Future<Boolean> removeListener(OperationCompletionListener listener) {
    return this;
}
 
Example #11
Source File: EVCacheLatchImpl.java    From EVCache with Apache License 2.0 4 votes vote down vote up
public void addFuture(ListenableFuture<Boolean, OperationCompletionListener> future) {
    future.addListener(this);
    if (future.isDone()) countDown();
    this.futures.add(future);
}
 
Example #12
Source File: EVCacheFutures.java    From EVCache with Apache License 2.0 4 votes vote down vote up
@Override
public Future<Boolean> addListener(OperationCompletionListener listener) {
    return this;
}
 
Example #13
Source File: EVCacheFutures.java    From EVCache with Apache License 2.0 4 votes vote down vote up
@Override
public Future<Boolean> removeListener(OperationCompletionListener listener) {
    return this;
}