There are two ways of getting the size - counting objects in the cache, or getting their weights. This type of eviction assumes that eviction occurs when the configured size limit of the cache is exceeded. Sometimes we need to invalidate some cached values manually: That’s why using get is preferable to getIfPresent. This means that the computation will be made only once - even if several threads ask for the value simultaneously. The get method performs the computation atomically.
get(key, k -> DataObject.get("Data for A")) ĪssertEquals("Data for A", dataObject.getData()) This function will be used for providing the fallback value if the key is not present in the cache, which would be inserted in the cache after computation: dataObject = cache We can also get the value using the get method, which takes a Function along with a key as an argument. We can populate the cache manually using the put method: cache.put(key, dataObject) This method will return null if the value is not present in the cache: String key = "A" ĭataObject dataObject = cache.getIfPresent(key) Now, we can get some value from the cache using the getIfPresent method.