Reuse generated key for get+put within same cacheable operation

Closes gh-31789
This commit is contained in:
Juergen Hoeller
2023-12-10 20:14:49 +01:00
parent b6364e3665
commit b510bc3bab
2 changed files with 46 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,11 +16,16 @@
package org.springframework.cache.config;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.CachingConfigurer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.CacheErrorHandler;
@@ -139,6 +144,17 @@ class EnableCachingTests extends AbstractCacheAnnotationTests {
context.close();
}
@Test
void mutableKey() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(EnableCachingConfig.class, ServiceWithMutableKey.class);
ctx.refresh();
ServiceWithMutableKey service = ctx.getBean(ServiceWithMutableKey.class);
String result = service.find(new ArrayList<>(List.of("id")));
assertThat(service.find(new ArrayList<>(List.of("id")))).isSameAs(result);
}
@Configuration
@EnableCaching
@@ -277,4 +293,14 @@ class EnableCachingTests extends AbstractCacheAnnotationTests {
}
}
static class ServiceWithMutableKey {
@Cacheable(value = "testCache", keyGenerator = "customKeyGenerator")
public String find(Collection<String> id) {
id.add("other");
return id.toString();
}
}
}