net.sf.ehcache和org.ehcache有什么区别?
net.sf.ehcache的当前版本是2.10.5,而org.ehcache的当前版本也是3.5.2。
Spring使用net.sf.ehcache的CacheManager,而org.ehcache的CacheManager不兼容。
这是有特定原因吗?请解释。
net.sf.ehcache和org.ehcache有什么区别?
net.sf.ehcache的当前版本是2.10.5,而org.ehcache的当前版本也是3.5.2。
Spring使用net.sf.ehcache的CacheManager,而org.ehcache的CacheManager不兼容。
这是有特定原因吗?请解释。
您可以在http://www.ehcache.org/downloads/页面上验证,Ehcache 3使用包前缀org.ehcache
,而Ehcache 2则使用包前缀net.sf.ehcache
。就是这样。
在不同的层面上,有所不同。使用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 = CacheManagerBuilder.newCacheManagerBuilder()
.withCache("preConfigured",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
ResourcePoolsBuilder.heap(100))
.build())
.build(true);