@Override public Optional<V> get(String key) { return (Optional<V>) Optional.ofNullable(memcachedClient.get(key)); }
@SuppressWarnings("unchecked") @Override public CacheWrapper<Object> get(final CacheKeyTO cacheKeyTO, Method method) throws CacheCenterConnectionException { if (null == cacheKeyTO) { return null; } String cacheKey = cacheKeyTO.getCacheKey(); if (null == cacheKey || cacheKey.isEmpty()) { return null; } String hfield = cacheKeyTO.getHfield(); if (null != hfield && hfield.length() > 0) { throw new RuntimeException("memcached does not support hash cache."); } return (CacheWrapper<Object>) memcachedClient.get(cacheKey); }
@SuppressWarnings("unchecked") @Override public Session get(final Builder builder) { String key = key(builder.sessionId()); Map<String, String> attrs = (Map<String, String>) memcached.get(key); if (attrs == null || attrs.size() == 0) { // expired return null; } // touch session memcached.touch(key, timeout); return builder .accessedAt(Long.parseLong(attrs.remove("_accessedAt"))) .createdAt(Long.parseLong(attrs.remove("_createdAt"))) .savedAt(Long.parseLong(attrs.remove("_savedAt"))) .set(attrs) .build(); }
@Override public Cacheable getBlock(BlockCacheKey cacheKey, boolean caching, boolean repeat, boolean updateCacheMetrics) { // Assume that nothing is the block cache HFileBlock result = null; try (TraceScope traceScope = TraceUtil.createTrace("MemcachedBlockCache.getBlock")) { result = client.get(cacheKey.toString(), tc); } catch (Exception e) { // Catch a pretty broad set of exceptions to limit any changes in the memecache client // and how it handles failures from leaking into the read path. if (LOG.isDebugEnabled()) { LOG.debug("Exception pulling from memcached [ " + cacheKey.toString() + " ]. Treating as a miss.", e); } result = null; } finally { // Update stats if this request doesn't have it turned off 100% of the time if (updateCacheMetrics) { if (result == null) { cacheStats.miss(caching, cacheKey.isPrimary(), cacheKey.getBlockType()); } else { cacheStats.hit(caching, cacheKey.isPrimary(), cacheKey.getBlockType()); } } } return result; }
@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); }
@Override public SessionData get(String sessionId) { return (SessionData) this.sessions.get(sessionId); }
@Override public byte[] get(String key) { return _memcached.get(key, ByteArrayTranscoder.INSTANCE); }
@Override public Object call() throws Exception { return _client.get( sessionId1 ); } }, notNullValue() );
@Override public Object call() throws Exception { return _client.get( sessionId1 ); } }, notNullValue());
@Override public byte[] get(final String key) { return _client.get(key, ByteArrayTranscoder.INSTANCE); } };
@Override public byte[] get(final String key) { return _client.get(key, ByteArrayTranscoder.INSTANCE); } };
public Object get(final String key) { return fromCacheValue(client.get(key)); }
@Override protected byte[] restore(final String storageKey) throws ResourceIOException { try { return castAsByteArray(client.get(storageKey)); } catch (final OperationTimeoutException ex) { throw new MemcachedOperationTimeoutException(ex); } }
@Test( enabled = true ) public void testInvalidateSessionShouldReleaseLockIssue144() throws IOException, InterruptedException, HttpException { _tomcat1.getManager().setLockingMode(LockingMode.AUTO.name()); final String sessionId1 = get( _httpClient, TC_PORT_1, null ).getSessionId(); assertNotNull( sessionId1, "No session created." ); final Response response = get( _httpClient, TC_PORT_1, PATH_INVALIDATE, sessionId1 ); assertNull( response.getResponseSessionId() ); assertNull(_client.get( sessionId1 ), "Invalidated session should be removed from memcached"); assertNull(_client.get(new SessionIdFormat().createLockName(sessionId1)), "Lock should be released."); }
@Test public void testInvalidNonStickySessionDoesNotCallOnBackupWithoutLoadedSessionIssue137() throws Exception { _service.setStickyInternal( false ); _service.setLockingMode( LockingMode.NONE, null, false ); _service.startInternal(new MemcachedStorageClient(_memcachedMock)); // we must put in our mock again final String sessionId = "nonStickySessionToTimeOut-n1"; // For findSession needed final Request requestMock = mock(Request.class); when(requestMock.getNote(eq(RequestTrackingContextValve.INVOKED))).thenReturn(Boolean.TRUE); _service.getTrackingHostValve().storeRequestThreadLocal(requestMock); final MemcachedBackupSession session = _service.findSession(sessionId); assertNull(session); _service.backupSession( sessionId, false, null ).get(); // check that validity info is not loaded - this would trigger the // WARNING: Found no validity info for session id ... final String validityKey = new SessionIdFormat().createValidityInfoKeyName( sessionId ); verify( _memcachedMock, times( 0 ) ).get( eq( validityKey ) ); }
when( _memcachedMock.get( eq( validityKey ), any ( Transcoder.class) ) ).thenReturn( validityData );
@Test public void testBackupSessionInCouchbase() throws InterruptedException, ExecutionException { final MemcachedSessionService service = _tomcat1.getService(); final MemcachedBackupSession session = createSession( service ); final String sessionId = "12345"; session.setId(sessionId); session.setAttribute( "foo", "bar" ); final BackupResult backupResult = service.backupSession( session.getIdInternal(), false, null ).get(); assertEquals(backupResult.getStatus(), BackupResultStatus.SUCCESS); final MemcachedBackupSession loadedSession = transcoderService.deserialize(mc.get(sessionId, ByteArrayTranscoder.INSTANCE), _tomcat1.getManager()); checkSession(loadedSession, session); }
_service.getTrackingHostValve().storeRequestThreadLocal(requestMock); when(_memcachedMock.get(eq(session.getId()), any(Transcoder.class))).thenReturn(transcoderService.serialize(session));
@Test(enabled = false) // spurious failures public void testBackupSessionInCouchbaseCluster() throws Exception { final MemcachedSessionService service = _tomcat1.getService(); cluster.add(setupCouchbase(getMaxCouchbasePort() + 1)); service.setMemcachedNodes(getMemcachedNodesConfig(getURIs())); setupCouchbaseClient(); waitForReconnect(service.getStorageClient(), cluster.size(), 1000); waitForReconnect(mc, cluster.size(), 1000); final MemcachedBackupSession session = createSession( service ); final String sessionId = "12345"; session.setId(sessionId); session.setAttribute( "foo", "bar" ); final BackupResult backupResult = service.backupSession( session.getIdInternal(), false, null ).get(); assertEquals(backupResult.getStatus(), BackupResultStatus.SUCCESS); final MemcachedBackupSession loadedSession = transcoderService.deserialize(mc.get(sessionId, ByteArrayTranscoder.INSTANCE), _tomcat1.getManager()); checkSession(loadedSession, session); }