Skip to content

Instantly share code, notes, and snippets.

@betandr
Last active August 26, 2015 13:12
Show Gist options
  • Save betandr/1a453e30198a20ac5e20 to your computer and use it in GitHub Desktop.
Save betandr/1a453e30198a20ac5e20 to your computer and use it in GitHub Desktop.
Recursive find of a value in a Cache-Control HTTP header value string
def find(key: String, in: String): scala.util.Try[Int] = {
scala.util.Try {
def findValue(items: Array[String]): String = {
val item = items.head
if (item.contains(key)) { item.split("=").last }
else { findValue(items.drop(1)) }
}
findValue(in.split(", ")).toInt
}
}
// NB: This returns 300 for "max-a" and the first value of 300 for "max"
val max = find("max-age", "max-age=300, s-maxage=300, stale-while-revalidate=30") match {
case scala.util.Success(value) => value
case scala.util.Failure(ex) => 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment