Skip to content

Commit

Permalink
use hashCode userKey instead of String in AerospikeCache, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
agrgr committed Jun 10, 2024
1 parent a27d434 commit cc0ebaf
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,12 @@ public ValueWrapper putIfAbsent(Object key, Object value) {
}

private Key getKey(Object key) {
return new Key(cacheConfiguration.getNamespace(), cacheConfiguration.getSet(), key.toString());
return new Key(cacheConfiguration.getNamespace(), cacheConfiguration.getSet(), key.hashCode());
}

private void serializeAndPut(WritePolicy writePolicy, Object key, Object value) {
AerospikeWriteData data = AerospikeWriteData.forWrite(cacheConfiguration.getNamespace());
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, aerospikeKey, data.getBinsAsArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package org.springframework.data.aerospike.cache;

import com.aerospike.client.IAerospikeClient;
import com.aerospike.client.Key;
import lombok.AllArgsConstructor;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -28,13 +28,16 @@
import org.springframework.data.aerospike.core.AerospikeOperations;
import org.springframework.data.aerospike.util.AwaitilityUtils;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.data.aerospike.util.AwaitilityUtils.awaitTenSecondsUntil;

public class AerospikeCacheManagerIntegrationTests extends BaseBlockingIntegrationTests {

private static final String KEY = "foo";
private static final String KEY_THAT_MATCHES_CONDITION = "abcdef";
private static final String STRING_PARAM = "foo";
private static final String STRING_PARAM_THAT_MATCHES_CONDITION = "abcdef";
private static final String VALUE = "bar";

@Autowired
Expand All @@ -49,14 +52,53 @@ public class AerospikeCacheManagerIntegrationTests extends BaseBlockingIntegrati
@AfterEach
public void tearDown() {
cachingComponent.reset();
client.delete(null, new Key(getNameSpace(), DEFAULT_SET_NAME, KEY));
client.delete(null, new Key(getNameSpace(), DEFAULT_SET_NAME, KEY_THAT_MATCHES_CONDITION));
client.truncate(null, getNameSpace(), DEFAULT_SET_NAME, null);
client.truncate(null, getNameSpace(), DIFFERENT_SET_NAME, null);
Awaitility.await().atMost(1, TimeUnit.SECONDS).until(() -> true);
}

@Test
public void shouldCache() {
CachedObject response1 = cachingComponent.cacheableMethod(KEY);
CachedObject response2 = cachingComponent.cacheableMethod(KEY);
CachedObject response1 = cachingComponent.cacheableMethod(STRING_PARAM);
CachedObject response2 = cachingComponent.cacheableMethod(STRING_PARAM);

assertThat(response1).isNotNull();
assertThat(response1.getValue()).isEqualTo(VALUE);
assertThat(response2).isNotNull();
assertThat(response2.getValue()).isEqualTo(VALUE);
assertThat(cachingComponent.getNoOfCalls()).isEqualTo(1);
}

@Test
public void shouldCacheWithNumericParam() {
CachedObject response1 = cachingComponent.cacheableMethodWithNumericParam(100L);
CachedObject response2 = cachingComponent.cacheableMethodWithNumericParam((long) 'd');

assertThat(response1).isNotNull();
assertThat(response1.getValue()).isEqualTo(VALUE);
assertThat(response2).isNotNull();
assertThat(response2.getValue()).isEqualTo(VALUE);
assertThat(cachingComponent.getNoOfCalls()).isEqualTo(1);
}

@Test
public void shouldCacheWithMapParam() {
Map<String, String> map = Map.of("1", "val1", "2", "val2", "3", "val3", "4", "val4");
CachedObject response1 = cachingComponent.cacheableMethodWithMapParam(map);
CachedObject response2 = cachingComponent.cacheableMethodWithMapParam(map);

assertThat(response1).isNotNull();
assertThat(response1.getValue()).isEqualTo(map);
assertThat(response2).isNotNull();
assertThat(response2.getValue()).isEqualTo(map);
assertThat(cachingComponent.getNoOfCalls()).isEqualTo(1);
}

@Test
public void shouldCacheWithMultipleParams() {
Map<String, String> map = Map.of("1", "val1", "2", "val2", "3", "val3", "4", "val4");
CachedObject response1 = cachingComponent.cacheableMethodWithMultipleParams(STRING_PARAM, 100, map);
CachedObject response2 = cachingComponent.cacheableMethodWithMultipleParams(STRING_PARAM, 100, map);

assertThat(response1).isNotNull();
assertThat(response1.getValue()).isEqualTo(VALUE);
Expand All @@ -67,9 +109,9 @@ public void shouldCache() {

@Test
public void shouldCacheUsingDefaultSet() {
// default cache configuration is used for cache names not pre-configured via AerospikeCacheManager
CachedObject response1 = cachingComponent.cacheableMethodDefaultCache(KEY);
CachedObject response2 = cachingComponent.cacheableMethod(KEY);
// default cache configuration is used for all cache names not pre-configured via AerospikeCacheManager
CachedObject response1 = cachingComponent.cacheableMethodDefaultCache(STRING_PARAM);
CachedObject response2 = cachingComponent.cacheableMethod(STRING_PARAM);

assertThat(response1).isNotNull();
assertThat(response1.getValue()).isEqualTo(VALUE);
Expand All @@ -81,8 +123,8 @@ public void shouldCacheUsingDefaultSet() {

@Test
public void shouldCacheUsingDifferentSet() {
CachedObject response1 = cachingComponent.cacheableMethodDifferentExistingCache(KEY);
CachedObject response2 = cachingComponent.cacheableMethodDifferentExistingCache(KEY);
CachedObject response1 = cachingComponent.cacheableMethodDifferentExistingCache(STRING_PARAM);
CachedObject response2 = cachingComponent.cacheableMethodDifferentExistingCache(STRING_PARAM);

assertThat(response1).isNotNull();
assertThat(response1.getValue()).isEqualTo(VALUE);
Expand All @@ -92,7 +134,7 @@ public void shouldCacheUsingDifferentSet() {
assertThat(aerospikeOperations.count(DIFFERENT_SET_NAME)).isEqualTo(1);
assertThat(aerospikeOperations.count(DEFAULT_SET_NAME)).isEqualTo(0);

CachedObject response3 = cachingComponent.cacheableMethod(KEY);
CachedObject response3 = cachingComponent.cacheableMethod(STRING_PARAM);
assertThat(response3).isNotNull();
assertThat(response3.getValue()).isEqualTo(VALUE);
assertThat(cachingComponent.getNoOfCalls()).isEqualTo(2);
Expand All @@ -102,9 +144,9 @@ public void shouldCacheUsingDifferentSet() {

@Test
public void shouldEvictCache() {
CachedObject response1 = cachingComponent.cacheableMethod(KEY);
cachingComponent.cacheEvictMethod(KEY);
CachedObject response2 = cachingComponent.cacheableMethod(KEY);
CachedObject response1 = cachingComponent.cacheableMethod(STRING_PARAM);
cachingComponent.cacheEvictMethod(STRING_PARAM);
CachedObject response2 = cachingComponent.cacheableMethod(STRING_PARAM);

assertThat(response1).isNotNull();
assertThat(response1.getValue()).isEqualTo(VALUE);
Expand All @@ -114,10 +156,10 @@ public void shouldEvictCache() {
}

@Test
public void shouldNotEvictCacheEvictingDifferentKey() {
CachedObject response1 = cachingComponent.cacheableMethod(KEY);
cachingComponent.cacheEvictMethod("not-the-relevant-key");
CachedObject response2 = cachingComponent.cacheableMethod(KEY);
public void shouldNotEvictCacheEvictingDifferentParam() {
CachedObject response1 = cachingComponent.cacheableMethod(STRING_PARAM);
cachingComponent.cacheEvictMethod("not-the-relevant-param");
CachedObject response2 = cachingComponent.cacheableMethod(STRING_PARAM);

assertThat(response1).isNotNull();
assertThat(response1.getValue()).isEqualTo(VALUE);
Expand All @@ -128,20 +170,25 @@ public void shouldNotEvictCacheEvictingDifferentKey() {

@Test
public void shouldCacheUsingCachePut() {
CachedObject response1 = cachingComponent.cachePutMethod(KEY);
CachedObject response2 = cachingComponent.cacheableMethod(KEY);
CachedObject response1 = cachingComponent.cachePutMethod(STRING_PARAM);
CachedObject response2 = cachingComponent.cacheableMethod(STRING_PARAM);

assertThat(response1).isNotNull();
assertThat(response1.getValue()).isEqualTo(VALUE);
assertThat(response2).isNotNull();
assertThat(response2.getValue()).isEqualTo(VALUE);
assertThat(cachingComponent.getNoOfCalls()).isEqualTo(1);

CachedObject response3 = cachingComponent.cachePutMethod(STRING_PARAM);
assertThat(response3).isNotNull();
assertThat(response3.getValue()).isEqualTo(VALUE);
assertThat(cachingComponent.getNoOfCalls()).isEqualTo(2);
}

@Test
public void shouldCacheKeyMatchesCondition() {
CachedObject response1 = cachingComponent.cacheableWithCondition(KEY_THAT_MATCHES_CONDITION);
CachedObject response2 = cachingComponent.cacheableWithCondition(KEY_THAT_MATCHES_CONDITION);
CachedObject response1 = cachingComponent.cacheableWithCondition(STRING_PARAM_THAT_MATCHES_CONDITION);
CachedObject response2 = cachingComponent.cacheableWithCondition(STRING_PARAM_THAT_MATCHES_CONDITION);

assertThat(response1).isNotNull();
assertThat(response1.getValue()).isEqualTo(VALUE);
Expand All @@ -152,8 +199,8 @@ public void shouldCacheKeyMatchesCondition() {

@Test
public void shouldNotCacheKeyDoesNotMatchCondition() {
CachedObject response1 = cachingComponent.cacheableWithCondition(KEY);
CachedObject response2 = cachingComponent.cacheableWithCondition(KEY);
CachedObject response1 = cachingComponent.cacheableWithCondition(STRING_PARAM);
CachedObject response2 = cachingComponent.cacheableWithCondition(STRING_PARAM);

assertThat(response1).isNotNull();
assertThat(response1.getValue()).isEqualTo(VALUE);
Expand All @@ -164,8 +211,8 @@ public void shouldNotCacheKeyDoesNotMatchCondition() {

@Test
public void shouldCacheWithConfiguredTTL() {
CachedObject response1 = cachingComponent.cacheableMethodWithTTL(KEY);
CachedObject response2 = cachingComponent.cacheableMethodWithTTL(KEY);
CachedObject response1 = cachingComponent.cacheableMethodWithTTL(STRING_PARAM);
CachedObject response2 = cachingComponent.cacheableMethodWithTTL(STRING_PARAM);

assertThat(cachingComponent.getNoOfCalls()).isEqualTo(1);
assertThat(response1).isNotNull();
Expand All @@ -174,7 +221,7 @@ public void shouldCacheWithConfiguredTTL() {
assertThat(response2.getValue()).isEqualTo(VALUE);

awaitTenSecondsUntil(() -> {
CachedObject response3 = cachingComponent.cacheableMethodWithTTL(KEY);
CachedObject response3 = cachingComponent.cacheableMethodWithTTL(STRING_PARAM);
assertThat(cachingComponent.getNoOfCalls()).isEqualTo(2);
assertThat(response3).isNotNull();
assertThat(response3.getValue()).isEqualTo(VALUE);
Expand All @@ -183,8 +230,8 @@ public void shouldCacheWithConfiguredTTL() {

@Test
public void shouldCacheUsingAnotherCacheManager() {
CachedObject response1 = cachingComponent.cacheableMethodWithAnotherCacheManager(KEY);
CachedObject response2 = cachingComponent.cacheableMethodWithAnotherCacheManager(KEY);
CachedObject response1 = cachingComponent.cacheableMethodWithAnotherCacheManager(STRING_PARAM);
CachedObject response2 = cachingComponent.cacheableMethodWithAnotherCacheManager(STRING_PARAM);

assertThat(response1).isNotNull();
assertThat(response1.getValue()).isEqualTo(VALUE);
Expand All @@ -195,7 +242,7 @@ public void shouldCacheUsingAnotherCacheManager() {

@Test
public void shouldNotClearCacheClearingDifferentCache() {
CachedObject response1 = cachingComponent.cacheableMethod(KEY);
CachedObject response1 = cachingComponent.cacheableMethod(STRING_PARAM);
assertThat(aerospikeOperations.count(DEFAULT_SET_NAME)).isEqualTo(1);
aerospikeCacheManager.getCache(DIFFERENT_EXISTING_CACHE).clear();
AwaitilityUtils.awaitTwoSecondsUntil(() -> {
Expand All @@ -213,26 +260,44 @@ public void reset() {
noOfCalls = 0;
}

@Cacheable("TEST") // "TEST" is a cache name not pre-configured in AerospikeCacheManager, so goes to default set
public CachedObject cacheableMethod(String param) {
noOfCalls++;
return new CachedObject(VALUE);
}

@Cacheable("TEST")
public CachedObject cacheableMethodWithNumericParam(Number param) {
noOfCalls++;
return new CachedObject(VALUE);
}

@Cacheable("TEST")
public CachedObject cacheableMethodWithMapParam(Map<String, String> param) {
noOfCalls++;
return new CachedObject(param);
}

@Cacheable("TEST")
public CachedObject cacheableMethod(String key) {
public CachedObject cacheableMethodWithMultipleParams(String param1, int param2, Map<String, String> param3) {
noOfCalls++;
return new CachedObject(VALUE);
}

@Cacheable("TEST12345ABC")
public CachedObject cacheableMethodDefaultCache(String key) {
@Cacheable("TEST12345ABC") // Cache name not pre-configured in AerospikeCacheManager, so it goes to default set
public CachedObject cacheableMethodDefaultCache(String param) {
noOfCalls++;
return new CachedObject(VALUE);
}

@Cacheable(DIFFERENT_EXISTING_CACHE)
public CachedObject cacheableMethodDifferentExistingCache(String key) {
public CachedObject cacheableMethodDifferentExistingCache(String param) {
noOfCalls++;
return new CachedObject(VALUE);
}

@Cacheable(value = CACHE_WITH_TTL)
public CachedObject cacheableMethodWithTTL(String key) {
public CachedObject cacheableMethodWithTTL(String param) {
noOfCalls++;
return new CachedObject(VALUE);
}
Expand All @@ -248,13 +313,13 @@ public void cacheEvictMethod(String param) {
}

@CachePut("TEST")
public CachedObject cachePutMethod(String key) {
public CachedObject cachePutMethod(String param) {
noOfCalls++;
return new CachedObject(VALUE);
}

@Cacheable(value = "TEST", condition = "#key.startsWith('abc')")
public CachedObject cacheableWithCondition(String key) {
@Cacheable(value = "TEST", condition = "#param.startsWith('abc')")
public CachedObject cacheableWithCondition(String param) {
noOfCalls++;
return new CachedObject(VALUE);
}
Expand All @@ -267,9 +332,9 @@ public int getNoOfCalls() {
@AllArgsConstructor
public static class CachedObject {

private final String value;
private final Object value;

public String getValue() {
public Object getValue() {
return value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public CacheManager cacheManager(IAerospikeClient aerospikeClient, MappingAerosp
public CacheManager anotherCacheManager(IAerospikeClient aerospikeClient,
MappingAerospikeConverter aerospikeConverter) {
AerospikeCacheConfiguration defaultCacheConfiguration = new AerospikeCacheConfiguration(namespace,
BaseIntegrationTests.DEFAULT_SET_NAME);
BaseIntegrationTests.DIFFERENT_SET_NAME);
return new AerospikeCacheManager(aerospikeClient, aerospikeConverter, defaultCacheConfiguration);
}

Expand Down

0 comments on commit cc0ebaf

Please sign in to comment.