1. 程式人生 > 實用技巧 >httpcache4j 一個不錯的httpcache 處理包

httpcache4j 一個不錯的httpcache 處理包

httpcache4j實現了http 的rfc2616 大部分協議約定,可以用來方便的控制資源的cache,同時也是遵循了http 的cache 規範
以下是一個參考使用

demo 說明

就是一個對於圖片下載的服務處理,第一次請求資料會cache(本地磁碟)後邊請求就不需要了

專案準備

  • pom.xml
    有一些冗餘的包引用,可以不用管
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.dalong</groupId>
  <artifactId>myhashids</artifactId>
  <version>1.0-SNAPSHOT</version>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <encoding>UTF-8</encoding>
    <java.version>1.8</java.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.22</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>1.7.21</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.httpcache4j</groupId>
      <artifactId>httpcache4j-core</artifactId>
      <version>5.1.0</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.httpcache4j.storage</groupId>
      <artifactId>storage-file</artifactId>
      <version>5.1.0</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.httpcache4j.resolvers</groupId>
      <artifactId>resolvers-commons-httpclient</artifactId>
      <version>5.1.0</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.httpcache4j.resolvers</groupId>
      <artifactId>resolvers-httpcomponents-httpclient</artifactId>
      <version>5.1.0</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.httpcache4j.resolvers</groupId>
      <artifactId>resolvers-ning-async</artifactId>
      <version>5.1.0</version>
    </dependency>
    <dependency>
      <groupId>org.hashids</groupId>
      <artifactId>hashids</artifactId>
      <version>1.0.3</version>
    </dependency>
  </dependencies>
</project>
  • 程式碼使用
    Application.java
package com.dalong;
import org.codehaus.httpcache4j.HTTPRequest;
import org.codehaus.httpcache4j.HTTPResponse;
import org.codehaus.httpcache4j.cache.FilePersistentCacheStorage;
import org.codehaus.httpcache4j.cache.HTTPCache;
import org.codehaus.httpcache4j.client.HTTPClientResponseResolver;
import org.codehaus.httpcache4j.util.IOUtils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
/**
 * @author dalong
 */
public class Application {
  public static void main(String[] args) throws URISyntaxException, IOException {
    httpCacheLearning();
   }
  public static void httpCacheLearning() throws URISyntaxException, IOException {
    File file =new File("./mycache");
    FilePersistentCacheStorage filePersistentCacheStorage =new FilePersistentCacheStorage(file);
    HTTPCache httpCache =new HTTPCache(filePersistentCacheStorage, HTTPClientResponseResolver.createMultithreadedInstance());
    HTTPResponse httpResponse= httpCache.execute(new HTTPRequest(new URI("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1604414122241&di=89d0b74128a600ebf054830beabf3535&imgtype=0&src=http%3A%2F%2Fimg.pconline.com.cn%2Fimages%2Fupload%2Fupc%2Ftx%2Fwallpaper%2F1307%2F10%2Fc3%2F23153395_1373426315898.jpg")));
    InputStream inputStream = httpResponse.getPayload().get().getInputStream();
    byte[] bytes = IOUtils.toByteArray(inputStream);
    System.out.println(new String(bytes));
    httpCache.shutdown();
   }
}
  • 執行效果

說明第二次請求走的是本地儲存,不需要再次請求圖片,同時我們嘗試斷開網路連線也是可以使用的

說明

golang 的httpcache 也是一個特別好用的庫,使用簡單,而且高效(支援了多種cache 的處理,redis,memcache,disk,s3,memory。。。以及cache 的合併。。。)httpcache4j 已經很久沒有維護了,但是是一個設計不錯的包,提供了各種擴充套件介面,我們直接可以複用,實際上對於httpcache4j的cache 處理上有點感覺不是很靈活的地方是資料以及元資料,httpcache 設計的時候就比較簡單,直接儲存位元組陣列到後端儲存中,內容就是http 請求相應回來的資料

參考資料

https://tools.ietf.org/html/rfc2616#section-13
https://github.com/httpcache4j/httpcache4j
https://github.com/gregjones/httpcache