0

Since Java 8 we can use .compute* methods on ConcurrentHashMap to synchronize processing by key, so that if two threads execute .compute* method on the same key at the same time, callbacks still will be executed one after another and not simultaneously. But ConcurrentHashMap doesn't provide ability to delete data in timely fashion as caches usually allow.

Guava/Caffeine caches provide ability to automatically delete values based on time, but you don't have that nasty feature of synchronized processing based on key, as in ConcurrentHashMap (you can get ConcurrentMap using asMap method, but .compute* implementations doesn't provide synchronization based on key)

My goal is to have both processing synchronization by key as in ConcurrentHashMap AND removals by time as in Guava/Caffeine.

What is the best way to achieve it in Java?

Kivan
  • 1,712
  • 1
  • 14
  • 30
  • 1
    Caffeine implements the compute methods to have the same atomicity as ConcurrentHashMap in it’s asMap view. – Ben Manes Sep 06 '19 at 13:24
  • @BenManes thx, it seems Caffeine really implements this feature, I was a bit undertested it. What about Guava, am I right, that it doesn't just to be sure? – Kivan Sep 06 '19 at 15:00
  • Support was added in v21 but it’s buggy. – Ben Manes Sep 06 '19 at 15:12
  • @BenManes our project uses version 20 lol, just one version behind from what we needed ) What kind of bugs are there, do you have ticket links? – Kivan Sep 06 '19 at 15:24
  • [#2863](https://github.com/google/guava/issues/2863), [#2827](https://github.com/google/guava/issues/2827), [#2743](https://github.com/google/guava/issues/2743), [corfu/#1878](https://github.com/CorfuDB/CorfuDB/issues/1878) are the ones that I am aware of. Some fixed after 21, others unresolved. – Ben Manes Sep 06 '19 at 16:08

1 Answers1

0

I was wrong about Caffeine - it supports atomic operations with compute. Guava support was added since 21 version.

Kivan
  • 1,712
  • 1
  • 14
  • 30