1

I want to use Caffeine for caching and I need to have a write-behind. I want to limit the amount of times I write to the database. The documentation speaks of write-back cache so it should be possible, but there is no example there on how to configure it. I have implemented a CacheWriter, but I don't understand how to configure it to for example only call the writer once every 10 seconds (If something changed to the cache ofcourse).

Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211

1 Answers1

2

CacheWriter is an extension point and the documentation describes the use-cases where it may make sense. Those cases are beyond the scope of the library and if implemented instead could have been too rigid.

The writer is called atomically during a normal write operation (but not a computation). This ensures that a sequential order of changes is observed for a given key. For write-behind the writer would add the entry into a queue that is processed asynchronously, e.g. to batch the operations.

When implementing this capability you might want to consider,

  • Coalescing the updates (e.g. collect into a LinkedHashMap)
  • Performing a batch prior to a periodic write-behind if it exceeds a threshold size
  • Loading from the write-behind buffer if the operations have not yet been flushed (This avoids an inconsistent view, e.g. due to eviction)
  • Handling retrials, rate limiting, and striping depending on the the characteristics of the external resource

Update:

Wim Deblauwe provided a nice example using RxJava.

Ben Manes
  • 9,178
  • 3
  • 35
  • 39
  • Ah, that was totally not clear. Maybe the first section that has no title can get a title "Usage" and the other 3 titles can be grouped under a new title "Possible use cases". Maybe even with a note that these are things that are possible to implement on top of that `CacheWriter` but are not available in Caffeine. In the end, I went with Hazelcast as they have write-behind out of the box. – Wim Deblauwe Apr 27 '16 at 17:54
  • 2
    Thanks, I'll improve the documentation then. I think a community extension to offer this capability would be nice. I'd like Caffeine too feel like a library (flexible, small, obvious, focused) to avoid the mistakes many framework authors make. Hopefully it is extensible enough to be used as a building block to handle aspects outside of its scope. – Ben Manes Apr 27 '16 at 18:17