Skip to content

Instantly share code, notes, and snippets.

@ScalaWilliam
Created January 12, 2017 11:55
Show Gist options
  • Save ScalaWilliam/b4327be6d73973d304204551a65dd793 to your computer and use it in GitHub Desktop.
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
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"
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()
}
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