Passes through cache TTL to underlying backing store.
MODIFY
gateway/engine/ispn/src/main/java/io/apiman/gateway/engine/ispn/InfinispanBackingStore.java
gateway/engine/ispn/src/main/java/io/apiman/gateway/engine/ispn/InfinispanBackingStore.java
@@ -18,6 +18,8 @@ package io.apiman.gateway.engine.ispn;
import io.apiman.gateway.engine.storage.store.IBackingStore;
import org.infinispan.Cache;
+import java.util.concurrent.TimeUnit;
+
/**
* Store provider for components backed by an Infinispan cache.
*/
@@ -33,6 +35,11 @@ public class InfinispanBackingStore implements IBackingStore {
cache.put(key, value);
}
+ @Override
+ public void put(String key, Object value, long ttl) {
+ cache.put(key, value, ttl, TimeUnit.SECONDS);
+ }
+
@SuppressWarnings("unchecked")
@Override
public <T> T get(String key, Class<T> type) {

MODIFY
gateway/engine/redis/src/main/java/io/apiman/gateway/engine/redis/common/RedisBackingStore.java
gateway/engine/redis/src/main/java/io/apiman/gateway/engine/redis/common/RedisBackingStore.java
@@ -19,8 +19,10 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import io.apiman.gateway.engine.storage.store.IBackingStore;
import io.apiman.gateway.engine.storage.util.BackingStoreUtil;
import org.redisson.api.RMap;
+import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient;
+import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Function;
@@ -47,8 +49,8 @@ public class RedisBackingStore implements IBackingStore {
* @param func the function to apply using the {@link RMap}
* @return the result
*/
- private <T> T onMap(Function<RMap<String, String>, T> func) {
- final RMap<String, String> map = client.getMap(prefix);
+ private <T> T onMap(Function<RMapCache<String, String>, T> func) {
+ final RMapCache<String, String> map = client.getMapCache(prefix);
return func.apply(map);
}
@@ -57,13 +59,13 @@ public class RedisBackingStore implements IBackingStore {
*
* @param consumer the consumer of the {@link RMap}
*/
- private void withMap(Consumer<RMap<String, String>> consumer) {
- final RMap<String, String> map = client.getMap(prefix);
+ private void withMap(Consumer<RMapCache<String, String>> consumer) {
+ final RMapCache<String, String> map = client.getMapCache(prefix);
consumer.accept(map);
}
@Override
- public void put(String key, Object value) {
+ public void put(String key, Object value, long ttl) {
withMap(map -> {
try {
final String raw;
@@ -74,7 +76,7 @@ public class RedisBackingStore implements IBackingStore {
} else {
raw = JSON_MAPPER.writeValueAsString(value);
}
- map.put(key, raw);
+ map.put(key, raw, ttl, TimeUnit.SECONDS);
} catch (JsonProcessingException e) {
throw new RuntimeException(String.format("Error setting value for key '%s'", key), e);

MODIFY
gateway/engine/storage-common/src/main/java/io/apiman/gateway/engine/storage/component/AbstractCacheStoreComponent.java
gateway/engine/storage-common/src/main/java/io/apiman/gateway/engine/storage/component/AbstractCacheStoreComponent.java
@@ -68,7 +68,7 @@ public abstract class AbstractCacheStoreComponent extends AbstractStorageCompone
entry.setExpiresOn(System.currentTimeMillis() + (timeToLive * 1000));
entry.setHead(JSON_MAPPER.writeValueAsString(jsonObject));
try {
- getStore().put(cacheKey, entry);
+ getStore().put(cacheKey, entry, timeToLive);
} catch (Throwable e) {
LOGGER.error("Error writing cache entry with key: {}", cacheKey, e);
}
@@ -110,7 +110,7 @@ public abstract class AbstractCacheStoreComponent extends AbstractStorageCompone
if (!aborted) {
entry.setData(Base64.encodeBase64String(data.getBytes()));
try {
- getStore().put(cacheKey, entry);
+ getStore().put(cacheKey, entry, timeToLive);
} catch (Throwable e) {
LOGGER.error("Error writing binary cache entry with key: {}", cacheKey, e);
}

MODIFY
gateway/engine/storage-common/src/main/java/io/apiman/gateway/engine/storage/store/IBackingStore.java
gateway/engine/storage-common/src/main/java/io/apiman/gateway/engine/storage/store/IBackingStore.java
@@ -25,7 +25,18 @@ public interface IBackingStore {
* @param key the item's key
* @param value the item's value
*/
- void put(String key, Object value);
+ default void put(String key, Object value) {
+ put(key, value, Integer.MAX_VALUE);
+ }
+
+ /**
+ * Insert an item into the store, with a given TTL.
+ *
+ * @param key the item's key
+ * @param value the item's value
+ * @param ttl the TTL in seconds
+ */
+ void put(String key, Object value, long ttl);
/**
* Fetch an item from the store.

MODIFY
gateway/engine/storage-common/src/main/java/io/apiman/gateway/engine/storage/store/MapBackingStore.java
gateway/engine/storage-common/src/main/java/io/apiman/gateway/engine/storage/store/MapBackingStore.java
@@ -19,6 +19,8 @@ import java.util.Map;
/**
* A backing store that uses a {@link Map}.
+ *
+ * Note: this implementation never evicts entries from its map, meaning that it will grow infinitely.
*/
public class MapBackingStore implements IBackingStore {
private final Map<String, Object> map;
@@ -28,7 +30,7 @@ public class MapBackingStore implements IBackingStore {
}
@Override
- public void put(String key, Object value) {
+ public void put(String key, Object value, long ttl) {
map.put(key, value);
}

Passes through cache TTL to underlying backing store. (#845)