-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1384 from jrudolph/fix-redownloading
Use a simple cache based on futures to avoid redownloading on concurrent usage
- Loading branch information
Showing
3 changed files
with
101 additions
and
47 deletions.
There are no files selected for viewing
This file contains 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
58 changes: 58 additions & 0 deletions
58
scalafmt-dynamic/src/main/scala/org/scalafmt/dynamic/utils/ReentrantCache.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.scalafmt.dynamic.utils | ||
|
||
import scala.annotation.tailrec | ||
import scala.concurrent.Await | ||
import scala.concurrent.Future | ||
import scala.concurrent.Promise | ||
import scala.concurrent.duration._ | ||
import scala.util.Try | ||
|
||
class ReentrantCache[K, V] { | ||
private[this] var cache: Map[K, Future[V]] = Map.empty | ||
|
||
@tailrec | ||
final def getOrAddToCache(key: K, shouldEvict: V => Boolean = _ => false)( | ||
get: () => V | ||
): V = | ||
synchronized { // try to exit quickly from synchronized block | ||
cache.get(key) match { | ||
case Some(fut) => Right(fut) | ||
case None => | ||
val p = Promise[V]() | ||
cache += key -> p.future | ||
Left(p) | ||
} | ||
} match { | ||
case Right(fut) => | ||
// we set the timeout to 10 minutes because | ||
// we can't expect everybody to have the same internet connection speed. | ||
// | ||
// get or wait for other thread to finish download | ||
val result = Await.result(fut, 10.minute) | ||
|
||
if (shouldEvict(result)) { | ||
synchronized { | ||
cache -= key | ||
} | ||
getOrAddToCache(key, shouldEvict)(get) | ||
} else | ||
result | ||
case Left(p) => | ||
val result = Try(get()) | ||
p.complete(result) | ||
result.get | ||
} | ||
|
||
def clear(): Iterable[Future[V]] = | ||
synchronized { | ||
val oldValues = cache.values | ||
cache = Map.empty | ||
oldValues | ||
} | ||
|
||
def getFromCache(key: K): Option[V] = | ||
cache.get(key).map(Await.result(_, 10.minute)) | ||
} | ||
object ReentrantCache { | ||
def apply[K, V](): ReentrantCache[K, V] = new ReentrantCache[K, V] | ||
} |
This file contains 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