Skip to content

Commit 1550cc4

Browse files
committed
PR #717: local cache disabled by default
1 parent cefd1f7 commit 1550cc4

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

powertools-idempotency/src/main/java/software/amazon/lambda/powertools/idempotency/IdempotencyConfig.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
package software.amazon.lambda.powertools.idempotency;
1515

16-
import software.amazon.lambda.powertools.utilities.cache.LRUCache;
16+
import software.amazon.lambda.powertools.idempotency.internal.cache.LRUCache;
1717

1818
import java.time.Duration;
1919

@@ -80,7 +80,7 @@ public static Builder builder() {
8080
public static class Builder {
8181

8282
private int localCacheMaxItems = 256;
83-
private boolean useLocalCache = true;
83+
private boolean useLocalCache = false;
8484
private long expirationInSeconds = 60 * 60; // 1 hour
8585
private String eventKeyJMESPath;
8686
private String payloadValidationJMESPath;
@@ -144,7 +144,7 @@ public Builder withLocalCacheMaxItems(int localCacheMaxItems) {
144144
}
145145

146146
/**
147-
* Whether to locally cache idempotency results, by default true
147+
* Whether to locally cache idempotency results, by default false
148148
*
149149
* @param useLocalCache boolean that indicate if a local cache must be used in addition to the persistence store.
150150
* If set to true, will use the {@link LRUCache}

powertools-idempotency/src/test/java/software/amazon/lambda/powertools/idempotency/persistence/BasePersistenceStoreTest.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
import software.amazon.lambda.powertools.idempotency.exceptions.IdempotencyItemNotFoundException;
2626
import software.amazon.lambda.powertools.idempotency.exceptions.IdempotencyKeyException;
2727
import software.amazon.lambda.powertools.idempotency.exceptions.IdempotencyValidationException;
28+
import software.amazon.lambda.powertools.idempotency.internal.cache.LRUCache;
2829
import software.amazon.lambda.powertools.idempotency.model.Product;
2930
import software.amazon.lambda.powertools.utilities.JsonConfig;
30-
import software.amazon.lambda.powertools.utilities.cache.LRUCache;
3131

3232
import java.time.Duration;
3333
import java.time.Instant;
@@ -97,7 +97,6 @@ public void saveInProgress_jmespath() {
9797
APIGatewayProxyRequestEvent event = EventLoader.loadApiGatewayRestEvent("apigw_event.json");
9898
persistenceStore.configure(IdempotencyConfig.builder()
9999
.withEventKeyJMESPath("powertools_json(body).id")
100-
.withUseLocalCache(false)
101100
.build(), "myfunc");
102101

103102
Instant now = Instant.now();
@@ -114,7 +113,6 @@ public void saveInProgress_jmespath() {
114113
public void saveInProgress_jmespath_NotFound_shouldThrowException() {
115114
APIGatewayProxyRequestEvent event = EventLoader.loadApiGatewayRestEvent("apigw_event.json");
116115
persistenceStore.configure(IdempotencyConfig.builder()
117-
.withUseLocalCache(false)
118116
.withEventKeyJMESPath("unavailable")
119117
.withThrowOnNoIdempotencyKey(true) // should throw
120118
.build(), "");
@@ -129,7 +127,6 @@ public void saveInProgress_jmespath_NotFound_shouldThrowException() {
129127
public void saveInProgress_jmespath_NotFound_shouldNotThrowException() {
130128
APIGatewayProxyRequestEvent event = EventLoader.loadApiGatewayRestEvent("apigw_event.json");
131129
persistenceStore.configure(IdempotencyConfig.builder()
132-
.withUseLocalCache(false)
133130
.withEventKeyJMESPath("unavailable")
134131
.build(), "");
135132
Instant now = Instant.now();
@@ -143,6 +140,7 @@ public void saveInProgress_withLocalCache_NotExpired_ShouldThrowException() {
143140
APIGatewayProxyRequestEvent event = EventLoader.loadApiGatewayRestEvent("apigw_event.json");
144141
LRUCache<String, DataRecord> cache = new LRUCache<>(2);
145142
persistenceStore.configure(IdempotencyConfig.builder()
143+
.withUseLocalCache(true)
146144
.withEventKeyJMESPath("powertools_json(body).id")
147145
.build(), null, cache);
148146
Instant now = Instant.now();
@@ -164,6 +162,7 @@ public void saveInProgress_withLocalCache_Expired_ShouldRemoveFromCache() {
164162
LRUCache<String, DataRecord> cache = new LRUCache<>(2);
165163
persistenceStore.configure(IdempotencyConfig.builder()
166164
.withEventKeyJMESPath("powertools_json(body).id")
165+
.withUseLocalCache(true)
167166
.withExpiration(Duration.of(2, ChronoUnit.SECONDS))
168167
.build(), null, cache);
169168
Instant now = Instant.now();
@@ -189,7 +188,7 @@ public void saveInProgress_withLocalCache_Expired_ShouldRemoveFromCache() {
189188
public void saveSuccess_shouldUpdateRecord() throws JsonProcessingException {
190189
APIGatewayProxyRequestEvent event = EventLoader.loadApiGatewayRestEvent("apigw_event.json");
191190
LRUCache<String, DataRecord> cache = new LRUCache<>(2);
192-
persistenceStore.configure(IdempotencyConfig.builder().withUseLocalCache(false).build(), null, cache);
191+
persistenceStore.configure(IdempotencyConfig.builder().build(), null, cache);
193192

194193
Product product = new Product(34543, "product", 42);
195194
Instant now = Instant.now();
@@ -208,7 +207,8 @@ public void saveSuccess_shouldUpdateRecord() throws JsonProcessingException {
208207
public void saveSuccess_withCacheEnabled_shouldSaveInCache() throws JsonProcessingException {
209208
APIGatewayProxyRequestEvent event = EventLoader.loadApiGatewayRestEvent("apigw_event.json");
210209
LRUCache<String, DataRecord> cache = new LRUCache<>(2);
211-
persistenceStore.configure(IdempotencyConfig.builder().build(), null, cache);
210+
persistenceStore.configure(IdempotencyConfig.builder()
211+
.withUseLocalCache(true).build(), null, cache);
212212

213213
Product product = new Product(34543, "product", 42);
214214
Instant now = Instant.now();
@@ -234,7 +234,7 @@ public void saveSuccess_withCacheEnabled_shouldSaveInCache() throws JsonProcessi
234234
public void getRecord_shouldReturnRecordFromPersistence() throws IdempotencyItemNotFoundException, IdempotencyValidationException {
235235
APIGatewayProxyRequestEvent event = EventLoader.loadApiGatewayRestEvent("apigw_event.json");
236236
LRUCache<String, DataRecord> cache = new LRUCache<>(2);
237-
persistenceStore.configure(IdempotencyConfig.builder().withUseLocalCache(false).build(), "myfunc", cache);
237+
persistenceStore.configure(IdempotencyConfig.builder().build(), "myfunc", cache);
238238

239239
Instant now = Instant.now();
240240
DataRecord record = persistenceStore.getRecord(JsonConfig.get().getObjectMapper().valueToTree(event), now);
@@ -248,7 +248,8 @@ public void getRecord_shouldReturnRecordFromPersistence() throws IdempotencyItem
248248
public void getRecord_cacheEnabledNotExpired_shouldReturnRecordFromCache() throws IdempotencyItemNotFoundException, IdempotencyValidationException {
249249
APIGatewayProxyRequestEvent event = EventLoader.loadApiGatewayRestEvent("apigw_event.json");
250250
LRUCache<String, DataRecord> cache = new LRUCache<>(2);
251-
persistenceStore.configure(IdempotencyConfig.builder().build(), "myfunc", cache);
251+
persistenceStore.configure(IdempotencyConfig.builder()
252+
.withUseLocalCache(true).build(), "myfunc", cache);
252253

253254
Instant now = Instant.now();
254255
DataRecord dr = new DataRecord(
@@ -270,7 +271,8 @@ public void getRecord_cacheEnabledNotExpired_shouldReturnRecordFromCache() throw
270271
public void getRecord_cacheEnabledExpired_shouldReturnRecordFromPersistence() throws IdempotencyItemNotFoundException, IdempotencyValidationException {
271272
APIGatewayProxyRequestEvent event = EventLoader.loadApiGatewayRestEvent("apigw_event.json");
272273
LRUCache<String, DataRecord> cache = new LRUCache<>(2);
273-
persistenceStore.configure(IdempotencyConfig.builder().build(), "myfunc", cache);
274+
persistenceStore.configure(IdempotencyConfig.builder()
275+
.withUseLocalCache(true).build(), "myfunc", cache);
274276

275277
Instant now = Instant.now();
276278
DataRecord dr = new DataRecord(
@@ -323,7 +325,8 @@ public void deleteRecord_shouldDeleteRecordFromPersistence() {
323325
public void deleteRecord_cacheEnabled_shouldDeleteRecordFromCache() {
324326
APIGatewayProxyRequestEvent event = EventLoader.loadApiGatewayRestEvent("apigw_event.json");
325327
LRUCache<String, DataRecord> cache = new LRUCache<>(2);
326-
persistenceStore.configure(IdempotencyConfig.builder().build(), null, cache);
328+
persistenceStore.configure(IdempotencyConfig.builder()
329+
.withUseLocalCache(true).build(), null, cache);
327330

328331
cache.put("testFunction#47261bd5b456f400f8d191cfb3a7482f",
329332
new DataRecord("testFunction#47261bd5b456f400f8d191cfb3a7482f", DataRecord.Status.COMPLETED, 123, null, null));

0 commit comments

Comments
 (0)