注意:Ehcache's LOCALRESTARTABLE strategy 需要企业许可证。好吧,如果我有 EhCache 的商业许可证,这将完美运行,而我没有。如果我知道这一点,我就不会遇到麻烦了。
使用 google 和 Httpclient 的文档进一步挖掘,您需要做的第一件事是将 httpclient-cache-<version>.jar 添加到您的类路径中。
之后,使用org.apache.http.impl.client.cache.CachingHttpClients 而不是org.apache.http.impl.client.HttpClients
现在您可以根据文档构建配置。
CloseableHttpClient httpclient = CachingHttpClients.custom()
.setCacheConfig(cacheConfig)
.setHttpCacheStorage(ehcacheHttpCacheStorage)
.build();
这让您试图弄清楚如何配置缓存配置,这相当容易。
//build the cacheconfig. this part belongs to httpclient
CacheConfig cacheConfig = CacheConfig
.custom()
.setMaxCacheEntries(1000)
.setMaxObjectSize(8192)
.build();
接下来要担心的是EhCacheHttpCacheStorage,这是相当有问题的,因为EhcacheHttpCacheStorage 构造函数采用EhCache,但没有直接的方法来初始化它。你需要做的是构造一个CacheManager,给它添加一个缓存,把它从CacheManager中取出,最后包装到EhcacheHttpCacheStorage中
第 1 步:创建管理器:
mgr = CacheManager.create();
第 2 步:创建缓存:
Cache testCache = new Cache(new CacheConfiguration("http", 100000)
.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
.eternal(false)
.timeToLiveSeconds(60)
.timeToIdleSeconds(30)
.diskExpiryThreadIntervalSeconds(0)
//Persist everything and keep it on restart
.persistence(new PersistenceConfiguration().strategy(Strategy.LOCALRESTARTABLE)));
第 3 步:将其添加到管理器中:
mgr.addCache(testCache);
第 4 步:从管理器中取出缓存:
Cache cache = mgr.getCache("http");
第5步:将其包装到实现HttpCacheStorage的EhcacheHttpCacheStorage中
EhcacheHttpCacheStorage ehcacheHttpCacheStorage = new EhcacheHttpCacheStorage(cache);
为了确保您可以在家中尝试,这里的所有内容都按正确的顺序排列,并带有必要的导入
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.Configuration;
import net.sf.ehcache.config.DiskStoreConfiguration;
import net.sf.ehcache.config.PersistenceConfiguration;
import net.sf.ehcache.config.PersistenceConfiguration.Strategy;
import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
import org.apache.http.impl.client.cache.CacheConfig;
import org.apache.http.impl.client.cache.CachingHttpClients;
import org.apache.http.impl.client.cache.ehcache.EhcacheHttpCacheStorage;
//build the cacheconfig. this part belongs to httpclient
CacheConfig cacheConfig = CacheConfig
.custom()
.setMaxCacheEntries(1000)
.setMaxObjectSize(8192)
.build();
//this part creates the ehcache
//Create a singleton CacheManager using defaults
mgr = CacheManager.create();
//Create a Cache named http specifying its configuration.
Cache testCache = new Cache(new CacheConfiguration("http", 100000)
.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
.eternal(false)
.timeToLiveSeconds(60)
.timeToIdleSeconds(30)
.diskExpiryThreadIntervalSeconds(0)
//Persist everything and keep it on restart
.persistence(new PersistenceConfiguration().strategy(Strategy.LOCALRESTARTABLE)));
mgr.addCache(testCache);
//get the cache back out the manager
Cache cache = mgr.getCache("http");
// and we are back to http-client
//Wrap it around EhcacheHttpCacheStorage which implements HttpCacheStorage
EhcacheHttpCacheStorage ehcacheHttpCacheStorage = new EhcacheHttpCacheStorage(cache);
//And Configure the httpclient
CloseableHttpClient httpclient = CachingHttpClients.custom()
.setCacheConfig(cacheConfig)
.setHttpCacheStorage(ehcacheHttpCacheStorage)
.build();