Skip to content

Support store-by-value in ConcurrentMapCacheManager [SPR-13758] #18331

@spring-projects-issues

Description

@spring-projects-issues

member sound opened SPR-13758 and commented

If you run the following code, the @Cachable method will not always return the same results (but it should).

@RestController 
@RequestMapping("/rest")
public class TestRunner {
	@Autowired
	private TestController controller;
	
	@ResponseStatus(HttpStatus.OK)
	@RequestMapping(value = "/test", method = RequestMethod.GET)
        @PostConstruct
	public void testCaching() {
		controller.test();
		controller.test();
	}
}
public class TestService {
	@Cacheable("test")
	public Set<String> get() {
		Set<String> set = new HashSet<>();
		set.add("ASD");
		set.add("123");
		return set;
	}
}
@Controller
public class TestController {
	@Autowired
	private TestService testService;

	public void test() {
		Set<String> list = testService.get();
		Assert.assertEquals(2, list.size()); //fails when run the 2nd time with: junit.framework.AssertionFailedError: expected:<2> but was:<1>

		Iterator<String> it = list.iterator();
		it.next();
		it.remove(); //removes the first item
	}
}
@Bean 
public CacheManager cacheManager() { 
     return new ConcurrentMapCacheManager(); 
}

Steps to reproduce the error: run http://localhost:8080/rest/test

I noticed that if I use a @PostConstruct method running the controller.test() method twice, this would not cause the error. So the application starts up fine, but invoking the servlet explicit triggers the failure.


Affects: 4.2.3

Issue Links:

Referenced from: commits cf20308, 0194988

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions