How to make a cache without thinking too much

Requirements

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.

Implementation method

ConcurrentHashMap # computeIfAbsentuse

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

How to make a cache without thinking too much
How to make a Java container
How to make a JDBC driver
How to make a splash screen
How to make a Jenkins plugin
How to make a Maven project
How to make a Java array
How to make a Java calendar Summary
How to make a Discord bot (Java)
How to make a follow function in Rails
How to make shaded-jar
How to make a factory with a model with polymorphic association
How to update devise user information without a password
How to make a mod for Slay the Spire
Java --How to make JTable
How to leave a comment
[Rails] How to make seed
How to insert a video
How to create a method
SDWebImage: How to clear the cache for a particular UIImageView
Learning Ruby with AtCoder 13 How to make a two-dimensional array
How to output array values without using a for statement
How to join a table without using DBFlute and sql
How to add columns to a table
Try to make a simple callback
How to make a jar file with no dependencies in Maven
How to sign a Minecraft MOD
[Java] How to create a folder
How to write a ternary operator
[Swift] How to send a notification
How to identify the path that is easy to make a mistake
Try to make a peepable iterator
How to make a groundbreaking diamond using Java for statement wwww
[Android] How to make Dialog Fragment
JVM Performance Tuning: What is Tuning and How to Make a Good Plan
How to update user edits in Rails Devise without entering a password
How to make an app with a plugin mechanism [C # and Java]
[Xcode] How to add a README.md file
How to execute a contract using web3j
How to sort a List using Comparator
A memorandum on how to use Eclipse
How to redo a deployment on Heroku
[Basic] How to write a Dockerfile Self-learning ②
How to insert a video in Rails
How to add a new hash / array
[Introduction to Java] How to write a Java program
How to create a Maven repository for 2020
[Swift5] How to create a splash screen
How to make asynchronous pagenations using Kaminari
How to publish a library in jCenter
[SpringBoot] How to write a controller test
How to make a judgment method to search for an arbitrary character in an array
How to make a unique combination of data in the rails intermediate table
It was surprisingly easy. How to update user information without entering a password
Rails: How to write a rake task nicely
How to create a database for H2 Database anywhere
[Rails] How to write when making a subquery
How to deploy a container on AWS Lambda
[Rails] How to create a graph using lazy_high_charts
[Android] How to convert a character string to resourceId
Make a margin to the left of the TextField