【问题标题】:Difference between net.sf.ehcache and org.ehcache?net.sf.ehcache 和 org.ehcache 的区别?
【发布时间】:2019-01-07 16:54:19
【问题描述】:

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

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

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

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

【问题讨论】:

标签: spring caching ehcache-3


【解决方案1】:

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

【讨论】:

  • 包装差异很明显。另一个区别是 Ehcache2 在 SVN 存储库上,而 Ehcache3 在 GIT 存储库上。但是就没有其他区别了吗?
【解决方案2】:

在许多层面上都有不同。使用 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-07
    • 2011-10-20
    • 2020-01-23
    • 1970-01-01
    • 2014-10-09
    • 2010-12-21
    • 2011-05-11
    • 2014-01-24
    相关资源
    最近更新 更多