From d99e2a2ef4670c44c6788823f3a83bd07fd19986 Mon Sep 17 00:00:00 2001 From: agrgr Date: Mon, 10 Jun 2024 13:31:36 +0300 Subject: [PATCH] Set data key in AerospikeCache --- .../data/aerospike/cache/AerospikeCache.java | 6 ++- .../data/aerospike/BaseIntegrationTests.java | 1 + ...AerospikeCacheManagerIntegrationTests.java | 48 ++++++++++++++----- .../aerospike/config/CommonTestConfig.java | 2 +- 4 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/springframework/data/aerospike/cache/AerospikeCache.java b/src/main/java/org/springframework/data/aerospike/cache/AerospikeCache.java index a0631b210..529719e2b 100644 --- a/src/main/java/org/springframework/data/aerospike/cache/AerospikeCache.java +++ b/src/main/java/org/springframework/data/aerospike/cache/AerospikeCache.java @@ -200,8 +200,10 @@ private Key getKey(Object key) { } private void serializeAndPut(WritePolicy writePolicy, Object key, Object value) { - AerospikeWriteData data = AerospikeWriteData.forWrite(getKey(key).namespace); + Key aerospikeKey = getKey(key); + AerospikeWriteData data = AerospikeWriteData.forWrite(aerospikeKey.namespace); + data.setKey(aerospikeKey); // Set the key on the data object aerospikeConverter.write(value, data); - client.put(writePolicy, getKey(key), data.getBinsAsArray()); + client.put(writePolicy, aerospikeKey, data.getBinsAsArray()); } } diff --git a/src/test/java/org/springframework/data/aerospike/BaseIntegrationTests.java b/src/test/java/org/springframework/data/aerospike/BaseIntegrationTests.java index ccb74308e..fac6ffddd 100644 --- a/src/test/java/org/springframework/data/aerospike/BaseIntegrationTests.java +++ b/src/test/java/org/springframework/data/aerospike/BaseIntegrationTests.java @@ -11,6 +11,7 @@ public abstract class BaseIntegrationTests { public static final String DEFAULT_SET_NAME = "aerospike"; public static final String OVERRIDE_SET_NAME = "testSet1"; + public static final String DIFFERENT_SET_NAME = "different-set"; protected static final int MILLIS_TO_NANO = 1_000_000; @Value("${spring-data-aerospike.connection.namespace}") diff --git a/src/test/java/org/springframework/data/aerospike/cache/AerospikeCacheManagerIntegrationTests.java b/src/test/java/org/springframework/data/aerospike/cache/AerospikeCacheManagerIntegrationTests.java index e99c72f30..c05959c01 100644 --- a/src/test/java/org/springframework/data/aerospike/cache/AerospikeCacheManagerIntegrationTests.java +++ b/src/test/java/org/springframework/data/aerospike/cache/AerospikeCacheManagerIntegrationTests.java @@ -27,7 +27,6 @@ import org.springframework.data.aerospike.BaseBlockingIntegrationTests; import org.springframework.data.aerospike.core.AerospikeOperations; import org.springframework.data.aerospike.util.AwaitilityUtils; -import org.springframework.data.annotation.Id; import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.data.aerospike.util.AwaitilityUtils.awaitTenSecondsUntil; @@ -66,6 +65,25 @@ public void shouldCache() { assertThat(cachingComponent.getNoOfCalls()).isEqualTo(1); } + @Test + public void shouldCacheInDifferentSet() { + CachedObject response1 = cachingComponent.cacheableMethodDifferentExistingCache(KEY); + CachedObject response2 = cachingComponent.cacheableMethodDifferentExistingCache(KEY); + + assertThat(response1).isNotNull(); + assertThat(response1.getValue()).isEqualTo(VALUE); + assertThat(response2).isNotNull(); + assertThat(response2.getValue()).isEqualTo(VALUE); + assertThat(cachingComponent.getNoOfCalls()).isEqualTo(1); + assertThat(aerospikeOperations.count(DIFFERENT_SET_NAME)).isEqualTo(1); + assertThat(aerospikeOperations.count(DEFAULT_SET_NAME)).isEqualTo(0); + + CachedObject response3 = cachingComponent.cacheableMethod(KEY); + assertThat(cachingComponent.getNoOfCalls()).isEqualTo(2); + assertThat(aerospikeOperations.count(DIFFERENT_SET_NAME)).isEqualTo(1); + assertThat(aerospikeOperations.count(DEFAULT_SET_NAME)).isEqualTo(1); + } + @Test public void shouldEvictCache() { CachedObject response1 = cachingComponent.cacheableMethod(KEY); @@ -180,21 +198,27 @@ public void reset() { } @Cacheable("TEST") - public CachedObject cacheableMethod(String param) { + public CachedObject cacheableMethod(String key) { + noOfCalls++; + return new CachedObject(VALUE); + } + + @Cacheable("DIFFERENT-EXISTING-CACHE") + public CachedObject cacheableMethodDifferentExistingCache(String key) { noOfCalls++; - return new CachedObject("id", VALUE); + return new CachedObject(VALUE); } @Cacheable(value = "CACHE-WITH-TTL") - public CachedObject cacheableMethodWithTTL(String param) { + public CachedObject cacheableMethodWithTTL(String key) { noOfCalls++; - return new CachedObject("id", VALUE); + return new CachedObject(VALUE); } @Cacheable(value = "TEST", cacheManager = "anotherCacheManager") public CachedObject cacheableMethodWithAnotherCacheManager(String param) { noOfCalls++; - return new CachedObject("id", VALUE); + return new CachedObject(VALUE); } @CacheEvict("TEST") @@ -202,15 +226,15 @@ public void cacheEvictMethod(String param) { } @CachePut("TEST") - public CachedObject cachePutMethod(String param) { + public CachedObject cachePutMethod(String key) { noOfCalls++; - return new CachedObject("id", VALUE); + return new CachedObject(VALUE); } - @Cacheable(value = "TEST", condition = "#param.startsWith('abc')") - public CachedObject cacheableWithCondition(String param) { + @Cacheable(value = "TEST", condition = "#key.startsWith('abc')") + public CachedObject cacheableWithCondition(String key) { noOfCalls++; - return new CachedObject("id", VALUE); + return new CachedObject(VALUE); } public int getNoOfCalls() { @@ -221,8 +245,6 @@ public int getNoOfCalls() { @AllArgsConstructor public static class CachedObject { - @Id - private final String id; private final String value; public String getValue() { diff --git a/src/test/java/org/springframework/data/aerospike/config/CommonTestConfig.java b/src/test/java/org/springframework/data/aerospike/config/CommonTestConfig.java index 615439412..61dd0d6eb 100644 --- a/src/test/java/org/springframework/data/aerospike/config/CommonTestConfig.java +++ b/src/test/java/org/springframework/data/aerospike/config/CommonTestConfig.java @@ -51,7 +51,7 @@ public CacheManager cacheManager(IAerospikeClient aerospikeClient, MappingAerosp AerospikeCacheConfiguration defaultCacheConfiguration = new AerospikeCacheConfiguration(namespace, BaseIntegrationTests.DEFAULT_SET_NAME); AerospikeCacheConfiguration aerospikeCacheConfiguration = new AerospikeCacheConfiguration(namespace, - "different-set"); + BaseIntegrationTests.DIFFERENT_SET_NAME); AerospikeCacheConfiguration configurationWithTTL = new AerospikeCacheConfiguration(namespace, BaseIntegrationTests.DEFAULT_SET_NAME, 2); Map aerospikeCacheConfigurationMap = new HashMap<>();