public void add(String key, Object value, int expiration) { client.add(key, expiration, value, tc); }
public boolean safeAdd(String key, Object value, int expiration) { Future<Boolean> future = client.add(key, expiration, value, tc); try { return future.get(1, TimeUnit.SECONDS); } catch (Exception e) { future.cancel(false); } return false; }
@Override public void cacheBlock(BlockCacheKey cacheKey, Cacheable buf) { if (buf instanceof HFileBlock) { client.add(cacheKey.toString(), MAX_SIZE, (HFileBlock) buf, tc); } else { if (LOG.isDebugEnabled()) { LOG.debug("MemcachedBlockCache can not cache Cacheable's of type " + buf.getClass().toString()); } } }
@Override public boolean add(final String key, int exp, final Object value) { log.trace("Memcached add operation on key '{}', with value:{}", key, value); boolean success = false; int tryCount = 0; do { try { if (tryCount > 0) { Thread.sleep(retryAfterSec * 1000); log.warn("retrying operation #{}", tryCount); } tryCount++; success = memcachedClient.add(key, exp, value) .get(); } catch (final Exception e) { log.warn("memcache set: {}", e.getMessage()); } } while (!success && tryCount < maxTry); if (!success) { log.error("Failed to add key to Elasticache {}", key); } if (success && tryCount > 1) { log.info("Connection restored OK to Elasticache cluster"); } available = success; return success; }
@Override public Status insert( String table, String key, Map<String, ByteIterator> values) { key = createQualifiedKey(table, key); try { OperationFuture<Boolean> future = memcachedClient().add(key, objectExpirationTime, toJson(values)); return getReturnCode(future); } catch (Exception e) { logger.error("Error inserting value", e); return Status.ERROR; } }
@Override public Future<Boolean> add(String key, int exp, byte[] o) { return _memcached.add(key, exp, o, ByteArrayTranscoder.INSTANCE); }
@Before public void setup() { memcachedClient = mock(MemcachedClient.class); stub(memcachedClient.get("key1")).toReturn("value1"); stub(memcachedClient.get("key2")).toReturn("value2"); OperationFuture<Boolean> mockedFuture = mock(OperationFuture.class); stub(memcachedClient.add("keyAdd", 3600, "valueadd")).toReturn(mockedFuture); }
when( futureMock.get() ).thenReturn( Boolean.FALSE ); when( futureMock.get( anyInt(), any( TimeUnit.class ) ) ).thenReturn( Boolean.FALSE ); when( _memcachedMock.add( any( String.class ), anyInt(), any(), any( Transcoder.class ) ) ).thenReturn( futureMock ); verify( _memcachedMock, times( 1 ) ).add( eq( sessionId ), anyInt(), any(), any( Transcoder.class ) ); verify( _memcachedMock, times( 1 ) ).add( eq( backupSessionKey ), anyInt(), any(), any( Transcoder.class ) );
when(addResultMock.get()).thenReturn(true); when(addResultMock.get(anyLong(), any(TimeUnit.class))).thenReturn(true); when(_memcachedMock.add(anyString(), anyInt(), any(), any(Transcoder.class))).thenReturn(addResultMock);
@Override public boolean safeAdd(String key, Object value, int expiration) { Future<Boolean> future = client.add(key, expiration, value, tc); try { return future.get(1, TimeUnit.SECONDS); } catch (Exception e) { future.cancel(false); } return false; }
private MemcachedClient bootstrapClient() throws IOException, UnknownHostException, InterruptedException, ExecutionException { MemcachedClient client = createMemcachedClient(); Future<Boolean> f = client.add("key", 10, "myStringValue"); f.get(); Future<Boolean> f1 = client.add("key1", 10, "myStringValue1"); f1.get(); return client; }
@Override public void cacheBlock(BlockCacheKey cacheKey, Cacheable buf) { if (buf instanceof HFileBlock) { client.add(cacheKey.toString(), MAX_SIZE, (HFileBlock) buf, tc); } else { if (LOG.isDebugEnabled()) { LOG.debug("MemcachedBlockCache can not cache Cacheable's of type " + buf.getClass().toString()); } } }
public void testAdd() throws Exception { MemcachedClient client = bootstrapClient(); Future<Boolean> f = client.add("key", 10, "newVal"); assertFalse(f.get()); }
public void testLongExpiration() throws Exception { MemcachedClient client = bootstrapClient(); client.add("newKey", (int)System.currentTimeMillis() - 60 *1000, "newValue"); Thread.sleep(15 *1000); assertEquals("newValue", client.get("newKey")); }
public void testStats() throws Exception { MemcachedClient client = bootstrapClient(); Map stats = client.getStats(); logger.info("stats:"+stats+" val:"+stats.values().toArray()[0]); assertEquals(1, stats.size()); assertTrue(((Map)stats.values().toArray()[0]).isEmpty()); assertTrue(client.add("keystats", 1, "stats").get()); }
public void testMultiGet() throws Exception { MemcachedClient client = bootstrapClient(); Map<String, Object> val = client.getBulk("key", "key1"); assertEquals(2, val.size()); assertEquals("myStringValue", val.get("key")); assertEquals("myStringValue1", val.get("key1")); client.add("Hello", 0, "World"); Thread.sleep(1100); assertEquals("World", client.get("Hello")); }
public void testDecr() throws Exception { MemcachedClient client = bootstrapClient(); client.add("decrkey", 10, 99).get(); assertEquals(95, client.decr("decrkey", 4)); assertEquals(94, client.decr("decrkey", 1)); assertEquals(-1, client.decr("decrkey1", 77)); }
public void testIncr() throws Exception { MemcachedClient client = bootstrapClient(); client.add("incrkey", 10, 99).get(); assertEquals(104, client.incr("incrkey", 5)); assertEquals(105, client.incr("incrkey", 1)); assertEquals(-1, client.incr("inckey1", 10)); }
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); }
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()); }