【问题标题】:Integrating persistent caching into Apache Httpclient with EhCache使用 EhCache 将持久缓存集成到 Apache Httpclient 中
【发布时间】:2014-04-23 21:20:50
【问题描述】:

今天我想将持久缓存集成到 apache 的 httpclient 库中。快速查看Http-Caching 的文档会告诉您:

当前版本支持使用 EhCache 和 memcached 实现来存储缓存条目,这允许将缓存条目溢出到磁盘或将它们存储在外部进程中。

很公平,稍后我快速的谷歌搜索我完全不知道该做什么以及如何做到这一点;)

但还有一个提示:

如果这些选项都不适合您的应用程序,则可以通过实现 HttpCacheStorage 接口并在构建时将其提供给缓存 HttpClient 来提供您自己的存储后端。

所以至少我们知道 HttpCacheStorage 需要使用一些实现 HttpCacheStorage 的类来初始化。

一个问题:

  • 如何将缓存集成到任何 http 请求中?
  • 如何初始化 EhCache?
  • 如何将 EhCache 集成到 http 缓存中。

【问题讨论】:

    标签: java caching ehcache apache-httpclient-4.x http-caching


    【解决方案1】:

    注意: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();
    

    【讨论】:

    • 顺便说一句,你提到的 EhCache 许可证有什么问题?
    • 好吧,我想我知道你的意思了: strategy:配置配置缓存提供的持久化类型。这必须是以下值之一: * localRestartable - 启用 RestartStore 并将所有缓存条目(堆上和/或堆外)复制到磁盘。此选项通过磁盘上的容错缓存持久性提供快速可重新启动性。 仅适用于 Enterprise Ehcache 用户。
    • 似乎有另一种选择:ManagedHttpCacheStorage + FileResource。即很快:CachingHttpClients.createFileBound(new File("cache/httpclient"))
    • 抱歉,我在不久前发布了这篇文章,并且有一段时间没有使用该技术了。但是没有我的帮助,你似乎做得很好:)
    猜你喜欢
    • 1970-01-01
    • 2016-11-22
    • 2018-04-14
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 2019-03-20
    • 2020-10-23
    • 1970-01-01
    相关资源
    最近更新 更多