Created
January 12, 2017 11:55
-
-
Save ScalaWilliam/b4327be6d73973d304204551a65dd793 to your computer and use it in GitHub Desktop.
Spike for HTTP Caching... blogspot works with hc, vynar works with both hc and okhttp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scalaVersion := "2.12.1" | |
// https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp | |
libraryDependencies += "com.squareup.okhttp3" % "okhttp" % "3.5.0" | |
// https://mvnrepository.com/artifact/org.apache.httpcomponents/fluent-hc | |
libraryDependencies += "org.apache.httpcomponents" % "fluent-hc" % "4.5.2" | |
// https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient-cache | |
libraryDependencies += "org.apache.httpcomponents" % "httpclient-cache" % "4.5.2" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File | |
import java.util.concurrent.TimeUnit | |
import okhttp3.{Cache, CacheControl, OkHttpClient, Request} | |
import org.apache.http.impl.client.cache.{CacheConfig, CachingHttpClientBuilder} | |
object ExpApp extends App { | |
def ok(): Unit = { | |
// val theUrl = "https://actionfps.blogspot.com/feeds/posts/default" | |
val theUrl = "http://vynar.com" | |
// val theUrl = "http://publicobject.com/helloworld.txt" | |
val cacheControl = new CacheControl.Builder() | |
// .maxStale(365, TimeUnit.DAYS) | |
.maxAge(5, TimeUnit.SECONDS) | |
.build() | |
val request = new Request.Builder() | |
// .cacheControl(cacheControl) | |
.url(theUrl) | |
.build() | |
val cache = new Cache(new File("tmp_cache"), 10 * 1024 * 1024) | |
val client = new OkHttpClient.Builder() | |
.cache(cache) | |
.build() | |
val result = client.newCall(request).execute() | |
println(result.code()) | |
println(result.headers()) | |
result.body().string() | |
Thread.sleep(2000) | |
val result2 = client.newCall(request).execute() | |
println(result2.code()) | |
println(result2.headers()) | |
result2.body().string() | |
Thread.sleep(3000) | |
val result3 = client.newCall(request).execute() | |
println(result3.code()) | |
println(result3.headers()) | |
result3.body().string() | |
println(cache.hitCount(), cache.networkCount(), cache.requestCount()) | |
} | |
Har.hc() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.http.client.cache.HttpCacheContext | |
import org.apache.http.client.fluent.Request | |
import org.apache.http.client.methods.HttpGet | |
import org.apache.http.impl.client.CloseableHttpClient | |
import org.apache.http.impl.client.cache.CachingHttpClientBuilder | |
import org.apache.http.util.EntityUtils | |
/** | |
* Created by me on 12/01/2017. | |
*/ | |
object Har { | |
def hc(): Unit = { | |
val client: CloseableHttpClient = CachingHttpClientBuilder.create().build() | |
// val theUrl = "http://vynar.com" | |
val theUrl = "https://actionfps.blogspot.com/feeds/posts/default" | |
val httpGet = new HttpGet(theUrl) | |
val context = HttpCacheContext.create() | |
def doIt(): Unit = { | |
val resp = client.execute(httpGet, context) | |
val str = EntityUtils.toString(resp.getEntity) | |
println(resp.getAllHeaders.toList) | |
// println(str) | |
println(context.getCacheResponseStatus) | |
} | |
doIt() | |
doIt() | |
doIt() | |
doIt() | |
doIt() | |
doIt() | |
doIt() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment