scwanglijun / ehcache-spring-annotations

Automatically exported from code.google.com/p/ehcache-spring-annotations
0 stars 1 forks source link

HashCodeCacheKeyGenerator provides duplicate hashes #96

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
run this code:
    public static void main(String[] args) {
        HashCodeCacheKeyGenerator g = new HashCodeCacheKeyGenerator();
        System.out.println(g.generateKey(DynamicFormService.class, "getFieldByEntityIdAndTypeId", Field.class, 2, 19));
        System.out.println(g.generateKey(DynamicFormService.class, "getFieldByEntityIdAndTypeId", Field.class, 1, 50));
    }

What is the expected output? What do you see instead?
I suppose to see different hashes returned, but seeing the same for different 
set of parameters

What version of the product are you using? On what operating system?
ehcache-spring-annotations-1.2.0.jar 
ehcache-core-2.4.7.jar               
ehcache-web-2.0.4.jar                

Please provide any additional information below.
N/A

Original issue reported on code.google.com by usate...@gmail.com on 11 May 2012 at 10:38

GoogleCodeExporter commented 9 years ago
The origin of this problem is line 139 of HashCodeKeyGenerator:
  generator.hash = MULTIPLIER * generator.hash + ((Long)e).longValue();
where MULTIPLIER is 31.

With the HashCodeKeyGenerator you will always get duplicate keys. A different 
MULTIPLIER only may decrease the propability of duplicate keys.

You could use the ListCacheKeyGenerator instead of the HashCodeKeyGenerator.

Original comment by StefanFriedrichBirkner on 24 Sep 2012 at 1:21

GoogleCodeExporter commented 9 years ago
I suppose if ehcache is giving you same hashes for some combinations of
parameters it may cause some apps crashes as I encounter recently. If f.e.
app is body health critical it may cause sad result and take too long to be
found and resolved as the hashes intersection problem is not obvious.
Why not to use some bit mask constructed from parameters that added as
boolean AND to resulting hash.

On Mon, Sep 24, 2012 at 4:21 PM,
<ehcache-spring-annotations@googlecode.com>wrote:

Original comment by usate...@gmail.com on 24 Sep 2012 at 1:55

GoogleCodeExporter commented 9 years ago
the default key generator should be StringCacheKeyGenerator instead of 
HashCodeCacheKeyGenerator. Otherwise method invocation with different param 
values will have the same key! Pretty bad stuff I think.

Original comment by alexg...@gmail.com on 6 Feb 2014 at 3:16