11

net.sf.ehcache 和 org.ehcache 有什么区别?

net.sf.ehcache 的当前版本是 2.10.5,而 org.ehcache 相同的是 3.5.2。

Spring 使用 net.sf.ehcache 的 CacheManager,而 org.ehcache 的 CacheManager 不兼容。

这有什么具体原因吗?请解释。

4

2 回答 2

4

正如您可以在页面http://www.ehcache.org/downloads/上验证的那样,Ehcache 3 正在使用包前缀org.ehcache,而 Ehcache 2 正在使用包前缀net.sf.ehcache。就是这样。

于 2018-07-31T20:23:21.560 回答
1

有很多不同的层次。使用 ehcache 3.x,Element 不再存在。应该直接将键和值放在缓存中,因此您可以在创建缓存时提供类型:

      Cache<Long, String> myCache = cacheManager.getCache("myCache", Long.class, String.class);

因此,在检索值时,您避免了 getObjectValue 的麻烦,而是将 Cache 视为 ConcurrentMap。因此,如果密钥不存在,您将不会得到 NullPointerException,因此您不需要检查 cache.get(cacheKey) != null

cache.get(cacheKey);

实例化 CacheManager 的方式也发生了变化。你不会得到实例,所以它不再是单例了。相反,您会得到一个更好的构建器,尤其是您可以为它提供内联配置参数:

        CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
            .withCache("preConfigured",
                       CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
                                                      ResourcePoolsBuilder.heap(100))
                       .build())
                        .build(true);
于 2019-06-18T10:11:08.313 回答