0

I want to use below code in my application

LoadingCache<Key, Graph> graphs = Caffeine.newBuilder()
    .maximumSize(10_000)
    .build(key -> createExpensiveGraph(key));

But, I am so dumb in multithreading that I could not figure it out how do I implement the function createExpensiveGraph. Please help.

user3289405
  • 127
  • 1
  • 9
  • 2
    This [tutorial](https://www.baeldung.com/java-caching-caffeine) might help. It could simply be `return 42;`. The cache will invoke it atomically per-key, but it may be called concurrently across different keys. So any shared state (like an http client) has to be used in a threadsafe manner. – Ben Manes Jun 21 '19 at 16:21
  • The tutorial link did help. thank you! – user3289405 Jun 21 '19 at 19:26

1 Answers1

2

The createExpensiveGraph is just a placeholder for whatever function you would like to cache. It could be to retrieve a value from a database, make a HTTP call, or compute a computationally expensive value. Depending on what you would like to cache, you have to implement - and name appropriately - this function yourself.

Simon Forsberg
  • 13,086
  • 10
  • 64
  • 108