|
25 | 25 | import java.io.Serializable;
|
26 | 26 | import java.nio.charset.Charset;
|
27 | 27 | import java.util.Collection;
|
| 28 | +import java.util.Collections; |
28 | 29 | import java.util.Date;
|
29 | 30 | import java.util.function.Consumer;
|
30 | 31 |
|
|
36 | 37 | import org.junit.runners.Parameterized.Parameters;
|
37 | 38 | import org.springframework.cache.Cache.ValueWrapper;
|
38 | 39 | import org.springframework.cache.interceptor.SimpleKey;
|
| 40 | +import org.springframework.cache.interceptor.SimpleKeyGenerator; |
39 | 41 | import org.springframework.cache.support.NullValue;
|
40 | 42 | import org.springframework.data.redis.ConnectionFactoryTracker;
|
41 | 43 | import org.springframework.data.redis.connection.RedisConnection;
|
42 | 44 | import org.springframework.data.redis.connection.RedisConnectionFactory;
|
43 |
| -import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; |
44 | 45 | import org.springframework.data.redis.serializer.RedisSerializationContext.SerializationPair;
|
45 | 46 | import org.springframework.data.redis.serializer.RedisSerializer;
|
46 | 47 |
|
@@ -300,6 +301,58 @@ public void fetchKeyWithComputedPrefixReturnsExpectedResult() {
|
300 | 301 | assertThat(result.get()).isEqualTo(sample);
|
301 | 302 | }
|
302 | 303 |
|
| 304 | + @Test // DATAREDIS-1032 |
| 305 | + public void cacheShouldAllowListKeyCacheKeysOfSimpleTypes() { |
| 306 | + |
| 307 | + Object key = SimpleKeyGenerator.generateKey(Collections.singletonList("my-cache-key-in-a-list")); |
| 308 | + cache.put(key, sample); |
| 309 | + |
| 310 | + Object target = cache.get(SimpleKeyGenerator.generateKey(Collections.singletonList("my-cache-key-in-a-list"))); |
| 311 | + assertThat(((ValueWrapper) target).get()).isEqualTo(sample); |
| 312 | + } |
| 313 | + |
| 314 | + @Test // DATAREDIS-1032 |
| 315 | + public void cacheShouldAllowArrayKeyCacheKeysOfSimpleTypes() { |
| 316 | + |
| 317 | + Object key = SimpleKeyGenerator.generateKey(new String[] { "my-cache-key-in-an-array" }); |
| 318 | + cache.put(key, sample); |
| 319 | + |
| 320 | + Object target = cache.get(SimpleKeyGenerator.generateKey(new String[] { "my-cache-key-in-an-array" })); |
| 321 | + assertThat(((ValueWrapper) target).get()).isEqualTo(sample); |
| 322 | + } |
| 323 | + |
| 324 | + @Test // DATAREDIS-1032 |
| 325 | + public void cacheShouldAllowListCacheKeysOfComplexTypes() { |
| 326 | + |
| 327 | + Object key = SimpleKeyGenerator |
| 328 | + .generateKey(Collections.singletonList(new ComplexKey(sample.getFirstame(), sample.getBirthdate()))); |
| 329 | + cache.put(key, sample); |
| 330 | + |
| 331 | + Object target = cache.get(SimpleKeyGenerator |
| 332 | + .generateKey(Collections.singletonList(new ComplexKey(sample.getFirstame(), sample.getBirthdate())))); |
| 333 | + assertThat(((ValueWrapper) target).get()).isEqualTo(sample); |
| 334 | + } |
| 335 | + |
| 336 | + @Test // DATAREDIS-1032 |
| 337 | + public void cacheShouldAllowMapCacheKeys() { |
| 338 | + |
| 339 | + Object key = SimpleKeyGenerator |
| 340 | + .generateKey(Collections.singletonMap("map-key", new ComplexKey(sample.getFirstame(), sample.getBirthdate()))); |
| 341 | + cache.put(key, sample); |
| 342 | + |
| 343 | + Object target = cache.get(SimpleKeyGenerator |
| 344 | + .generateKey(Collections.singletonMap("map-key", new ComplexKey(sample.getFirstame(), sample.getBirthdate())))); |
| 345 | + assertThat(((ValueWrapper) target).get()).isEqualTo(sample); |
| 346 | + } |
| 347 | + |
| 348 | + @Test // DATAREDIS-1032 |
| 349 | + public void cacheShouldFailOnNonConvertibleCacheKey() { |
| 350 | + |
| 351 | + Object key = SimpleKeyGenerator |
| 352 | + .generateKey(Collections.singletonList(new InvalidKey(sample.getFirstame(), sample.getBirthdate()))); |
| 353 | + assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> cache.put(key, sample)); |
| 354 | + } |
| 355 | + |
303 | 356 | void doWithConnection(Consumer<RedisConnection> callback) {
|
304 | 357 | RedisConnection connection = connectionFactory.getConnection();
|
305 | 358 | try {
|
|
0 commit comments