When selecting a library is troublesome and you don't want to think too much but want to cache objects. If the cache size is small and you do not need to clear the cache while the JVM is running. However, it must support parallel processing. With these requirements, you can easily implement it without the need for a library.
ConcurrentHashMap # computeIfAbsent
use
For example, if you want to get a public field from a specific class, it looks like this.
public class PublicFieldCache {
private static final Map<Class<?>, Field[]> cache = new ConcurrentHashMap<>();
public static Field[] getFields(Class<?> type) {
return cache.computeIfAbsent(type, Class::getFields);
}
}
Recommended Posts