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