0

From this doc, looks like caffeine cache is thread safe, when using manual cache population cache.put(key, value), what happens if put failed due to another thread is writing to the same key? (Or will it fail due to another thread is accessing the same key?). Sees this put method does not provide a return value to indicate success or failure, and would throw only on key value being null.

I seem to have find limited documentation, and would love to learn more about details regarding multi-thread access of the caffeine cache.

Stu
  • 30,392
  • 6
  • 14
  • 33
Yituo
  • 1,458
  • 4
  • 17
  • 26
  • You could use the `asMap()` view's methods like `put`, `putIfAbsent`, `replace`, or computes. The `Cache` interface is opinionated that as a transient data store you probably shouldn't inspect the state and instead load through it. The reality is that often times you do want to manage it, so the `Map` view and similar offer the more open-ended functionality. We also wanted to keep the core interfaces small and piggyback on the Collections Framework to minimize the conceptual weight. – Ben Manes Aug 01 '23 at 21:23
  • Thanks @BenManes. Just to clarify the behavior here: Is that to say that the behavior of put, putIfAbsent, replace is threadsafe, but could fail silently if there is thread contention, but it is ok(and I agree) as you can just load it next time you read? – Yituo Aug 01 '23 at 22:01
  • The methods won’t fail silently, are atomic, and linearizable. It mirrors ConcurrentHashMap, which blocks concurrent writes to the same mapping. – Ben Manes Aug 02 '23 at 01:18

0 Answers0