Skip to content

Commit

Permalink
Set data key in AerospikeCache
Browse files Browse the repository at this point in the history
  • Loading branch information
agrgr committed Jun 10, 2024
1 parent 1a43ab6 commit d99e2a2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -180,37 +198,43 @@ 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")
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() {
Expand All @@ -221,8 +245,6 @@ public int getNoOfCalls() {
@AllArgsConstructor
public static class CachedObject {

@Id
private final String id;
private final String value;

public String getValue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, AerospikeCacheConfiguration> aerospikeCacheConfigurationMap = new HashMap<>();
Expand Down

0 comments on commit d99e2a2

Please sign in to comment.