Guava

本地缓存性能之王Caffeine

天涯浪子 提交于 2021-02-11 10:51:10
前言 随着互联网的高速发展,市面上也出现了越来越多的网站和app。我们判断一个软件是否好用,用户体验就是一个重要的衡量标准。比如说我们经常用的微信,打开一个页面要十几秒,发个语音要几分钟对方才能收到。相信这样的软件大家肯定是都不愿意用的。软件要做到用户体验好,响应速度快,缓存就是必不可少的一个神器。缓存又分进程内缓存和分布式缓存两种:分布式缓存如redis、memcached等,还有本地(进程内)缓存如ehcache、GuavaCache、Caffeine等。说起Guava Cache,很多人都不会陌生,它是Google Guava工具包中的一个非常方便易用的本地化缓存实现,基于LRU算法实现,支持多种缓存过期策略。由于Guava的大量使用,Guava Cache也得到了大量的应用。但是,Guava Cache的性能一定是最好的吗?也许,曾经它的性能是非常不错的。正所谓长江后浪推前浪,前浪被拍在沙滩上。我们就来介绍一个比Guava Cache性能更高的缓存框架: Caffeine 。 Tips: Spring5(SpringBoot2)开始用Caffeine取代guava.详见官方信息SPR-13797 https://jira.spring.io/browse/SPR-13797 官方性能比较 以下测试都是基于 jmh 测试的,官网地址 测试为什么要基于jmh测试,可以

Using Guava multisets or Collections.frequency()?

徘徊边缘 提交于 2021-02-11 07:27:46
问题 I was using Multiset to have easy access to the freq of elements, but I realize there is Collections#frequency(Collection<?>, Object) that does the same for any collection. What is the point of using Multiset then? Is performance an issue here? 回答1: Guava documentation for Multiset#count() has to say: Note that for an Object.equals(java.lang.Object)-based multiset, this gives the same result as Collections.frequency(java.util.Collection, java.lang.Object) (which would presumably perform more

Using Guava multisets or Collections.frequency()?

♀尐吖头ヾ 提交于 2021-02-11 07:27:04
问题 I was using Multiset to have easy access to the freq of elements, but I realize there is Collections#frequency(Collection<?>, Object) that does the same for any collection. What is the point of using Multiset then? Is performance an issue here? 回答1: Guava documentation for Multiset#count() has to say: Note that for an Object.equals(java.lang.Object)-based multiset, this gives the same result as Collections.frequency(java.util.Collection, java.lang.Object) (which would presumably perform more

idea查看jar冲突和解决方法

半城伤御伤魂 提交于 2021-02-09 19:03:56
选中Dependencies,点上边那个按钮,出现下图 依赖图太小了,根本没法看啊?好办,点击鼠标右键,呼出右键菜单栏,然后点击 Actual Size : 如果我们仔细观察上图,会发现在项目依赖图中,有一些红色标记的线,实际上,这些红色标记出来的线所指向的 jar 包,就是项目中冲突的 jar 包!且在我们点击 jar 包之后,还会显示出多条指向 jar 包的红色虚线,其代表着该 jar 包被多次引用,及具体引用路径 解决方案 方案1、点击冲突的 jar 包,右键呼出菜单栏,点击 Exclude 选项。 在排除冲突的 jar 包之后, pom.xml 文件会自动更新,添加排除语句。 方案2、排除所有,在外提供一个最高版本的依赖 例如 com.google.guava冲突了 在引入swagger的maven依赖时 <!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> <exclusions> <exclusion> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </exclusion> <

Is Guava's ImmutableList.Builder thread safe?

本秂侑毒 提交于 2021-02-08 12:20:20
问题 What are the thread safety guarantees for Guava's ImmutableList.Builder? The javadocs don't say. 回答1: While the Guava Immutable classes are threadsafe, their builders are not. For most applications, only one thread will interact with any particular Builder instance. While the absence of thread-safety usually doesn't need to be documented, such Javadoc might make sense for the Immutable collection builders. People may be surprised that ImmutableList is threadsafe while ImmutableList.Builder

Spring force @Cacheable to use putifAbsent instead of put

江枫思渺然 提交于 2021-02-08 10:01:44
问题 I've Spring cache implemented as below @Component public class KPCacheExample { private static final Logger LOG = LoggerFactory.getLogger(KPCacheExample.class); @CachePut(value="kpCache") public String saveCache(String userName, String password){ LOG.info("Called saveCache"); return userName; } @Cacheable(value="kpCache") public String getCache(String userName, String password){ LOG.info("Called getCache"); return "kp"; } } And Java Config file @Configuration @ComponentScan(basePackages={"com

Sort a list with known values before unknown values

心不动则不痛 提交于 2021-02-07 19:30:17
问题 I'm trying to sort a list with the following rules: Known values should be ordered before unknown values. Known values should be ordered by a separately defined key. Unknown values should be ordered by their natural ordering. I've got (1) and (2), just struggling with adding (3) in to the mix. So far I have this: List<String> values = Arrays.asList( "red", "orange", "yellow", "green", "blue", "indigo", "violet"); ImmutableMap<String, Integer> map = ImmutableMap.of("red", 1, "green", 2, "blue"

LoadingCache with async loading

为君一笑 提交于 2021-02-07 12:48:18
问题 In guava, when using LoadingCache CacheLoader is called synchronously. However, my load() operation may take too long (~1 sec), I want to take a default action in case it takes too long (>200 ms) and load the value asynchronously. Is there a way to achieve this? Or are there any other approaches you can recommend? 回答1: The Caffeine library is a Java 8 rewrite of Guava's cache that allows asynchronous automatic loading of entries into a cache, returning CompletableFutures. It is written by

LoadingCache with async loading

梦想的初衷 提交于 2021-02-07 12:48:05
问题 In guava, when using LoadingCache CacheLoader is called synchronously. However, my load() operation may take too long (~1 sec), I want to take a default action in case it takes too long (>200 ms) and load the value asynchronously. Is there a way to achieve this? Or are there any other approaches you can recommend? 回答1: The Caffeine library is a Java 8 rewrite of Guava's cache that allows asynchronous automatic loading of entries into a cache, returning CompletableFutures. It is written by

How to import Guava into Android applications

北战南征 提交于 2021-02-05 10:47:05
问题 What is the proper way to import Guava into an Android project? Every time I try to use it I get a NoClassDefFoundError . This is what I'm doing to generate the crash. I'm using Android Studio 3.0 Canary 7. Create an new project File > New > New Project , target API 26.0 , using the Empty Activity template. Add to app/build.gradle in the dependencies section implementation "com.google.guava:guava:20.0" Add this to the onCreate method in MainActivity.java ImmutableList<String> foo =