How does Guava handle caching?
A) Through the Cache interface and CacheBuilder class
B) By using the Map interface
C) By storing data in files
D) By using the System properties
Answer:
A) Through the Cache interface and CacheBuilder class
Explanation:
Guava handles caching through the Cache
interface and the CacheBuilder
class. These provide a flexible and efficient mechanism for caching data in memory, with options for eviction policies, expiration times, and other configurations.
For example:
Cache<String, String> cache = CacheBuilder.newBuilder()
.maximumSize(1000)
.expireAfterWrite(10, TimeUnit.MINUTES)
.build();
cache.put("key", "value");
String value = cache.getIfPresent("key");
In this example, a cache is created with a maximum size of 1000 entries and a 10-minute expiration time after writing. The cache stores key-value pairs and can retrieve values efficiently.