Last active
August 29, 2015 14:01
-
-
Save rocketraman/b3bf9abdc2a063cf73c1 to your computer and use it in GitHub Desktop.
Spray caching test with no compression
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 akka.actor.ActorSystem | |
import java.io.FileWriter | |
import scala.Some | |
import scala.util.{Failure, Success} | |
import spray.http.HttpEncodings._ | |
import spray.http.HttpHeaders.`Accept-Encoding` | |
import spray.http.HttpMethods.GET | |
import spray.http._ | |
import spray.httpx.encoding.{Gzip, Deflate, NoEncoding} | |
import spray.routing.RequestContext | |
import spray.routing.SimpleRoutingApp | |
import spray.routing.directives.{CacheKeyer, CachingDirectives} | |
object TestCacheOpen extends App with SimpleRoutingApp with CachingDirectives { | |
implicit val system = ActorSystem("simple-routing-app") | |
import system.dispatcher | |
case class CustomCacheKey(uri: Uri, encoding: Option[HttpEncoding]) | |
final val indexHtml = "test.html" | |
final val encoders = (Gzip, Deflate, NoEncoding) | |
final val httpEncoders = encoders.map(_.encoding) | |
implicit val cacheKeyUriAndEncoding = CacheKeyer { | |
case RequestContext(r, _, _) => | |
val uri = r.uri | |
val encoding = httpEncoders.find(r.isEncodingAccepted(_)) | |
CustomCacheKey(uri, encoding) | |
} | |
val output = new FileWriter(indexHtml) | |
output.write(""" | |
|<html> | |
|<body> | |
|<p>This is a test! Use 'strace -e trace=file -f -p <pid>' in order to see file opens.</p> | |
|<p>To test with gzip encoding, but bypass cache (will do an 'open' on test.html):</p> | |
|<pre>curl -s --header 'Accept-Encoding: gzip' --header 'Cache-Control: no-cache' http://localhost:8080/</pre> | |
|<p>To test with gzip encoding but use cache (correctly does 'open' on test.html only on the first call):</p> | |
|<pre>curl -s --header 'Accept-Encoding: gzip' http://localhost:8080/</pre> | |
|<p>To test with no encoding but use cache (strangely does an 'open' on test.html on every call, seemingly bypassing the cache):</p> | |
|<pre>curl -s --header 'Accept-Encoding: identity' http://localhost:8080/</pre> | |
|<p>The reason for the above is here: https://groups.google.com/d/msg/spray-user/PoYuBCt_7m0/m-qgzsdy41QJ</p> | |
|</body> | |
|</html> | |
""".stripMargin) | |
output.close() | |
startServer("localhost", port = 8080) { | |
cache(routeCache()) { | |
(get & compressResponse(encoders)) { | |
pathSingleSlash { | |
getFromFile(indexHtml) | |
} | |
} | |
} | |
}.onComplete { | |
case Success(b) => | |
println(s"Successfully bound to ${b.localAddress}") | |
case Failure(ex) => | |
println(ex.getMessage) | |
system.shutdown() | |
} | |
implicit def tuple3ToList[T](t: (T, T, T)): List[T] = List(t._1, t._2, t._3) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment