diff options
author | Christopher Vogt <oss.nsp@cvogt.org> | 2017-03-19 20:03:08 -0400 |
---|---|---|
committer | Christopher Vogt <oss.nsp@cvogt.org> | 2017-03-27 23:54:16 -0400 |
commit | 4185639097f8dc546c6474095c1fe853d43a1069 (patch) | |
tree | f69d90aac7f7fc3801f92e0511069f4239b9bf2e /stage1/Stage1Lib.scala | |
parent | d24aa27365015d024d5b11dbbfbbe5f3fceda4fb (diff) | |
download | cbt-4185639097f8dc546c6474095c1fe853d43a1069.tar.gz cbt-4185639097f8dc546c6474095c1fe853d43a1069.tar.bz2 cbt-4185639097f8dc546c6474095c1fe853d43a1069.zip |
performance tweak: cache results of parsed pom files in memory
Diffstat (limited to 'stage1/Stage1Lib.scala')
-rw-r--r-- | stage1/Stage1Lib.scala | 46 |
1 files changed, 31 insertions, 15 deletions
diff --git a/stage1/Stage1Lib.scala b/stage1/Stage1Lib.scala index 9e6f6b0..3ffc878 100644 --- a/stage1/Stage1Lib.scala +++ b/stage1/Stage1Lib.scala @@ -310,23 +310,39 @@ ${sourceFiles.sorted.mkString(" \\\n")} groupId, artifactId ++ "_" ++ scalaMajorVersion, version, classifier, verifyHash ) - def cacheOnDisk[T] - ( cbtLastModified: Long, cacheFile: File ) + def cacheOnDisk[T,J <: AnyRef : scala.reflect.ClassTag] + ( cbtLastModified: Long, cacheFile: File, persistentCache: java.util.Map[AnyRef,AnyRef] ) ( deserialize: String => T ) ( serialize: T => String ) - ( compute: => Seq[T] ) = { - if(cacheFile.exists && cacheFile.lastModified > cbtLastModified ){ - import collection.JavaConversions._ - Files - .readAllLines( cacheFile.toPath, StandardCharsets.UTF_8 ) - .toStream - .map(deserialize) - } else { - val result = compute - val string = result.map(serialize).mkString("\n") - write(cacheFile, string) - result - } + ( dejavafy: J => Seq[T] ) + ( javafy: Seq[T] => J ) + ( compute: => Seq[T] ): Seq[T] = { + val key = "cacheOnDisk:" + cacheFile + Option( persistentCache.get(key) ).map( + _.asInstanceOf[Array[AnyRef]] + ).map{ + case Array(time: java.lang.Long, javafied: J) => (time, javafied) + }.filter( _._1 > cbtLastModified ) + .map( _._2 ) + .map( dejavafy ) + .orElse{ + (cacheFile.exists && cacheFile.lastModified > cbtLastModified).option{ + import collection.JavaConversions._ + val v = Files + .readAllLines( cacheFile.toPath, StandardCharsets.UTF_8 ) + .toStream + .map( deserialize ) + persistentCache.put(key, Array(System.currentTimeMillis:java.lang.Long, javafy(v))) + v + } + }.getOrElse{ + val result = compute + val strings = result.map(serialize) + val string = strings.mkString("\n") + write(cacheFile, string) + persistentCache.put(key, Array(System.currentTimeMillis:java.lang.Long, javafy(result))) + result + } } def dependencyTreeRecursion(root: Dependency, indent: Int = 0): String = ( |