ehcache / ehcache3

Ehcache 3.x line
http://www.ehcache.org
Apache License 2.0
2k stars 579 forks source link

getCachePuts() gives incorrect value in case multiple threads accessing cache simultaneously #3166

Closed sahil3390 closed 1 year ago

sahil3390 commented 1 year ago

Two threads access(get if not present put) the cache simultaneouly and performs get/put for element 1-10 in cache . In this case it is observed that getCachePuts() return random values. Usually it is observed that getCachePuts() comes out to be 12 or 16 in case cache size is 10. As per our understanding getCachePuts() should be 10 as elements 1-10 are being accessed. The configured time to idle is 1 hour. For example in one of the run of the unit case following were the respective values: getCachePuts= 12 getCacheRemovals= 0 getCacheEvictions= 0 getCacheExpirations= 0

chrisdennis commented 1 year ago

Can you show me the actual code driving the cache here? If it's just two threads doing:

if (cache.get(key) == null) {
  cache.put(key, value);
}

in parallel then this is expected behavior. The two cache operations are not atomic, and both threads can see null and put independently.