Skip to content

Commit

Permalink
#1 Added caching of shape types.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikolak-net committed Jan 14, 2018
1 parent 22aedf4 commit 720f242
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ libraryDependencies ++= Seq(
"org.apache.tinkerpop" % "tinkergraph-gremlin" % "3.3.0",
//TODO: "com.github.mdr" %% "ascii-graphs" % "0.0.3", pending 2.12
"com.indvd00m.ascii.render" % "ascii-render" % "1.2.1", //used instead of above^
"com.github.cb372" %% "scalacache-core" % "0.22.0",
"com.github.cb372" %% "scalacache-caffeine" % "0.22.0",
"org.scala-lang.modules" %% "scala-java8-compat" % "0.8.0",
"org.scala-lang" % "scala-reflect" % scalaVersion.value,
"org.log4s" %% "log4s" % "1.4.0"
Expand Down
17 changes: 17 additions & 0 deletions src/main/scala/net/mikolak/travesty/Registry.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,25 @@ package net.mikolak.travesty
import akka.stream.{Graph, Shape}

import scala.reflect.runtime.universe._
import scalacache.Id

object Registry {
import scalacache.Cache
import scalacache.modes.sync._
import scalacache.caffeine._
import scala.concurrent.duration._
import language.postfixOps

private val cache: Cache[ShapeTypes] = CaffeineCache[ShapeTypes]

def register[T <: Graph[_ <: Shape, _]: TypeTag](g: T): T = {
val shapeTypes = deconstructShape(g)
cache.put(g)(shapeTypes, ttl = Some(5 minutes)) //TODO: move to config
g
}

def lookup(g: Graph[_ <: Shape, _]): Id[Option[ShapeTypes]] = cache.get(g)

private[travesty] def deconstructShape[T <: Graph[_ <: Shape, _]: TypeTag](g: T): ShapeTypes = {
val tpe = typeOf[T]
val graphType = tpe.baseType(typeOf[Graph[_, _]].typeSymbol)
Expand Down
29 changes: 29 additions & 0 deletions src/test/scala/net/mikolak/travesty/RegistrySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,35 @@ class RegistrySpec extends FlatSpec with MustMatchers with MustVerb with TableDr
tested(Flow[A].mapAsync(3)(a => Future.successful(a.toB)).async) must be(ShapeTypes(List(typeOf[A]), List(typeOf[B])))
}
}

{
def when[T <: Graph[_ <: Shape, _]: TypeTag](g: T) = Registry.register(g)

"register" must "remember shape immediately after saving" in {
val s = Source.single("t")

when(s)

Registry.lookup(s) must be a 'nonEmpty
}

it must "not remember a non-saved shape" in {
val s = Source.single("t")

Registry.lookup(s) must be an 'empty
}

it must "remember a shape after a short time" in {
val s = Flow[B].map(identity)

when(s)

Thread.sleep(100)

Registry.lookup(s) must be a 'nonEmpty
}

}
}

trait A {
Expand Down

0 comments on commit 720f242

Please sign in to comment.