Skip to content

Commit b3be2e0

Browse files
committed
Fix locking DynamoCacheWriter and improve test coverage
1 parent 67abb23 commit b3be2e0

16 files changed

+238
-17
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,7 @@ public void remove(String name, String key) {
126126
Assert.notNull(key, "Key must not be null!");
127127

128128
execute(name, connection -> {
129-
dynamoTemplate.deleteItem(new DeleteItemRequest()
130-
.withTableName(name)
131-
.withKey(Collections.singletonMap(ATTRIBUTE_KEY, new AttributeValue(key))));
129+
removeInternal(name, key);
132130
return "OK";
133131
});
134132
}
@@ -226,19 +224,29 @@ private void putInternal(String name, String key, @Nullable byte[] value, @Nulla
226224
dynamoTemplate.putItem(putItemRequest);
227225
}
228226

227+
private void removeInternal(String name, String key) {
228+
dynamoTemplate.deleteItem(new DeleteItemRequest()
229+
.withTableName(name)
230+
.withKey(Collections.singletonMap(ATTRIBUTE_KEY, new AttributeValue(key))));
231+
}
232+
229233
private void doLock(String name) {
230234
// TODO should a ttl be provided for locking?
231-
putInternal(name, createCacheLockKey(name), new byte[0], null);
235+
putInternal(name, createCacheLockKey(name), "1".getBytes(), null);
232236
}
233237

234238
private void doUnlock(String name) {
235-
remove(name, createCacheLockKey(name));
239+
try {
240+
removeInternal(name, createCacheLockKey(name));
241+
} catch (ResourceNotFoundException e) {
242+
// ignore
243+
}
236244
}
237245

238246
private boolean doCheckLock(String name) {
239247
try {
240248
getInternal(name, createCacheLockKey(name));
241-
} catch (NoSuchElementException e) {
249+
} catch (NoSuchElementException | ResourceNotFoundException e) {
242250
return false;
243251
}
244252
return true;

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected DynamoCacheBuilder(final String cacheName, final AmazonDynamoDB dynamo
4444
Assert.hasText(cacheName, "'cacheName' must not be null and must contain at least one non-whitespace character.");
4545

4646
this.cacheName = cacheName;
47-
this.writer = new DefaultDynamoCacheWriter(dynamoTemplate);
47+
this.writer = DynamoCacheWriter.nonLockingDynamoCacheWriter(dynamoTemplate);
4848
this.cacheConfig = DynamoCacheConfiguration.defaultCacheConfig();
4949
}
5050

@@ -124,4 +124,16 @@ public DynamoCacheBuilder withSerializer(DynamoSerializer serializer) {
124124
return this;
125125
}
126126

127+
/**
128+
* Give a {@link DynamoCacheWriter} to the cache to be built.
129+
* Defaults to {@link DefaultDynamoCacheWriter}.
130+
*
131+
* @param writer a writer doing the actual cache operations.
132+
* @return this buzilder for chaining.
133+
*/
134+
public DynamoCacheBuilder withWriter(DynamoCacheWriter writer) {
135+
this.writer = writer;
136+
return this;
137+
}
138+
127139
}

src/main/java/com/dasburo/spring/cache/dynamo/autoconfigure/DynamoCacheAutoConfiguration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
1818
import com.dasburo.spring.cache.dynamo.DynamoCacheBuilder;
1919
import com.dasburo.spring.cache.dynamo.DynamoCacheManager;
20+
import com.dasburo.spring.cache.dynamo.DynamoCacheWriter;
21+
import com.dasburo.spring.cache.dynamo.serializer.StringSerializer;
2022
import org.springframework.beans.factory.annotation.Autowired;
2123
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2224
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -77,6 +79,8 @@ private List<DynamoCacheBuilder> dynamoCacheBuilders() {
7779
.withFlushOnBoot(dynamoCacheProperties.isFlushOnBoot())
7880
.withReadCapacityUnit(dynamoCacheProperties.getReadCapacityUnits())
7981
.withWriteCapacityUnit(dynamoCacheProperties.getWriteCapacityUnits())
82+
.withSerializer(new StringSerializer())
83+
.withWriter(DynamoCacheWriter.nonLockingDynamoCacheWriter(dynamoTemplate))
8084
);
8185
}
8286
}

src/main/java/com/dasburo/spring/cache/dynamo/serializer/SerializationUtils.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
public abstract class SerializationUtils {
2020

21-
static final byte[] EMPTY_ARRAY = new byte[0];
22-
2321
private SerializationUtils() {
2422
throw new IllegalStateException("Utility class");
2523
}

src/test/java/com/dasburo/spring/cache/dynamo/DynamoCacheTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
*/
1515
package com.dasburo.spring.cache.dynamo;
1616

17+
import com.dasburo.spring.cache.dynamo.helper.Address;
1718
import com.dasburo.spring.cache.dynamo.serializer.Jackson2JsonSerializer;
18-
import com.dasburo.spring.cache.dynamo.serializer.StringSerializer;
1919
import org.junit.Assert;
2020
import org.junit.Before;
2121
import org.junit.ClassRule;
@@ -40,7 +40,7 @@
4040
public class DynamoCacheTest {
4141

4242
private static final String CACHE_NAME = "cache";
43-
private static final Duration TTL = Duration.ZERO;
43+
private static final Duration TTL = Duration.ofSeconds(10);
4444

4545
@ClassRule
4646
public static TestDbCreationRule dynamoDB = new TestDbCreationRule();
@@ -55,6 +55,7 @@ public void setup() {
5555
writer = spy(writer);
5656

5757
DynamoCacheConfiguration config = DynamoCacheConfiguration.defaultCacheConfig();
58+
config.setTtl(TTL);
5859
config.setFlushOnBoot(true);
5960

6061
cache = new DynamoCache(CACHE_NAME, writer, config);

src/test/java/com/dasburo/spring/cache/dynamo/TestConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public AmazonDynamoDB amazonDynamoDB(AWSCredentialsProvider amazonAWSCredentials
6161

6262
@Bean
6363
public DynamoCacheWriter dynamoCacheWriter(AmazonDynamoDB amazonDynamoDB) {
64-
return new DefaultDynamoCacheWriter(amazonDynamoDB);
64+
return DynamoCacheWriter.lockingDynamoCacheWriter(amazonDynamoDB);
6565
}
6666

6767
}

src/test/java/com/dasburo/spring/cache/dynamo/autoconfigure/DynamoCacheAutoConfigurationTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public class DynamoCacheAutoConfigurationTest extends UnitTestBase {
3737
private static final String CACHE_NAME = "cache";
3838
private static final boolean FLUSH_ON_BOOT = false;
3939
private static final Duration TTL = Duration.ofSeconds(30);
40+
private static final Long READ_CAPACITY_UNITS = 1L;
41+
private static final Long WRITE_CAPACITY_UNITS = 1L;
4042

4143
@ClassRule
4244
public static TestDbCreationRule dynamoDB = new TestDbCreationRule();
@@ -50,7 +52,9 @@ public void load() {
5052
new Class<?>[]{DynamoCacheAutoConfiguration.class},
5153
"spring.cache.dynamo.caches[0].ttl:" + TTL,
5254
"spring.cache.dynamo.caches[0].cacheName:" + CACHE_NAME,
53-
"spring.cache.dynamo.caches[0].flushOnBoot:" + FLUSH_ON_BOOT
55+
"spring.cache.dynamo.caches[0].flushOnBoot:" + FLUSH_ON_BOOT,
56+
"spring.cache.dynamo.caches[0].readCapacityUnits:" + READ_CAPACITY_UNITS,
57+
"spring.cache.dynamo.caches[0].writeCapacityUnits:" + WRITE_CAPACITY_UNITS
5458
);
5559
}
5660

src/test/java/com/dasburo/spring/cache/dynamo/Address.java renamed to src/test/java/com/dasburo/spring/cache/dynamo/helper/Address.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* See the License for the specific language governing permissions and
1313
* limitations under the License.
1414
*/
15-
package com.dasburo.spring.cache.dynamo;
15+
package com.dasburo.spring.cache.dynamo.helper;
1616

1717
import java.io.Serializable;
1818

src/test/java/com/dasburo/spring/cache/dynamo/Company.java renamed to src/test/java/com/dasburo/spring/cache/dynamo/helper/Company.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* See the License for the specific language governing permissions and
1313
* limitations under the License.
1414
*/
15-
package com.dasburo.spring.cache.dynamo;
15+
package com.dasburo.spring.cache.dynamo.helper;
1616

1717
import java.io.Serializable;
1818

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* https://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
package com.dasburo.spring.cache.dynamo.helper;
16+
17+
public class NotSerializeable {
18+
}

0 commit comments

Comments
 (0)