Java – use guava cache without load function

My java application has a cache. I want to replace the current cache implementation with guava cache

Unfortunately, my application's cache usage doesn't seem to match the way guava cache seems to work All I want is to be able to create an empty cache, read an item from the cache using the "get" method, and store the item in the cache using the "put" method I don't want the "get" call to try to add an item to the cache

It seems that the loadcache class has the get and put methods I need However, there is no "load" function, and I can't find out how to create a cache

My first attempt was this:

LoadingCache<String,String> CACHE = CacheBuilder.newBuilder().build();

But this can lead to compiler errors: incompatible types; There are no instances of type variables K1 and V1, making the cache conform to loadingcache

Obviously, I have to pass a cacheloader with a "load" method

I think I can create a cacheloader, which has a "load" method and just throws an exception, but this seems to be a strange and inefficient thing

Solution

CacheBuilder. Build () returns an unloaded cache Just use what you want

Cache<String,String> cache = CacheBuilder.newBuilder().build();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>