Last active
November 23, 2015 12:39
-
-
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
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
| 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