<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=macromaniac" rel="nofollow">Andrey Karandey</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-9377?redirect=false" rel="nofollow">SPR-9377</a></strong> and commented</p>
<p dir="auto">Key generating has a weak hashing function. Next results are equal:</p>
<p dir="auto">generate( object1,method1,new Integer( 109 ),new Integer( 434)));<br>
generate( object1,method1,new Integer( 110 ),new Integer( 403)));</p>
<p dir="auto">It was pity to catch it on production...</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 3.1.1</p>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398156862" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/14870" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/14870/hovercard" href="https://github.com/spring-projects/spring-framework/issues/14870">#14870</a> Cacheable key collision with DefaultKeyGenerator (<em><strong>"duplicates"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398116727" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/13675" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/13675/hovercard" href="https://github.com/spring-projects/spring-framework/issues/13675">#13675</a> Improve DefaultKeyGenerator</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398156862" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/14870" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/14870/hovercard" href="https://github.com/spring-projects/spring-framework/issues/14870">#14870</a> Cacheable key collision with DefaultKeyGenerator</li>
</ul>
<p dir="auto">3 votes, 4 watchers</p>
<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=bozho" rel="nofollow">Bozhidar Bozhanov</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-9036?redirect=false" rel="nofollow">SPR-9036</a></strong> and commented</p>
<p dir="auto">org.springframework.cache.interceptor.DefaultKeyGenerator is implemented by calculating the hashcode of all arguments. This is problematic in many cases:</p>
<ul dir="auto">
<li>methods with similar signature - foo(int, String), foo(int, List)  - if the 2nd argument is null they will get mixed</li>
<li>methods with the same signature in different classes, using the same cache - they will get mixed</li>
<li>is there a collision resolution mechanism? If not, things will randomly fail. Rarely, but they will. Imagine that the arguments of two distinct methods (that use the same cache region) evaluate to the same hashcode (not unlikely) - they will override each other's values, and possibly a ClassCastException will occur on retrieval</li>
</ul>
<p dir="auto">What is in the first place the reason to get the hashcodes? The underlying cache mechanism is a hashtable of some sort, so it will calculate the hash of the key anyway. But it will have a collision-resolution strategy, unlike the KeyGenerator.</p>
<p dir="auto">I suggest:</p>
<ul dir="auto">
<li>concatenating the string representations of primitives, and the hashcode of non-primitives.</li>
<li>adding the class name and method name to the key</li>
<li>optionally, where non-primitive values are used as keys, log a warning - this is usually wrong, as the hashcode will change even though the values inside the object are the same</li>
</ul>
<hr>
<p dir="auto"><strong>Attachments:</strong></p>
<ul dir="auto">
<li><a href="https://jira.spring.io/secure/attachment/20806/TestDefaultKeyGenerator.java" rel="nofollow">TestDefaultKeyGenerator.java</a> (<em>2.01 kB</em>)</li>
</ul>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398156862" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/14870" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/14870/hovercard" href="https://github.com/spring-projects/spring-framework/issues/14870">#14870</a> Cacheable key collision with DefaultKeyGenerator (<em><strong>"duplicates"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398156862" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/14870" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/14870/hovercard" href="https://github.com/spring-projects/spring-framework/issues/14870">#14870</a> Cacheable key collision with DefaultKeyGenerator</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398118942" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/14013" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/14013/hovercard" href="https://github.com/spring-projects/spring-framework/issues/14013">#14013</a> org.springframework.cache.interceptor.DefaultKeyGenerator has too weak hashing functionality</li>
</ul>
<p dir="auto">9 votes, 11 watchers</p>