Mastering Play Framework for Scala by 2015

Mastering Play Framework for Scala by 2015

Author:2015
Language: eng
Format: mobi, epub
Publisher: Packt Publishing


All of this is handled by the Cached case class and its companion object. The case class is defined as follows:

case class Cached(key: RequestHeader => String, caching: PartialFunction[ResponseHeader, Duration]) { … }

The companion object provides commonly required methods to generate cached instances, such as cache action based on its status, and so on.

The apply method in cached calls the build method, which is defined as follows:

def build(action: EssentialAction)(implicit app: Application) = EssentialAction { request => val resultKey = key(request) val etagKey = s"$resultKey-etag" // Has the client a version of the resource as fresh as the last one we served? val notModified = for { requestEtag <- request.headers.get(IF_NONE_MATCH) etag <- Cache.getAs[String](etagKey) if requestEtag == "*" || etag == requestEtag } yield Done[Array[Byte], Result](NotModified) notModified.orElse { // Otherwise try to serve the resource from the cache, if it has not yet expired Cache.getAs[Result](resultKey).map(Done[Array[Byte], Result](_)) }.getOrElse { // The resource was not in the cache, we have to run the underlying action val iterateeResult = action(request) // Add cache information to the response, so clients can cache its content iterateeResult.map(handleResult(_, etagKey, resultKey, app)) } }



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.