Skip to content

Instantly share code, notes, and snippets.

@MishaelRosenthal
Last active November 23, 2015 12:39
Show Gist options
  • Select an option

  • Save MishaelRosenthal/dcbc019eb06a324a65d6 to your computer and use it in GitHub Desktop.

Select an option

Save MishaelRosenthal/dcbc019eb06a324a65d6 to your computer and use it in GitHub Desktop.
Ferforms the first fetch. If it results in None, locks, performs the second fetch, and releases
package core
object FetchLockFetch {
/**
* Try to fetch using inexpensiveFetch, obtain lock, try again (using inexpensiveFetch),
* fall back to more expensive expensiveFetch.
*/
def fetchLockFetch[T](inexpensiveFetch: => Option[T], lockFunc: => ((=> Option[T]) => Option[T]),
expensiveFetch: => Option[T]): Option[T] =
inexpensiveFetch.orElse{
lockFunc(inexpensiveFetch.orElse(expensiveFetch))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment