Reputation: 75
https://github.com/ethanalef/caffeine-cache-example
Try to run CacheTest::threadedTest()
to replicate the problem
A costly IO method would be frequently accessed by multiple threads at the same time. Using L1 caching would a relief.
I have several classes for their purpose:
CaffeineConfig: Cache TTL = 300 seconds
CacheHelper: get the cached AtomicInteger, if value is get from cache, then the integer should not be incremented
Worker: work to get the atomicInteger
Manager: use ScheduledExecutorService to start the threading workers A, B, C to work
CacheTest: simulate the Manager class
When manager starts, first worker to get value = 0 and cached. second and third worker should get the value (=0) from the cache.
When manager starts, first worker to get value = 0 and cached. second and third worker get the incremented value rather than from the cache.
Does anyone have the same experience could share how should I do to fix this problem?
Upvotes: 0
Views: 215