public Object get(String key) { Future<Object> future = client.asyncGet(key, tc); try { return future.get(1, TimeUnit.SECONDS); } catch (Exception e) { future.cancel(false); } return null; }
@Override public Status read( String table, String key, Set<String> fields, Map<String, ByteIterator> result) { key = createQualifiedKey(table, key); try { GetFuture<Object> future = memcachedClient().asyncGet(key); Object document = future.get(); if (document != null) { fromJson((String) document, fields, result); } return Status.OK; } catch (Exception e) { logger.error("Error encountered for key: " + key, e); return Status.ERROR; } }
@Before public void setUp() throws Exception { this.createTestMetadata(); keyValueMap = Maps.newHashMap(); keyValueMap.put("sql1", "value1"); keyValueMap.put("sql11", "value11"); MemcachedCacheConfig cacheConfig = new MemcachedCacheConfig(); MemcachedClient memcachedClient = mock(MemcachedClient.class); MemcachedCache memcachedCache = new MemcachedCache(memcachedClient, cacheConfig, CacheConstants.QUERY_CACHE, 7 * 24 * 3600); memCachedAdaptor = new MemCachedCacheAdaptor(memcachedCache); //Mock put to cache for (String key : keyValueMap.keySet()) { String keyS = memcachedCache.serializeKey(key); String hashedKey = memcachedCache.computeKeyHash(keyS); String value = keyValueMap.get(key); byte[] valueE = memcachedCache.encodeValue(keyS, value); GetFuture<Object> future = mock(GetFuture.class); when(future.get(cacheConfig.getTimeout(), TimeUnit.MILLISECONDS)).thenReturn(valueE); when(memcachedClient.asyncGet(hashedKey)).thenReturn(future); } }
@Override protected Cancellable restore(final String storageKey, final FutureCallback<byte[]> callback) { final GetFuture<Object> getFuture = client.asyncGet(storageKey); getFuture.addListener(new GetCompletionListener() { @Override public void onComplete(final GetFuture<?> future) throws Exception { try { callback.completed(castAsByteArray(getFuture.get())); } catch (final ExecutionException ex) { if (ex.getCause() instanceof Exception) { callback.failed((Exception) ex.getCause()); } else { callback.failed(ex); } } } }); return Operations.cancellable(getFuture); }
/** * Get the given key asynchronously and decode with the default * transcoder. * * @param key the key to fetch * @return a future that will hold the return value of the fetch * @throws IllegalStateException in the rare circumstance where queue * is too full to accept any more requests */ public Future<Object> asyncGet(final String key) { return asyncGet(key, transcoder); }
/** * Get the given key asynchronously and decode with the default transcoder. * * @param key the key to fetch * @return a future that will hold the return value of the fetch * @throws IllegalStateException in the rare circumstance where queue is too * full to accept any more requests */ @Override public GetFuture<Object> asyncGet(final String key) { return asyncGet(key, transcoder); }
/** * Get the given key asynchronously and decode with the default * transcoder. * * @param key the key to fetch * @return a future that will hold the return value of the fetch * @throws IllegalStateException in the rare circumstance where queue * is too full to accept any more requests */ public Future<Object> asyncGet(final String key) { return asyncGet(key, transcoder); }
/** * Get the given key asynchronously and decode with the default transcoder. * * @param key the key to fetch * @return a future that will hold the return value of the fetch * @throws IllegalStateException in the rare circumstance where queue is too * full to accept any more requests */ public GetFuture<Object> asyncGet(final String key) { return asyncGet(key, transcoder); }
/** * Get the given key asynchronously and decode with the default transcoder. * * @param key the key to fetch * @return a future that will hold the return value of the fetch * @throws IllegalStateException in the rare circumstance where queue is too * full to accept any more requests */ @Override public GetFuture<Object> asyncGet(final String key) { return asyncGet(key, transcoder); }
/** * Get the given key asynchronously and decode with the default * transcoder. * * @param key the key to fetch * @return a future that will hold the return value of the fetch * @throws IllegalStateException in the rare circumstance where queue * is too full to accept any more requests */ public Future<Object> asyncGet(final String key) { return asyncGet(key, transcoder); }
@Override public <T> T get(String key) { Future<Object> future = client.asyncGet(key, tc); try { return (T) future.get(1, TimeUnit.SECONDS); } catch (Exception e) { future.cancel(false); } return null; }
@Override public Object get(String key) { Future<Object> future = client.asyncGet(key, tc); try { return future.get(1, TimeUnit.SECONDS); } catch (Exception e) { future.cancel(false); } return null; }
@Override protected CacheEntry<V> doGet(String cacheKey) throws Exception { if (cacheKey.length() > MaxKeyLength) { throw new IllegalArgumentException("Key length exceded maximum " + MaxKeyLength); } Future<Object> future = client.asyncGet(cacheKey); try { return (CacheEntry<V>) future.get(operationTimeout, TimeUnit.MILLISECONDS); } catch (CheckedOperationTimeoutException cotx) { logger.warn("GET operation timeout: " + operationTimeout + " millis, Key: " + cacheKey); return null; } }
@Override byte[] getValue(String tenantId, String clusterId) throws IOException { // to prevent continuous retries if the memcache server is down // Try to get a value, for up to 5 seconds, and cancel if it doesn't return Future<Object> f = client.asyncGet(getKey(tenantId, clusterId)); try { return (byte[]) f.get(timeoutSeconds, TimeUnit.SECONDS); } catch (TimeoutException e) { LOG.error("Timed out after {} seconds getting credentials for tenant {} and cluster {} from memcache.", timeoutSeconds, tenantId, clusterId, e); // Since we don't need this, go ahead and cancel the operation. This // is not strictly necessary, but it'll save some work on the server. f.cancel(false); throw new IOException(e); } catch (Exception e) { LOG.error("Exception getting credentials for tenant {} and cluster {} from memcache.", e); throw new IOException(e); } }
/** * Get with a single key. * * @param <T> * @param key the key to get * @param tc the transcoder to serialize and unserialize value * @return the result from the cache (null if there is none) * @throws OperationTimeoutException if the global operation timeout is * exceeded * @throws IllegalStateException in the rare circumstance where queue * is too full to accept any more requests */ public <T> T get(String key, Transcoder<T> tc) { try { return asyncGet(key, tc).get( operationTimeout, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { throw new RuntimeException("Interrupted waiting for value", e); } catch (ExecutionException e) { throw new RuntimeException("Exception waiting for value", e); } catch (TimeoutException e) { throw new OperationTimeoutException("Timeout waiting for value", e); } }
public byte[] get(String key) throws KeyValueStoreClientException { if (!isAlive()) { throw(new KeyValueStoreClientException(new IllegalStateException("client not established"))); } byte[] raw = null; try { Future<byte[]> f = _client.asyncGet(key, _transcoder); raw = f.get(_timeoutInMs, TimeUnit.MILLISECONDS); } catch (Exception error) { throw(new KeyValueStoreClientException(error)); } return raw; }
@Override protected Cancellable restore(final String storageKey, final FutureCallback<byte[]> callback) { final GetFuture<Object> getFuture = client.asyncGet(storageKey); getFuture.addListener(new GetCompletionListener() { @Override public void onComplete(final GetFuture<?> future) throws Exception { try { callback.completed(castAsByteArray(getFuture.get())); } catch (final ExecutionException ex) { if (ex.getCause() instanceof Exception) { callback.failed((Exception) ex.getCause()); } else { callback.failed(ex); } } } }); return Operations.cancellable(getFuture); }
/** * Get the value of the key. * Check the local cache first. If the key is not found, send the command to the server. * * @param key the key to fetch * @param tc the transcoder to serialize and unserialize value * @return a future that will hold the value of the key */ @Override public <T> Future<T> asyncGet(final String key, final Transcoder<T> tc) { Element frontElement = null; if (localCacheManager != null) { frontElement = localCacheManager.getElement(key); } if (frontElement == null) { return super.asyncGet(key, tc); } else { return new FrontCacheGetFuture<T>(frontElement); } }
@Override @SuppressWarnings("unchecked") public <T> T get(String key) throws IOException { notNullValue("key", key); try { if (hasNoAvailableServer()) { return null; } return (T) memcached.asyncGet(getKeyWithNamespace(key)).get(getMaxWaitMillis(), TimeUnit.MILLISECONDS); } catch (Throwable t) { String failedMessage = "Failed to get value on memcached! (key:" + key + ")"; throw new IOException(failedMessage, t); } }