记一次本地缓存的用法
Caffeine 是基于Java 8的高性能,接近最佳的缓存库。
Caffeine使用Google Guava启发的API提供内存缓存。 改进取决于您设计Guava缓存和ConcurrentLinkedHashMap的体验。
maven依赖:
<dependency>
<groupId>com.github.benmanes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.8.5</version>
</dependency>
使用案例:
import com.github.benmanes.caffeine.cache.CacheLoader; import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.LoadingCache; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.concurrent.TimeUnit; @Configuration public class CacheConfig { private static final int EXPIRE_SECONDS = 600; /** * 默认最大数量为3000 */ @Bean public LoadingCache<String, Object> CacheStorage() { return Caffeine.newBuilder() .expireAfterWrite(EXPIRE_SECONDS, TimeUnit.SECONDS) .maximumSize(3000) .build(new CacheLoader<String, Object>() { @Nullable @Override public Integer load(@NonNull String s) throws Exception { return 0; } }); } }

浙公网安备 33010602011771号