Skip to content

Commit 4a568b2

Browse files
committed
feat(gh-15): Check if TTL has been exceeded
1 parent 5994162 commit 4a568b2

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/main/java/com/dasburo/spring/cache/dynamo/DefaultDynamoCacheWriter.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,13 @@ public boolean createIfNotExists(String name, Duration ttl, Long readCapacityUni
206206

207207
private byte[] getInternal(String name, String key) {
208208
final GetItemRequest request = GetItemRequest.builder()
209-
.attributesToGet(ATTRIBUTE_VALUE)
209+
.attributesToGet(ATTRIBUTE_VALUE, ATTRIBUTE_TTL)
210210
.tableName(name)
211211
.key(Collections.singletonMap(ATTRIBUTE_KEY, AttributeValue.fromS(key)))
212212
.build();
213213

214214
final GetItemResponse result = dynamoTemplate.getItem(request);
215-
if (result.hasItem()) {
215+
if (result.hasItem() && !isPastTtl(result)) {
216216
return getAttributeValue(result);
217217
} else {
218218
throw new NoSuchElementException(String.format("No entry found for '%s'.", key));
@@ -234,6 +234,15 @@ private byte[] getAttributeValue(GetItemResponse result) {
234234
}
235235
}
236236

237+
private boolean isPastTtl(GetItemResponse result) {
238+
final AttributeValue attributeTtl = result.item().get(ATTRIBUTE_TTL);
239+
if (attributeTtl != null && attributeTtl.n() != null) {
240+
Instant ttlInstant = Instant.ofEpochSecond(Long.parseLong(attributeTtl.n()));
241+
return Instant.now().isAfter(ttlInstant);
242+
}
243+
return false;
244+
}
245+
237246
private void putInternal(String name, String key, @Nullable byte[] value, @Nullable Duration ttl, @Nullable List<RootAttribute> rootAttributes) {
238247
Map<String, AttributeValue> attributeValues = new HashMap<>();
239248
attributeValues.put(ATTRIBUTE_KEY, AttributeValue.fromS(key));

0 commit comments

Comments
 (0)