@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(); }
/** * Touch the given key to reset its expiration time with the default * transcoder. * * @param key the key to fetch * @param exp the new expiration to set for the given key * @return a future that will hold the return value of whether or not the * fetch succeeded * @throws IllegalStateException in the rare circumstance where queue is too * full to accept any more requests */ @Override public <T> OperationFuture<Boolean> touch(final String key, final int exp) { return touch(key, exp, transcoder); }
/** * Touch the given key to reset its expiration time with the default * transcoder. * * @param key the key to fetch * @param exp the new expiration to set for the given key * @return a future that will hold the return value of whether or not the * fetch succeeded * @throws IllegalStateException in the rare circumstance where queue is too * full to accept any more requests */ public <T> OperationFuture<Boolean> touch(final String key, final int exp) { return touch(key, exp, transcoder); }
/** * Touch the given key to reset its expiration time with the default * transcoder. * * @param key the key to fetch * @param exp the new expiration to set for the given key * @return a future that will hold the return value of whether or not the * fetch succeeded * @throws IllegalStateException in the rare circumstance where queue is too * full to accept any more requests */ @Override public <T> OperationFuture<Boolean> touch(final String key, final int exp) { return touch(key, exp, transcoder); }
@Override public T get( String key ) { Object value = client.get( prefix( key ), new SerializingTranscoder() ); client.touch( prefix( key ), expiration ); if( value == null ) { return null; } return valueType.cast( value ); }
@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(); }
for (String key : keySet) { logger.debug("Updating expiration of key {} to {}", key, expiry); results.add(memcacheClient.touch(key, expiry));
@Test public void whenPutIfAbsentAndNoCachedValueThenReturnNewValue() { when(memcachedClient.get(anyString())) .thenReturn(NAMESPACE_KEY_VALUE) .thenReturn(null) .thenReturn(NAMESPACE_KEY_VALUE); Cache.ValueWrapper actual = memcachedCache.putIfAbsent(CACHED_OBJECT_KEY, newCachedValue); assertThat(actual.get()).isEqualTo(newCachedValue); verify(memcachedClient, times(2)).get(namespaceKey); verify(memcachedClient).get(memcachedKey); verify(memcachedClient).set(eq(memcachedKey), anyInt(), eq(newCachedValue)); verify(memcachedClient).touch(namespaceKey, CACHE_EXPIRATION); }
@Test public void whenGetWithValueLoaderAndCachedValueMissingThenReturnValueLoaderNull() { when(memcachedClient.get(namespaceKey)).thenReturn(NAMESPACE_KEY_VALUE); when(memcachedClient.get(memcachedKey)).thenReturn(null); Object actual = memcachedCache.get(CACHED_OBJECT_KEY, () -> valueLoaderNullValue); assertThat(actual).isEqualTo(null); verify(memcachedClient, times(3)).get(namespaceKey); verify(memcachedClient, times(2)).get(memcachedKey); verify(memcachedClient).set(memcachedKey, CACHE_EXPIRATION, valueLoaderNullValue); verify(memcachedClient).touch(namespaceKey, CACHE_EXPIRATION); }
@Test public void whenGetWithValueLoaderAndCachedValueMissingThenReturnValueLoaderValue() { when(memcachedClient.get(namespaceKey)).thenReturn(NAMESPACE_KEY_VALUE); when(memcachedClient.get(memcachedKey)).thenReturn(null); Object actual = memcachedCache.get(CACHED_OBJECT_KEY, () -> valueLoaderValue); assertThat(actual).isEqualTo(valueLoaderValue); verify(memcachedClient, times(3)).get(namespaceKey); verify(memcachedClient, times(2)).get(memcachedKey); verify(memcachedClient).set(memcachedKey, CACHE_EXPIRATION, valueLoaderValue); verify(memcachedClient).touch(namespaceKey, CACHE_EXPIRATION); }
@Test public void whenPutAndNamespaceMissingThenSetNamespace() { when(memcachedClient.get(namespaceKey)).thenReturn(null); memcachedCache.put(CACHED_OBJECT_KEY, cachedValue); verify(memcachedClient).get(namespaceKey); verify(memcachedClient).set(eq(namespaceKey), eq(CACHE_EXPIRATION), anyString()); verify(memcachedClient).set(endsWith(CACHED_OBJECT_KEY), eq(CACHE_EXPIRATION), eq(cachedValue)); verify(memcachedClient).touch(namespaceKey, CACHE_EXPIRATION); }
/** {@inheritDoc} */ @Override public boolean updateExpiration(@Nonnull @NotEmpty final String context, @Nonnull @NotEmpty final String key, @Nullable @Positive final Long expiration) throws IOException { Constraint.isNotNull(StringSupport.trimOrNull(context), "Context cannot be null or empty"); Constraint.isNotNull(StringSupport.trimOrNull(key), "Key cannot be null or empty"); final int expiry = MemcachedStorageRecord.expiry(expiration); Constraint.isGreaterThan(-1, expiry, "Expiration must be null or positive"); final String namespace = lookupNamespace(context); if (namespace == null) { logger.debug("Namespace for context {} does not exist", context); return false; } final String cacheKey = memcachedKey(namespace, key); logger.debug("Updating expiration for entry at {} for context={}, key={}", cacheKey, context, key); return handleAsyncResult(memcacheClient.touch(cacheKey, expiry)); }
@Test public void whenPutNullThenStoreNullValueInstance() { when(memcachedClient.get(namespaceKey)).thenReturn(NAMESPACE_KEY_VALUE); memcachedCache.put(CACHED_OBJECT_KEY, null); verify(memcachedClient).get(namespaceKey); verify(memcachedClient).set(memcachedKey, CACHE_EXPIRATION, NullValue.INSTANCE); verify(memcachedClient).touch(namespaceKey, CACHE_EXPIRATION); }
@Override public void put(Object key, Object value) { this.memcachedClient.set(memcachedKey(key), this.memcacheCacheMetadata.expiration(), toStoreValue(value)); this.memcachedClient.touch(this.memcacheCacheMetadata.namespaceKey(), this.memcacheCacheMetadata.expiration()); }