Add library and resolver in build.sbt
libraryDependencies ++= Seq(
"org.janzhou" %%% "scalajs-cache" % "0.0.2"
)
resolvers += "Jian Zhou" at "https://raw.githubusercontent.com/janzhou/mvn-repo/release"
import org.janzhou.scalajs.Cache
object cached extends Cache("cached", 1000, 60 * 60 * 1000) {
lazy val sub = Cache("cached.sub", 1000, 60 * 60 * 1000)
def cleanAll = {
rm()
sub.rm()
}
}
Store data into a "key". The data will be serialized by using uPickle.
cached.set("key", data)
The expiration data can be specified per-key.
cached.set("key", data, expiration)
The Future object can be cached.
val newFuture = cached("future", Future{ real job })
This function will return a new Future with the cached value. Otherwise, the real job will be scheduled. This can be used in conjunction with a remote call.
Cache a lambda expression.
val newValue = cached("lambda", { real job })
This function will return the cached value. Otherwise, the real job will be executed.