Skip to content

Commit

Permalink
add CachedFileStorage
Browse files Browse the repository at this point in the history
This uses lastModified of all files to cache FileStorage instance.
Preview should get much faster on larger pamflets.
  • Loading branch information
eed3si9n committed Jan 25, 2015
1 parent 7a99661 commit 95b00fd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app/src/main/scala/app.scala
Expand Up @@ -12,7 +12,7 @@ object Pamflet {
def main(args: Array[String]) {
System.exit(run(args))
}
private def storage(dir: File) = FileStorage(dir)
private def storage(dir: File) = CachedFileStorage(dir)
def run(args: Array[String]) = {
args match {
case Array(Dir(input), Dir(output)) =>
Expand All @@ -34,7 +34,7 @@ object Pamflet {
1
}
}
def preview(dir: File) = {
def preview(dir: File): Int = {
Preview(storage(dir).globalized).run { server =>
unfiltered.util.Browser.open(
"http://127.0.0.1:%d/".format(server.portBindings.head.port)
Expand Down
32 changes: 31 additions & 1 deletion library/src/main/scala/storage.scala
@@ -1,14 +1,44 @@
package pamflet

import java.io.File
import java.nio.charset.Charset
import com.tristanhunt.knockoff._
import collection.immutable.Map
import java.nio.charset.Charset
import collection.concurrent.TrieMap

trait Storage {
def globalized: Globalized
}

/** Cache FileStorage based on the last modified time.
* This should make previewing much faster on large pamflets.
*/
case class CachedFileStorage(base: File) extends Storage {
def allFiles(f0: File): Seq[File] =
f0.listFiles.toVector flatMap {
case dir if dir.isDirectory => allFiles(dir)
case f => Vector(f)
}
def maxLastModified(f0: File): Long =
(allFiles(f0) map {_.lastModified}).max

def globalized = {
val lm = maxLastModified(base)
CachedFileStorage.cache.get(base) match {
case Some((lm0, gl0)) if lm == lm0 => gl0
case _ =>
val st = FileStorage(base)
val gl = st.globalized
CachedFileStorage.cache(base) = (lm, gl)
gl
}
}
}

object CachedFileStorage {
val cache: TrieMap[File, (Long, Globalized)] = TrieMap()
}

case class FileStorage(base: File) extends Storage {
def propFile(dir: File): Option[File] =
new File(dir, "template.properties") match {
Expand Down

0 comments on commit 95b00fd

Please sign in to comment.