Skip to content

Instantly share code, notes, and snippets.

@bandrzejczak
Created February 9, 2018 22:23
Show Gist options
  • Save bandrzejczak/115990ebc460982bf9fda544cb1c4f55 to your computer and use it in GitHub Desktop.
Save bandrzejczak/115990ebc460982bf9fda544cb1c4f55 to your computer and use it in GitHub Desktop.
def cachingWithTTL[T, M[_]](keyParts: Any*)(value: => M[(T, Duration)])
(implicit cache: Cache[T], mode: Mode[M], flags: Flags): M[T] = {
import mode._
M.flatMap(get(keyParts:_*)) {
case Some(found) => M.pure(found)
case None =>
M.flatMap(value) {
case (v, ttl) =>
M.map(put(keyParts:_*)(v, Some(ttl)))(_ => v)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment